Submission #301438

# Submission time Handle Problem Language Result Execution time Memory
301438 2020-09-18T00:01:58 Z KWang31 Connecting Supertrees (IOI20_supertrees) Java 11
100 / 100
536 ms 59016 KB
import java.io.*; import java.util.*;
public class supertrees {

    static class Pair implements Comparable<Pair>{
        int v; int l0;
        public Pair(int a, int b){
            this.v=a; this.l0=b;
        }
        public int compareTo(Pair other){//Sort by l0 values
            if(this.l0>other.l0)return 1;
            if(this.l0<other.l0)return -1;
            if(this.v<other.v)return -1;
            if(this.v>other.v)return 1;
            return 0;
        }
    }
    /*
    public static void main(String[] args) throws IOException {
        // Tester code
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int N=Integer.parseInt(br.readLine());
        int[][] p=new int[N][N];
        for (int i = 0; i < N; i++) {
            StringTokenizer st=new StringTokenizer(br.readLine());
            for (int j = 0; j < N; j++) {
                p[i][j]=Integer.parseInt(st.nextToken());
            }
        }
        System.out.println(construct(p));
    }
*/
   public static int construct(int[][] p){
        int N=p.length;
        
        int[][] b=new int[N][N];
        int[] rank1=new int[N];//Consider the graph of 1-edges
        int[] rank0=new int[N];//Graph of 1,2-edges
        
        int[] par1=new int[N];
        int[] par0=new int[N];
        
        for (int i =0; i < N; i++) {
            par1[i]=i; par0[i]=i; 
        }
        int ii,jj;
        for (int i = 0; i < N; i++) {
            for (int j =0; j < N; j++) {
                if(i==j)continue;
                if(p[i][j]==1){
                    ii=find(i,par1); jj=find(j,par1);
                    if(ii!=jj){
                        if(rank1[ii]<rank1[jj]){
                            par1[ii]=jj;
                        }else if(rank1[ii]==rank1[jj]){
                            par1[ii]=jj; rank1[jj]++;
                        }else{
                            par1[jj]=ii;
                        }
                    }
                    ii=find(i,par0); jj=find(j,par0);
                    if(ii!=jj){
                        if(rank0[ii]<rank0[jj]){
                            par0[ii]=jj; 
                        }else if(rank0[ii]==rank0[jj]){
                            par0[ii]=jj; rank0[jj]++;
                        }else{
                            par0[jj]=ii;
                        }
                    }
                }else if(p[i][j]==2){
                    ii=find(i,par1); jj=find(j,par1);
                    if(ii==jj){//Deals with case i)
                        return 0;
                    }
                    
                    ii=find(i,par0); jj=find(j,par0);
                    if(ii!=jj){
                        if(rank0[ii]<rank0[jj]){
                            par0[ii]=jj;
                        }else if(rank0[ii]==rank0[jj]){
                            par0[ii]=jj; rank0[jj]++;
                        }else{
                            par0[jj]=ii;
                        }
                    }
                }else if(p[i][j]==3){
                    return 0;
                }else{
                    ii=find(i,par0); jj=find(j,par0);
                    if(ii==jj)return 0;
                }
            }
        }
        //Now for every 1-2 component, we need to find number of 1-leaders
        //That is the length of cycle. I lose if its length is 2 (length 1 is just a tree)
        boolean[] done=new boolean[N];
        int[] ans=new int[N];
        
        for (int i = 0; i < N; i++) {
            ii=find(i,par1);
            if(!done[ii]){
                jj=find(ii,par0);
                
               ans[jj]++;
                
                done[ii]=true;
            }
        }
         //System.out.println(Arrays.toString(ans));
        for (int i = 0; i < N; i++) {
            if(ans[i]==2){
                return 0;
            }
        }
        
        
        TreeSet<Pair> leads=new TreeSet<>();//Include leaders of all 1-components
        done=new boolean[N];
        for (int i = 0; i < N; i++) {//1-graphs
            
            ii=find(i,par1);
            if(ii!=i){
                b[i][ii]=1; b[ii][i]=1;
                
                //Same component guys don't matter
            }
            //I want to insert every root of each par1 component exactly once
            if(!done[ii]){
                leads.add(new Pair(ii,find(ii,par0)));
                
            }
            
        }
        
        int prevl=-1;
        //Completes a cycle for a component
        
        ArrayList<Integer> arl=new ArrayList<>();
        for (Pair q: leads) {
            //System.out.println(q.v+" "+q.l0+" "+prevv);
            //System.out.println(q.v+" "+q.l0);
            if(prevl!=q.l0){
                if(arl.size()>0){
                    for (int i = 0; i < arl.size()-1; i++) {
                        b[arl.get(i)][arl.get(i+1)]=1;
                        b[arl.get(i+1)][arl.get(i)]=1;
                    }
                    b[arl.get(0)][arl.get(arl.size()-1)]=1;
                    b[arl.get(arl.size()-1)][arl.get(0)]=1;
                    arl=new ArrayList<>();
                    
                }
            }
            arl.add(q.v);
            prevl=q.l0;
        }
       //  System.out.println(arl);
        if(arl.size()>0){
              for (int i = 0; i < arl.size()-1; i++) {
                   b[arl.get(i)][arl.get(i+1)]=1;
                   b[arl.get(i+1)][arl.get(i)]=1;
              }
              b[arl.get(0)][arl.get(arl.size()-1)]=1;
              b[arl.get(arl.size()-1)][arl.get(0)]=1;
                    
       }
        //Last fill
        //The leaders of every component forms a cycle as long as they're connected in the >=1 graph
        for (int i = 0; i < N; i++) {
           b[i][i]=0;
       }
        grader.build(b);
        
        
        return 1;
    }
    
    public static int find(int x, int[] par){
        if(par[x]==x)return x;
        int ans=find(par[x],par);
        par[x]=ans; return ans;
    }
}
# Verdict Execution time Memory Grader output
1 Correct 80 ms 10212 KB Output is correct
2 Correct 82 ms 10220 KB Output is correct
3 Correct 79 ms 10004 KB Output is correct
4 Correct 80 ms 10380 KB Output is correct
5 Correct 85 ms 10360 KB Output is correct
6 Correct 185 ms 14596 KB Output is correct
7 Correct 463 ms 57368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 10212 KB Output is correct
2 Correct 82 ms 10220 KB Output is correct
3 Correct 79 ms 10004 KB Output is correct
4 Correct 80 ms 10380 KB Output is correct
5 Correct 85 ms 10360 KB Output is correct
6 Correct 185 ms 14596 KB Output is correct
7 Correct 463 ms 57368 KB Output is correct
8 Correct 80 ms 10236 KB Output is correct
9 Correct 80 ms 10384 KB Output is correct
10 Correct 84 ms 10224 KB Output is correct
11 Correct 81 ms 10360 KB Output is correct
12 Correct 201 ms 15840 KB Output is correct
13 Correct 476 ms 58072 KB Output is correct
14 Correct 79 ms 10232 KB Output is correct
15 Correct 79 ms 10220 KB Output is correct
16 Correct 95 ms 11140 KB Output is correct
17 Correct 138 ms 19232 KB Output is correct
18 Correct 84 ms 10088 KB Output is correct
19 Correct 81 ms 10224 KB Output is correct
20 Correct 314 ms 28536 KB Output is correct
21 Correct 477 ms 57216 KB Output is correct
22 Correct 483 ms 57228 KB Output is correct
23 Correct 464 ms 57636 KB Output is correct
24 Correct 496 ms 57804 KB Output is correct
25 Correct 204 ms 20816 KB Output is correct
26 Correct 181 ms 20280 KB Output is correct
27 Correct 503 ms 57940 KB Output is correct
28 Correct 525 ms 57728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 79 ms 10396 KB Output is correct
2 Correct 83 ms 10300 KB Output is correct
3 Correct 83 ms 10356 KB Output is correct
4 Correct 81 ms 10340 KB Output is correct
5 Correct 80 ms 10472 KB Output is correct
6 Correct 80 ms 10232 KB Output is correct
7 Correct 82 ms 10360 KB Output is correct
8 Correct 163 ms 15976 KB Output is correct
9 Correct 508 ms 57884 KB Output is correct
10 Correct 81 ms 10344 KB Output is correct
11 Correct 84 ms 10424 KB Output is correct
12 Correct 203 ms 15980 KB Output is correct
13 Correct 494 ms 58612 KB Output is correct
14 Correct 82 ms 10340 KB Output is correct
15 Correct 81 ms 10084 KB Output is correct
16 Correct 99 ms 10960 KB Output is correct
17 Correct 133 ms 19192 KB Output is correct
18 Correct 81 ms 10100 KB Output is correct
19 Correct 80 ms 10356 KB Output is correct
20 Correct 79 ms 10360 KB Output is correct
21 Correct 334 ms 25668 KB Output is correct
22 Correct 491 ms 58448 KB Output is correct
23 Correct 505 ms 58492 KB Output is correct
24 Correct 501 ms 58916 KB Output is correct
25 Correct 194 ms 20820 KB Output is correct
26 Correct 147 ms 19084 KB Output is correct
27 Correct 512 ms 58752 KB Output is correct
28 Correct 500 ms 58320 KB Output is correct
29 Correct 188 ms 20956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 83 ms 10344 KB Output is correct
2 Correct 82 ms 10328 KB Output is correct
3 Correct 84 ms 10176 KB Output is correct
4 Correct 332 ms 30272 KB Output is correct
5 Correct 472 ms 57380 KB Output is correct
6 Correct 497 ms 59016 KB Output is correct
7 Correct 475 ms 57880 KB Output is correct
8 Correct 84 ms 10340 KB Output is correct
9 Correct 395 ms 30176 KB Output is correct
10 Correct 530 ms 58128 KB Output is correct
11 Correct 485 ms 58656 KB Output is correct
12 Correct 505 ms 57652 KB Output is correct
13 Correct 81 ms 10232 KB Output is correct
14 Correct 81 ms 10360 KB Output is correct
15 Correct 81 ms 10300 KB Output is correct
16 Correct 345 ms 30296 KB Output is correct
17 Correct 470 ms 57968 KB Output is correct
18 Correct 478 ms 57372 KB Output is correct
19 Correct 536 ms 58612 KB Output is correct
20 Correct 501 ms 57728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 10212 KB Output is correct
2 Correct 82 ms 10220 KB Output is correct
3 Correct 79 ms 10004 KB Output is correct
4 Correct 80 ms 10380 KB Output is correct
5 Correct 85 ms 10360 KB Output is correct
6 Correct 185 ms 14596 KB Output is correct
7 Correct 463 ms 57368 KB Output is correct
8 Correct 80 ms 10236 KB Output is correct
9 Correct 80 ms 10384 KB Output is correct
10 Correct 84 ms 10224 KB Output is correct
11 Correct 81 ms 10360 KB Output is correct
12 Correct 201 ms 15840 KB Output is correct
13 Correct 476 ms 58072 KB Output is correct
14 Correct 79 ms 10232 KB Output is correct
15 Correct 79 ms 10220 KB Output is correct
16 Correct 95 ms 11140 KB Output is correct
17 Correct 138 ms 19232 KB Output is correct
18 Correct 84 ms 10088 KB Output is correct
19 Correct 81 ms 10224 KB Output is correct
20 Correct 314 ms 28536 KB Output is correct
21 Correct 477 ms 57216 KB Output is correct
22 Correct 483 ms 57228 KB Output is correct
23 Correct 464 ms 57636 KB Output is correct
24 Correct 496 ms 57804 KB Output is correct
25 Correct 204 ms 20816 KB Output is correct
26 Correct 181 ms 20280 KB Output is correct
27 Correct 503 ms 57940 KB Output is correct
28 Correct 525 ms 57728 KB Output is correct
29 Correct 79 ms 10396 KB Output is correct
30 Correct 83 ms 10300 KB Output is correct
31 Correct 83 ms 10356 KB Output is correct
32 Correct 81 ms 10340 KB Output is correct
33 Correct 80 ms 10472 KB Output is correct
34 Correct 80 ms 10232 KB Output is correct
35 Correct 82 ms 10360 KB Output is correct
36 Correct 163 ms 15976 KB Output is correct
37 Correct 508 ms 57884 KB Output is correct
38 Correct 81 ms 10344 KB Output is correct
39 Correct 84 ms 10424 KB Output is correct
40 Correct 203 ms 15980 KB Output is correct
41 Correct 494 ms 58612 KB Output is correct
42 Correct 82 ms 10340 KB Output is correct
43 Correct 81 ms 10084 KB Output is correct
44 Correct 99 ms 10960 KB Output is correct
45 Correct 133 ms 19192 KB Output is correct
46 Correct 81 ms 10100 KB Output is correct
47 Correct 80 ms 10356 KB Output is correct
48 Correct 79 ms 10360 KB Output is correct
49 Correct 334 ms 25668 KB Output is correct
50 Correct 491 ms 58448 KB Output is correct
51 Correct 505 ms 58492 KB Output is correct
52 Correct 501 ms 58916 KB Output is correct
53 Correct 194 ms 20820 KB Output is correct
54 Correct 147 ms 19084 KB Output is correct
55 Correct 512 ms 58752 KB Output is correct
56 Correct 500 ms 58320 KB Output is correct
57 Correct 188 ms 20956 KB Output is correct
58 Correct 80 ms 10216 KB Output is correct
59 Correct 78 ms 10360 KB Output is correct
60 Correct 95 ms 10996 KB Output is correct
61 Correct 139 ms 19300 KB Output is correct
62 Correct 85 ms 10308 KB Output is correct
63 Correct 83 ms 10212 KB Output is correct
64 Correct 82 ms 10344 KB Output is correct
65 Correct 336 ms 25012 KB Output is correct
66 Correct 139 ms 19192 KB Output is correct
67 Correct 138 ms 19204 KB Output is correct
68 Correct 189 ms 20676 KB Output is correct
69 Correct 197 ms 21028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 10212 KB Output is correct
2 Correct 82 ms 10220 KB Output is correct
3 Correct 79 ms 10004 KB Output is correct
4 Correct 80 ms 10380 KB Output is correct
5 Correct 85 ms 10360 KB Output is correct
6 Correct 185 ms 14596 KB Output is correct
7 Correct 463 ms 57368 KB Output is correct
8 Correct 80 ms 10236 KB Output is correct
9 Correct 80 ms 10384 KB Output is correct
10 Correct 84 ms 10224 KB Output is correct
11 Correct 81 ms 10360 KB Output is correct
12 Correct 201 ms 15840 KB Output is correct
13 Correct 476 ms 58072 KB Output is correct
14 Correct 79 ms 10232 KB Output is correct
15 Correct 79 ms 10220 KB Output is correct
16 Correct 95 ms 11140 KB Output is correct
17 Correct 138 ms 19232 KB Output is correct
18 Correct 84 ms 10088 KB Output is correct
19 Correct 81 ms 10224 KB Output is correct
20 Correct 314 ms 28536 KB Output is correct
21 Correct 477 ms 57216 KB Output is correct
22 Correct 483 ms 57228 KB Output is correct
23 Correct 464 ms 57636 KB Output is correct
24 Correct 496 ms 57804 KB Output is correct
25 Correct 204 ms 20816 KB Output is correct
26 Correct 181 ms 20280 KB Output is correct
27 Correct 503 ms 57940 KB Output is correct
28 Correct 525 ms 57728 KB Output is correct
29 Correct 79 ms 10396 KB Output is correct
30 Correct 83 ms 10300 KB Output is correct
31 Correct 83 ms 10356 KB Output is correct
32 Correct 81 ms 10340 KB Output is correct
33 Correct 80 ms 10472 KB Output is correct
34 Correct 80 ms 10232 KB Output is correct
35 Correct 82 ms 10360 KB Output is correct
36 Correct 163 ms 15976 KB Output is correct
37 Correct 508 ms 57884 KB Output is correct
38 Correct 81 ms 10344 KB Output is correct
39 Correct 84 ms 10424 KB Output is correct
40 Correct 203 ms 15980 KB Output is correct
41 Correct 494 ms 58612 KB Output is correct
42 Correct 82 ms 10340 KB Output is correct
43 Correct 81 ms 10084 KB Output is correct
44 Correct 99 ms 10960 KB Output is correct
45 Correct 133 ms 19192 KB Output is correct
46 Correct 81 ms 10100 KB Output is correct
47 Correct 80 ms 10356 KB Output is correct
48 Correct 79 ms 10360 KB Output is correct
49 Correct 334 ms 25668 KB Output is correct
50 Correct 491 ms 58448 KB Output is correct
51 Correct 505 ms 58492 KB Output is correct
52 Correct 501 ms 58916 KB Output is correct
53 Correct 194 ms 20820 KB Output is correct
54 Correct 147 ms 19084 KB Output is correct
55 Correct 512 ms 58752 KB Output is correct
56 Correct 500 ms 58320 KB Output is correct
57 Correct 188 ms 20956 KB Output is correct
58 Correct 83 ms 10344 KB Output is correct
59 Correct 82 ms 10328 KB Output is correct
60 Correct 84 ms 10176 KB Output is correct
61 Correct 332 ms 30272 KB Output is correct
62 Correct 472 ms 57380 KB Output is correct
63 Correct 497 ms 59016 KB Output is correct
64 Correct 475 ms 57880 KB Output is correct
65 Correct 84 ms 10340 KB Output is correct
66 Correct 395 ms 30176 KB Output is correct
67 Correct 530 ms 58128 KB Output is correct
68 Correct 485 ms 58656 KB Output is correct
69 Correct 505 ms 57652 KB Output is correct
70 Correct 81 ms 10232 KB Output is correct
71 Correct 81 ms 10360 KB Output is correct
72 Correct 81 ms 10300 KB Output is correct
73 Correct 345 ms 30296 KB Output is correct
74 Correct 470 ms 57968 KB Output is correct
75 Correct 478 ms 57372 KB Output is correct
76 Correct 536 ms 58612 KB Output is correct
77 Correct 501 ms 57728 KB Output is correct
78 Correct 80 ms 10216 KB Output is correct
79 Correct 78 ms 10360 KB Output is correct
80 Correct 95 ms 10996 KB Output is correct
81 Correct 139 ms 19300 KB Output is correct
82 Correct 85 ms 10308 KB Output is correct
83 Correct 83 ms 10212 KB Output is correct
84 Correct 82 ms 10344 KB Output is correct
85 Correct 336 ms 25012 KB Output is correct
86 Correct 139 ms 19192 KB Output is correct
87 Correct 138 ms 19204 KB Output is correct
88 Correct 189 ms 20676 KB Output is correct
89 Correct 197 ms 21028 KB Output is correct
90 Correct 77 ms 10212 KB Output is correct
91 Correct 79 ms 10232 KB Output is correct
92 Correct 93 ms 11104 KB Output is correct
93 Correct 129 ms 18920 KB Output is correct
94 Correct 80 ms 10452 KB Output is correct
95 Correct 79 ms 10360 KB Output is correct
96 Correct 89 ms 10252 KB Output is correct
97 Correct 140 ms 13468 KB Output is correct
98 Correct 145 ms 19224 KB Output is correct
99 Correct 174 ms 20364 KB Output is correct
100 Correct 156 ms 20036 KB Output is correct
101 Correct 166 ms 20512 KB Output is correct
102 Correct 132 ms 19152 KB Output is correct
103 Correct 135 ms 19152 KB Output is correct
104 Correct 126 ms 19168 KB Output is correct
105 Correct 132 ms 18904 KB Output is correct