Submission #339246

# Submission time Handle Problem Language Result Execution time Memory
339246 2020-12-25T00:28:12 Z KWang31 Mergers (JOI19_mergers) Java 11
56 / 100
3000 ms 52388 KB
import java.io.*; import java.util.*;
public class mergers{
  static class FastReader 
    { 
        BufferedReader br; 
        StringTokenizer st; 
  
        public FastReader() 
        { 
            br = new BufferedReader(new
                     InputStreamReader(System.in)); 
        } 
  
        String next() 
        { 
            while (st == null || !st.hasMoreElements()) 
            { 
                try
                { 
                    st = new StringTokenizer(br.readLine()); 
                } 
                catch (IOException  e) 
                { 
                    e.printStackTrace(); 
                } 
            } 
            return st.nextToken(); 
        } 
  
        int nextInt() 
        { 
            return Integer.parseInt(next()); 
        } 
  
        
    } 
    public static class Pair implements Comparable<Pair>{
        int vtx; int val;
        public Pair(int a, int b){
            this.vtx=a; this.val=b;
        }
        public int compareTo(Pair other){
            if(this.val<other.val)return -1;
            if(this.val>other.val)return 1;
            if(this.vtx<other.vtx)return -1;
            return 1;
        }
    }
    static int MOD=998244353;
    static int[] rk, p,siz;
    static int[] par,dep;
    public static void main(String[] args){
        FastReader br=new FastReader();
        int N=br.nextInt(); int M=br.nextInt();
        ArrayList<Integer> states[]=new ArrayList[M]; for(int i=0;i<M;i++){states[i]=new ArrayList<>();}
        ArrayList<Integer> adj[]=new ArrayList[N]; for(int i=0;i<N;i++){adj[i]=new ArrayList<>();}
        int a,b;
        for (int i = 0; i < N-1; i++) {
            a=br.nextInt()-1; b=br.nextInt()-1; adj[a].add(b); adj[b].add(a);
        }
        for (int i = 0; i < N; i++) {
            a=br.nextInt()-1; states[a].add(i);
        }
        par=new int[N]; dep=new int[N]; dfs(adj,-1,0);
        
        int[] skip=new int[N];//Skipping: avoiding repeated merging
        for (int i = 0; i < N; i++) {
            skip[i]=i;
        }
        for (int i = 0; i < M; i++) {
            for (int j = 0; j < states[i].size()-1; j++) {
                a=states[i].get(j); b=states[i].get(j+1);
                
                while(a!=b){
                    if(dep[a]>=dep[b]){
                        skip[a]=find(par[a],skip); a=par[a]; 
                    }else{
                        skip[b]=find(par[b],skip); b=par[b]; 
                    }
                }
                //For each a, we have merged a with its 1st, .., kth ancestor.
                //They belong to the same state
            }
        }
        //In the end, all vertices of the same state have the same skip
        //Therefore, after merging, if two vertices don't have some skip, they are not in the same state
        int[] deg=new int[N];
        for (int i = 0; i < N; i++) {
            for (int j : adj[i]) {
                if(find(i,skip)!=find(j,skip)){
                    //Notice in the compressed form, there is only one leader for each state, namely j
                    //If i is adjacent to two vertices with same find, i must have that find too
                    deg[find(i,skip)]++;
                }
            }
        }
        //System.out.println(Arrays.toString(deg));
        int ans=0;
        for (int i = 0; i < N; i++) {
            if(deg[i]==1)ans++;
        }
        System.out.println((ans+1)/2);
        //If a state is a leaf, there exist exactly one 
    }
    
    public static int find(int x, int[] p){
        if(p[x]==x)return x;
        p[x]=find(p[x],p); return p[x];
    }
    
    public static void merge(int a, int b) {
        if(rk[a]<rk[b]) {
            p[a]=b; siz[b]+=siz[a];
        }else if(rk[a]==rk[b]) {
            p[a]=b; rk[b]++;siz[b]+=siz[a];
        }else {
            p[b]=a; siz[a]+=siz[b];
        }
    }
    public static void dfs(ArrayList<Integer>[] adj, int p, int v){
        for (int c : adj[v]) {
            if(c==p)continue;
            dep[c]=dep[v]+1; par[c]=v; dfs(adj,v,c);
        }
    }
}
//Debugging:
//Are you sure your algorithm is correct?
//Bounds: long
//Special cases: n=0,1?
//Make sure you remove your debugging code before you submit!

Compilation message

Note: mergers.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
# Verdict Execution time Memory Grader output
1 Correct 71 ms 8808 KB Output is correct
2 Correct 78 ms 8684 KB Output is correct
3 Correct 78 ms 8684 KB Output is correct
4 Correct 75 ms 8552 KB Output is correct
5 Correct 71 ms 8556 KB Output is correct
6 Correct 76 ms 8556 KB Output is correct
7 Correct 75 ms 8684 KB Output is correct
8 Correct 76 ms 8684 KB Output is correct
9 Correct 70 ms 8556 KB Output is correct
10 Correct 71 ms 8412 KB Output is correct
11 Correct 77 ms 8564 KB Output is correct
12 Correct 80 ms 8540 KB Output is correct
13 Correct 80 ms 8556 KB Output is correct
14 Correct 75 ms 8556 KB Output is correct
15 Correct 78 ms 8556 KB Output is correct
16 Correct 72 ms 8556 KB Output is correct
17 Correct 72 ms 8688 KB Output is correct
18 Correct 77 ms 8556 KB Output is correct
19 Correct 70 ms 8556 KB Output is correct
20 Correct 71 ms 8432 KB Output is correct
21 Correct 80 ms 8556 KB Output is correct
22 Correct 80 ms 8428 KB Output is correct
23 Correct 71 ms 8428 KB Output is correct
24 Correct 71 ms 8556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 8808 KB Output is correct
2 Correct 78 ms 8684 KB Output is correct
3 Correct 78 ms 8684 KB Output is correct
4 Correct 75 ms 8552 KB Output is correct
5 Correct 71 ms 8556 KB Output is correct
6 Correct 76 ms 8556 KB Output is correct
7 Correct 75 ms 8684 KB Output is correct
8 Correct 76 ms 8684 KB Output is correct
9 Correct 70 ms 8556 KB Output is correct
10 Correct 71 ms 8412 KB Output is correct
11 Correct 77 ms 8564 KB Output is correct
12 Correct 80 ms 8540 KB Output is correct
13 Correct 80 ms 8556 KB Output is correct
14 Correct 75 ms 8556 KB Output is correct
15 Correct 78 ms 8556 KB Output is correct
16 Correct 72 ms 8556 KB Output is correct
17 Correct 72 ms 8688 KB Output is correct
18 Correct 77 ms 8556 KB Output is correct
19 Correct 70 ms 8556 KB Output is correct
20 Correct 71 ms 8432 KB Output is correct
21 Correct 80 ms 8556 KB Output is correct
22 Correct 80 ms 8428 KB Output is correct
23 Correct 71 ms 8428 KB Output is correct
24 Correct 71 ms 8556 KB Output is correct
25 Correct 73 ms 8576 KB Output is correct
26 Correct 292 ms 14708 KB Output is correct
27 Correct 310 ms 14800 KB Output is correct
28 Correct 302 ms 14860 KB Output is correct
29 Correct 304 ms 14552 KB Output is correct
30 Correct 299 ms 14560 KB Output is correct
31 Correct 74 ms 8556 KB Output is correct
32 Correct 290 ms 14944 KB Output is correct
33 Correct 73 ms 8556 KB Output is correct
34 Correct 294 ms 14544 KB Output is correct
35 Correct 307 ms 14592 KB Output is correct
36 Correct 316 ms 14672 KB Output is correct
37 Correct 319 ms 14732 KB Output is correct
38 Correct 81 ms 8556 KB Output is correct
39 Correct 338 ms 14692 KB Output is correct
40 Correct 362 ms 14388 KB Output is correct
41 Correct 330 ms 14620 KB Output is correct
42 Correct 299 ms 14692 KB Output is correct
43 Correct 366 ms 14892 KB Output is correct
44 Correct 81 ms 8556 KB Output is correct
45 Correct 348 ms 14648 KB Output is correct
46 Correct 350 ms 14560 KB Output is correct
47 Correct 77 ms 8556 KB Output is correct
48 Correct 300 ms 14540 KB Output is correct
49 Correct 294 ms 14704 KB Output is correct
50 Correct 293 ms 14668 KB Output is correct
51 Correct 304 ms 14740 KB Output is correct
52 Correct 287 ms 14532 KB Output is correct
53 Correct 292 ms 14660 KB Output is correct
54 Correct 324 ms 14708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 8808 KB Output is correct
2 Correct 78 ms 8684 KB Output is correct
3 Correct 78 ms 8684 KB Output is correct
4 Correct 75 ms 8552 KB Output is correct
5 Correct 71 ms 8556 KB Output is correct
6 Correct 76 ms 8556 KB Output is correct
7 Correct 75 ms 8684 KB Output is correct
8 Correct 76 ms 8684 KB Output is correct
9 Correct 70 ms 8556 KB Output is correct
10 Correct 71 ms 8412 KB Output is correct
11 Correct 77 ms 8564 KB Output is correct
12 Correct 80 ms 8540 KB Output is correct
13 Correct 80 ms 8556 KB Output is correct
14 Correct 75 ms 8556 KB Output is correct
15 Correct 78 ms 8556 KB Output is correct
16 Correct 72 ms 8556 KB Output is correct
17 Correct 72 ms 8688 KB Output is correct
18 Correct 77 ms 8556 KB Output is correct
19 Correct 70 ms 8556 KB Output is correct
20 Correct 71 ms 8432 KB Output is correct
21 Correct 80 ms 8556 KB Output is correct
22 Correct 80 ms 8428 KB Output is correct
23 Correct 71 ms 8428 KB Output is correct
24 Correct 71 ms 8556 KB Output is correct
25 Correct 78 ms 8556 KB Output is correct
26 Correct 725 ms 36392 KB Output is correct
27 Correct 789 ms 36892 KB Output is correct
28 Correct 318 ms 14680 KB Output is correct
29 Correct 76 ms 8556 KB Output is correct
30 Correct 83 ms 8684 KB Output is correct
31 Correct 772 ms 36564 KB Output is correct
32 Correct 290 ms 14452 KB Output is correct
33 Execution timed out 3085 ms 39808 KB Time limit exceeded
34 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 730 ms 36544 KB Output is correct
2 Correct 769 ms 50192 KB Output is correct
3 Correct 294 ms 14696 KB Output is correct
4 Correct 382 ms 14680 KB Output is correct
5 Correct 75 ms 8556 KB Output is correct
6 Correct 75 ms 8556 KB Output is correct
7 Correct 290 ms 14576 KB Output is correct
8 Correct 767 ms 38800 KB Output is correct
9 Correct 294 ms 14796 KB Output is correct
10 Correct 744 ms 36836 KB Output is correct
11 Correct 79 ms 8556 KB Output is correct
12 Correct 765 ms 37224 KB Output is correct
13 Correct 766 ms 39316 KB Output is correct
14 Correct 842 ms 43536 KB Output is correct
15 Correct 693 ms 36684 KB Output is correct
16 Correct 293 ms 14560 KB Output is correct
17 Correct 83 ms 8556 KB Output is correct
18 Correct 756 ms 49144 KB Output is correct
19 Correct 1223 ms 52388 KB Output is correct
20 Correct 335 ms 14636 KB Output is correct
21 Correct 79 ms 8684 KB Output is correct
22 Correct 769 ms 41688 KB Output is correct
23 Correct 286 ms 14672 KB Output is correct
24 Correct 735 ms 37204 KB Output is correct
25 Correct 774 ms 45968 KB Output is correct
26 Correct 291 ms 14688 KB Output is correct
27 Correct 289 ms 14812 KB Output is correct
28 Correct 290 ms 14672 KB Output is correct
29 Correct 285 ms 14432 KB Output is correct
30 Correct 287 ms 14556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 8808 KB Output is correct
2 Correct 78 ms 8684 KB Output is correct
3 Correct 78 ms 8684 KB Output is correct
4 Correct 75 ms 8552 KB Output is correct
5 Correct 71 ms 8556 KB Output is correct
6 Correct 76 ms 8556 KB Output is correct
7 Correct 75 ms 8684 KB Output is correct
8 Correct 76 ms 8684 KB Output is correct
9 Correct 70 ms 8556 KB Output is correct
10 Correct 71 ms 8412 KB Output is correct
11 Correct 77 ms 8564 KB Output is correct
12 Correct 80 ms 8540 KB Output is correct
13 Correct 80 ms 8556 KB Output is correct
14 Correct 75 ms 8556 KB Output is correct
15 Correct 78 ms 8556 KB Output is correct
16 Correct 72 ms 8556 KB Output is correct
17 Correct 72 ms 8688 KB Output is correct
18 Correct 77 ms 8556 KB Output is correct
19 Correct 70 ms 8556 KB Output is correct
20 Correct 71 ms 8432 KB Output is correct
21 Correct 80 ms 8556 KB Output is correct
22 Correct 80 ms 8428 KB Output is correct
23 Correct 71 ms 8428 KB Output is correct
24 Correct 71 ms 8556 KB Output is correct
25 Correct 73 ms 8576 KB Output is correct
26 Correct 292 ms 14708 KB Output is correct
27 Correct 310 ms 14800 KB Output is correct
28 Correct 302 ms 14860 KB Output is correct
29 Correct 304 ms 14552 KB Output is correct
30 Correct 299 ms 14560 KB Output is correct
31 Correct 74 ms 8556 KB Output is correct
32 Correct 290 ms 14944 KB Output is correct
33 Correct 73 ms 8556 KB Output is correct
34 Correct 294 ms 14544 KB Output is correct
35 Correct 307 ms 14592 KB Output is correct
36 Correct 316 ms 14672 KB Output is correct
37 Correct 319 ms 14732 KB Output is correct
38 Correct 81 ms 8556 KB Output is correct
39 Correct 338 ms 14692 KB Output is correct
40 Correct 362 ms 14388 KB Output is correct
41 Correct 330 ms 14620 KB Output is correct
42 Correct 299 ms 14692 KB Output is correct
43 Correct 366 ms 14892 KB Output is correct
44 Correct 81 ms 8556 KB Output is correct
45 Correct 348 ms 14648 KB Output is correct
46 Correct 350 ms 14560 KB Output is correct
47 Correct 77 ms 8556 KB Output is correct
48 Correct 300 ms 14540 KB Output is correct
49 Correct 294 ms 14704 KB Output is correct
50 Correct 293 ms 14668 KB Output is correct
51 Correct 304 ms 14740 KB Output is correct
52 Correct 287 ms 14532 KB Output is correct
53 Correct 292 ms 14660 KB Output is correct
54 Correct 324 ms 14708 KB Output is correct
55 Correct 78 ms 8556 KB Output is correct
56 Correct 725 ms 36392 KB Output is correct
57 Correct 789 ms 36892 KB Output is correct
58 Correct 318 ms 14680 KB Output is correct
59 Correct 76 ms 8556 KB Output is correct
60 Correct 83 ms 8684 KB Output is correct
61 Correct 772 ms 36564 KB Output is correct
62 Correct 290 ms 14452 KB Output is correct
63 Execution timed out 3085 ms 39808 KB Time limit exceeded
64 Halted 0 ms 0 KB -