Submission #3770470


Source Code Expand

import java.util.*;

class Main{
    static HashSet<Integer> seen = new HashSet<>();
    static long[] frac = new long[17];
    static HashSet<Integer>[] map;
    static int[] degree = new int[17];
    static HashMap<Integer,Long> dp = new HashMap<>();
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        map = new HashSet[n+1];
        frac[0] = 1;
        for(int i=1;i<17;i++) frac[i] = i*frac[i-1];
        for(int i=0;i<=n;i++) map[i] = new HashSet<>();
        for(int i=0;i<m;i++){
            int s = sc.nextInt();
            int t = sc.nextInt();
            map[s].add(t);
            seen.add(s); seen.add(t);
            degree[t]++;
        }
        long topo = dfs(0);
        int sz = seen.size();
        long com = frac[n]/frac[sz]/frac[n-sz];
        long ans = frac[n-sz]*topo*com;
        System.out.println(ans);
    }
    static long  dfs(int state){
        if(dp.containsKey(state)) return dp.get(state);
        if(count(state)==seen.size()){
            dp.put(state,1L);
            return 1;
        }
        long ans = 0;
        for(int w:seen){
            if((state&(1<<w))>0||degree[w]>0) continue; // already taken or not ready to be taken
            state |= 1<<w;
            for(int s:map[w]) degree[s]--;
            ans += dfs(state);
            state &= ~(1<<w);
            for(int s:map[w]) degree[s]++;
        }
        dp.put(state,ans);
        return ans;
    }
    static int count(int i){
        int cnt = 0;
        while(i>0){
            cnt += i%2;
            i /= 2;
        }
        return cnt;
    }
}

Submission Info

Submission Time
Task D - 徒競走
User AlbertZ
Language Java8 (OpenJDK 1.8.0)
Score 100
Code Size 1727 Byte
Status AC
Exec Time 125 ms
Memory 22612 KB

Compile Error

Note: ./Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Judge Result

Set Name Sample Subtask1 All
Score / Max Score 0 / 0 30 / 30 70 / 70
Status
AC × 3
AC × 15
AC × 32
Set Name Test Cases
Sample 0_00.txt, 0_01.txt, 0_02.txt
Subtask1 0_00.txt, 0_01.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt
All 0_00.txt, 0_01.txt, 0_02.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 2_00.txt, 2_01.txt, 2_02.txt, 2_03.txt, 2_04.txt, 2_05.txt, 2_06.txt, 2_07.txt, 2_08.txt, 2_09.txt, 2_10.txt, 2_11.txt, 2_12.txt, 2_13.txt, 2_14.txt, 2_15.txt
Case Name Status Exec Time Memory
0_00.txt AC 112 ms 19796 KB
0_01.txt AC 99 ms 20820 KB
0_02.txt AC 98 ms 21716 KB
1_00.txt AC 97 ms 20692 KB
1_01.txt AC 98 ms 21076 KB
1_02.txt AC 101 ms 21204 KB
1_03.txt AC 98 ms 20692 KB
1_04.txt AC 98 ms 18772 KB
1_05.txt AC 97 ms 19284 KB
1_06.txt AC 99 ms 22612 KB
1_07.txt AC 100 ms 19540 KB
1_08.txt AC 98 ms 20692 KB
1_09.txt AC 99 ms 21712 KB
1_10.txt AC 99 ms 19284 KB
1_11.txt AC 97 ms 19412 KB
1_12.txt AC 96 ms 19796 KB
2_00.txt AC 97 ms 20564 KB
2_01.txt AC 109 ms 21460 KB
2_02.txt AC 107 ms 21712 KB
2_03.txt AC 106 ms 21588 KB
2_04.txt AC 105 ms 18768 KB
2_05.txt AC 107 ms 18644 KB
2_06.txt AC 104 ms 19668 KB
2_07.txt AC 109 ms 21460 KB
2_08.txt AC 116 ms 19668 KB
2_09.txt AC 106 ms 18644 KB
2_10.txt AC 116 ms 19284 KB
2_11.txt AC 106 ms 20688 KB
2_12.txt AC 110 ms 19152 KB
2_13.txt AC 113 ms 21844 KB
2_14.txt AC 125 ms 20180 KB
2_15.txt AC 102 ms 18772 KB