Submission #858284

# Submission time Handle Problem Language Result Execution time Memory
858284 2023-10-07T23:14:28 Z _uros9 Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
234 ms 24336 KB
#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;

int n;
int N=1e3+9;
vector<int> rnk(N,0),par(N),rnk1(N,0),par1(N);
int ulp(int node){
    if(par[node]==node) return node;
    return par[node]=ulp(par[node]);
}
void spajaj(int a,int b){
    a=ulp(a);b=ulp(b);
    if(a==b) return;
    if(rnk[b]>rnk[a])
        swap(a,b);
    par[b]=a;
    if(rnk[a]==rnk[b])
        rnk[a]++;
}
int ulp1(int node){
    if(par1[node]==node) return node;
    return par1[node]=ulp1(par1[node]);
}
void spajaj1(int a,int b){
    a=ulp1(a);b=ulp1(b);
    if(a==b) return;
    if(rnk1[b]>rnk1[a])
        swap(a,b);
    par1[b]=a;
    if(rnk1[a]==rnk1[b])
        rnk1[a]++;
}

int construct(std::vector<std::vector<int>> p){
	n=p.size();
	std::vector<std::vector<int>> answer;
	for (int i = 0; i < n; i++) {
		std::vector<int> row;
		row.resize(n);
		answer.push_back(row);
	}
	vector<set<int>> lanci(n);
	for(int i=0; i<n; i++) par[i]=i;
	for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            if(p[i][j]==1)
                spajaj(i,j);
            if(p[i][j]==3) return 0;
        }
	}
	for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            if(p[i][j]==1){
                lanci[ulp(i)].insert(j);
                lanci[ulp(i)].insert(i);
            }
            if(ulp(i)==ulp(j)&&p[i][j]!=1)
                return 0;
        }
	}
	for(int i=0; i<n; i++){
        if(lanci[i].size()<=1) continue;
        for(auto it=next(lanci[i].begin()); it!=lanci[i].end(); it++){
            answer[*prev(it)][*it]=1;
            answer[*it][*prev(it)]=1;
        }
	}
	for(int i=0; i<n; i++) par1[ulp(i)]=ulp(i);
	for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            if(p[i][j]==2)
                spajaj1(ulp(i),ulp(j));
        }
	}
	vector<set<int>> adj(n);
	for(int i=0; i<n; i++)
        adj[ulp1(ulp(i))].insert(ulp(i));
	for(int i=0; i<n; i++){
        if(adj[i].size()<=1) continue;
        if(adj[i].size()==2) return 0;
        for(auto it=next(adj[i].begin()); it!=adj[i].end(); it++){
            answer[*prev(it)][*it]=1;
            answer[*it][*prev(it)]=1;
        }
        answer[*prev(adj[i].end())][*adj[i].begin()]=1;
        answer[*adj[i].begin()][*prev(adj[i].end())]=1;
	}
	for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            if(p[i][j]==0){
                if(ulp(i)==ulp(j)) return 0;
                if(ulp1(ulp(i))==ulp1(ulp(j))) return 0;
            }
            if(p[i][j]==1)
                if(ulp(i)!=ulp(j)) return 0;
            if(p[i][j]==2){
                if(ulp(i)==ulp(j)) return 0;
                if(ulp1(ulp(i))!=ulp1(ulp(j))) return 0;
            }
        }
	}
	build(answer);
	return 1;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 456 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 9 ms 1868 KB Output is correct
7 Correct 217 ms 23376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 456 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 9 ms 1868 KB Output is correct
7 Correct 217 ms 23376 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 7 ms 1460 KB Output is correct
13 Correct 174 ms 23320 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 944 KB Output is correct
17 Correct 76 ms 13332 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 456 KB Output is correct
20 Correct 43 ms 6432 KB Output is correct
21 Correct 185 ms 23892 KB Output is correct
22 Correct 168 ms 23948 KB Output is correct
23 Correct 207 ms 23892 KB Output is correct
24 Correct 167 ms 23940 KB Output is correct
25 Correct 62 ms 13968 KB Output is correct
26 Correct 60 ms 14092 KB Output is correct
27 Correct 218 ms 24148 KB Output is correct
28 Correct 166 ms 24144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 7 ms 1424 KB Output is correct
9 Correct 165 ms 24216 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 8 ms 1456 KB Output is correct
13 Correct 181 ms 24236 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 4 ms 860 KB Output is correct
17 Correct 85 ms 14420 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 448 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 43 ms 6280 KB Output is correct
22 Correct 170 ms 24216 KB Output is correct
23 Correct 168 ms 24292 KB Output is correct
24 Correct 200 ms 24336 KB Output is correct
25 Correct 64 ms 14416 KB Output is correct
26 Correct 68 ms 14420 KB Output is correct
27 Correct 170 ms 24144 KB Output is correct
28 Correct 188 ms 24328 KB Output is correct
29 Correct 68 ms 14348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 43 ms 6280 KB Output is correct
5 Correct 188 ms 24320 KB Output is correct
6 Correct 172 ms 24100 KB Output is correct
7 Correct 234 ms 24228 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 44 ms 6476 KB Output is correct
10 Correct 174 ms 23960 KB Output is correct
11 Correct 168 ms 24144 KB Output is correct
12 Correct 191 ms 24076 KB Output is correct
13 Correct 1 ms 500 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 600 KB Output is correct
16 Correct 44 ms 6320 KB Output is correct
17 Correct 172 ms 24148 KB Output is correct
18 Correct 175 ms 24144 KB Output is correct
19 Correct 174 ms 24068 KB Output is correct
20 Correct 163 ms 24144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 456 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 9 ms 1868 KB Output is correct
7 Correct 217 ms 23376 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 7 ms 1460 KB Output is correct
13 Correct 174 ms 23320 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 944 KB Output is correct
17 Correct 76 ms 13332 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 456 KB Output is correct
20 Correct 43 ms 6432 KB Output is correct
21 Correct 185 ms 23892 KB Output is correct
22 Correct 168 ms 23948 KB Output is correct
23 Correct 207 ms 23892 KB Output is correct
24 Correct 167 ms 23940 KB Output is correct
25 Correct 62 ms 13968 KB Output is correct
26 Correct 60 ms 14092 KB Output is correct
27 Correct 218 ms 24148 KB Output is correct
28 Correct 166 ms 24144 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 0 ms 344 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 7 ms 1424 KB Output is correct
37 Correct 165 ms 24216 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 8 ms 1456 KB Output is correct
41 Correct 181 ms 24236 KB Output is correct
42 Correct 1 ms 600 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 4 ms 860 KB Output is correct
45 Correct 85 ms 14420 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 0 ms 448 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 43 ms 6280 KB Output is correct
50 Correct 170 ms 24216 KB Output is correct
51 Correct 168 ms 24292 KB Output is correct
52 Correct 200 ms 24336 KB Output is correct
53 Correct 64 ms 14416 KB Output is correct
54 Correct 68 ms 14420 KB Output is correct
55 Correct 170 ms 24144 KB Output is correct
56 Correct 188 ms 24328 KB Output is correct
57 Correct 68 ms 14348 KB Output is correct
58 Correct 1 ms 604 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 3 ms 860 KB Output is correct
61 Correct 74 ms 14216 KB Output is correct
62 Correct 0 ms 348 KB Output is correct
63 Correct 0 ms 348 KB Output is correct
64 Correct 0 ms 348 KB Output is correct
65 Correct 44 ms 6348 KB Output is correct
66 Correct 71 ms 14268 KB Output is correct
67 Correct 63 ms 14164 KB Output is correct
68 Correct 67 ms 14160 KB Output is correct
69 Correct 64 ms 14420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 456 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 9 ms 1868 KB Output is correct
7 Correct 217 ms 23376 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 7 ms 1460 KB Output is correct
13 Correct 174 ms 23320 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 944 KB Output is correct
17 Correct 76 ms 13332 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 456 KB Output is correct
20 Correct 43 ms 6432 KB Output is correct
21 Correct 185 ms 23892 KB Output is correct
22 Correct 168 ms 23948 KB Output is correct
23 Correct 207 ms 23892 KB Output is correct
24 Correct 167 ms 23940 KB Output is correct
25 Correct 62 ms 13968 KB Output is correct
26 Correct 60 ms 14092 KB Output is correct
27 Correct 218 ms 24148 KB Output is correct
28 Correct 166 ms 24144 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 0 ms 344 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 7 ms 1424 KB Output is correct
37 Correct 165 ms 24216 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 8 ms 1456 KB Output is correct
41 Correct 181 ms 24236 KB Output is correct
42 Correct 1 ms 600 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 4 ms 860 KB Output is correct
45 Correct 85 ms 14420 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 0 ms 448 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 43 ms 6280 KB Output is correct
50 Correct 170 ms 24216 KB Output is correct
51 Correct 168 ms 24292 KB Output is correct
52 Correct 200 ms 24336 KB Output is correct
53 Correct 64 ms 14416 KB Output is correct
54 Correct 68 ms 14420 KB Output is correct
55 Correct 170 ms 24144 KB Output is correct
56 Correct 188 ms 24328 KB Output is correct
57 Correct 68 ms 14348 KB Output is correct
58 Correct 0 ms 348 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 0 ms 344 KB Output is correct
61 Correct 43 ms 6280 KB Output is correct
62 Correct 188 ms 24320 KB Output is correct
63 Correct 172 ms 24100 KB Output is correct
64 Correct 234 ms 24228 KB Output is correct
65 Correct 1 ms 348 KB Output is correct
66 Correct 44 ms 6476 KB Output is correct
67 Correct 174 ms 23960 KB Output is correct
68 Correct 168 ms 24144 KB Output is correct
69 Correct 191 ms 24076 KB Output is correct
70 Correct 1 ms 500 KB Output is correct
71 Correct 0 ms 348 KB Output is correct
72 Correct 0 ms 600 KB Output is correct
73 Correct 44 ms 6320 KB Output is correct
74 Correct 172 ms 24148 KB Output is correct
75 Correct 175 ms 24144 KB Output is correct
76 Correct 174 ms 24068 KB Output is correct
77 Correct 163 ms 24144 KB Output is correct
78 Correct 1 ms 604 KB Output is correct
79 Correct 0 ms 348 KB Output is correct
80 Correct 3 ms 860 KB Output is correct
81 Correct 74 ms 14216 KB Output is correct
82 Correct 0 ms 348 KB Output is correct
83 Correct 0 ms 348 KB Output is correct
84 Correct 0 ms 348 KB Output is correct
85 Correct 44 ms 6348 KB Output is correct
86 Correct 71 ms 14268 KB Output is correct
87 Correct 63 ms 14164 KB Output is correct
88 Correct 67 ms 14160 KB Output is correct
89 Correct 64 ms 14420 KB Output is correct
90 Correct 0 ms 600 KB Output is correct
91 Correct 0 ms 348 KB Output is correct
92 Correct 3 ms 860 KB Output is correct
93 Correct 71 ms 14424 KB Output is correct
94 Correct 0 ms 344 KB Output is correct
95 Correct 0 ms 348 KB Output is correct
96 Correct 0 ms 348 KB Output is correct
97 Correct 15 ms 3860 KB Output is correct
98 Correct 62 ms 14164 KB Output is correct
99 Correct 62 ms 14092 KB Output is correct
100 Correct 59 ms 14160 KB Output is correct
101 Correct 58 ms 14164 KB Output is correct
102 Correct 64 ms 14232 KB Output is correct
103 Correct 62 ms 14160 KB Output is correct
104 Correct 64 ms 14164 KB Output is correct
105 Correct 70 ms 14224 KB Output is correct