#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
vector<int>parent;
int UF(int u) {
if(parent[u] == u)
return u;
return parent[u] = UF(parent[u]);
}
int construct(vector<vector<int>> p) {
const int n = (int)p.size();
parent.resize(n);
vector<vector<int>>ans(n,vector<int>(n,0));
for(int i = 0 ; i < n ; i++)
parent[i] = i;
for(int i = 0 ; i < n ; i++) {
for(int j = i+1 ; j < n ; j++) {
if(p[i][j] == 3) {
return 0; // invalid
}
assert(p[i][j] == p[j][i]);
if(p[i][j] == 1) {
int u = UF(i), v = UF(j);
if(u != v) {
ans[i][j] = ans[j][i] = 1;
parent[u] = v;
}
}
}
}
for(int i = 0 ; i < n ; i++) {
for(int j = i+1 ; j < n ; j++) {
if((((p[i][j] == 0 || p[i][j] == 2) && UF(i) == UF(j))) || (p[i][j] == 1 && UF(i) != UF(j))) {
return 0;
}
}
}
vector<int>remember(n,-1);
vector<int>from(n);
vector<int>sz(n,1);
for(int i = 0 ; i < n ; i++)
from[i]=i;
for(int i = 0 ; i < n ; i++) {
for(int j = i+1 ; j < n ; j++) {
if(p[i][j] == 2) {
int root_a = UF(i), root_b = UF(j);
if(root_a == root_b)
continue;
vector<int>a,b;
for(int k = 0 ; k < n ; k++) {
if(UF(k) == root_a)
a.push_back(k);
else if(UF(k) == root_b)
b.push_back(k);
}
for(auto aa : a)
for(auto bb : b)
if(p[aa][bb] != 2)
return 0;
parent[root_a] = root_b;
ans[root_a][from[root_b]] = 1;
ans[from[root_b]][root_a] = 1;
remember[root_a] = -1;
remember[root_b] = from[root_a];
from[root_b] = remember[root_b];
sz[root_b]+=sz[root_a];
sz[root_a] = 0;
}
}
}
for(int i = 0 ; i < n ; i++) {
if(remember[i] != -1) {
ans[i][remember[i]] = 1;
ans[remember[i]][i] = 1;
}
if(sz[i] == 2) {
return 0;
}
}
build(ans);
return 1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
7 ms |
1228 KB |
Output is correct |
7 |
Correct |
184 ms |
24056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
7 ms |
1228 KB |
Output is correct |
7 |
Correct |
184 ms |
24056 KB |
Output is correct |
8 |
Correct |
1 ms |
292 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
7 ms |
1228 KB |
Output is correct |
13 |
Correct |
163 ms |
23912 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
3 ms |
844 KB |
Output is correct |
17 |
Correct |
77 ms |
14072 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
44 ms |
6244 KB |
Output is correct |
21 |
Correct |
167 ms |
24060 KB |
Output is correct |
22 |
Correct |
164 ms |
23944 KB |
Output is correct |
23 |
Correct |
177 ms |
23924 KB |
Output is correct |
24 |
Correct |
165 ms |
23932 KB |
Output is correct |
25 |
Correct |
68 ms |
14056 KB |
Output is correct |
26 |
Correct |
65 ms |
14020 KB |
Output is correct |
27 |
Correct |
174 ms |
23924 KB |
Output is correct |
28 |
Correct |
182 ms |
23924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
9 ms |
1228 KB |
Output is correct |
9 |
Correct |
166 ms |
23912 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
292 KB |
Output is correct |
12 |
Correct |
8 ms |
1228 KB |
Output is correct |
13 |
Correct |
176 ms |
23992 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
4 ms |
844 KB |
Output is correct |
17 |
Correct |
75 ms |
14072 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
44 ms |
6212 KB |
Output is correct |
22 |
Correct |
179 ms |
24004 KB |
Output is correct |
23 |
Correct |
166 ms |
23932 KB |
Output is correct |
24 |
Correct |
183 ms |
24004 KB |
Output is correct |
25 |
Correct |
69 ms |
14020 KB |
Output is correct |
26 |
Correct |
80 ms |
14068 KB |
Output is correct |
27 |
Correct |
167 ms |
24004 KB |
Output is correct |
28 |
Correct |
191 ms |
24180 KB |
Output is correct |
29 |
Correct |
68 ms |
14068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
45 ms |
6252 KB |
Output is correct |
5 |
Correct |
178 ms |
24048 KB |
Output is correct |
6 |
Correct |
165 ms |
23916 KB |
Output is correct |
7 |
Correct |
179 ms |
24056 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
43 ms |
6252 KB |
Output is correct |
10 |
Correct |
177 ms |
23964 KB |
Output is correct |
11 |
Correct |
174 ms |
24004 KB |
Output is correct |
12 |
Correct |
184 ms |
23924 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
47 ms |
6204 KB |
Output is correct |
17 |
Correct |
173 ms |
23924 KB |
Output is correct |
18 |
Correct |
170 ms |
23988 KB |
Output is correct |
19 |
Correct |
166 ms |
23928 KB |
Output is correct |
20 |
Correct |
158 ms |
23916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
7 ms |
1228 KB |
Output is correct |
7 |
Correct |
184 ms |
24056 KB |
Output is correct |
8 |
Correct |
1 ms |
292 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
7 ms |
1228 KB |
Output is correct |
13 |
Correct |
163 ms |
23912 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
3 ms |
844 KB |
Output is correct |
17 |
Correct |
77 ms |
14072 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
44 ms |
6244 KB |
Output is correct |
21 |
Correct |
167 ms |
24060 KB |
Output is correct |
22 |
Correct |
164 ms |
23944 KB |
Output is correct |
23 |
Correct |
177 ms |
23924 KB |
Output is correct |
24 |
Correct |
165 ms |
23932 KB |
Output is correct |
25 |
Correct |
68 ms |
14056 KB |
Output is correct |
26 |
Correct |
65 ms |
14020 KB |
Output is correct |
27 |
Correct |
174 ms |
23924 KB |
Output is correct |
28 |
Correct |
182 ms |
23924 KB |
Output is correct |
29 |
Correct |
0 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
0 ms |
204 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
0 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
9 ms |
1228 KB |
Output is correct |
37 |
Correct |
166 ms |
23912 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
0 ms |
292 KB |
Output is correct |
40 |
Correct |
8 ms |
1228 KB |
Output is correct |
41 |
Correct |
176 ms |
23992 KB |
Output is correct |
42 |
Correct |
0 ms |
204 KB |
Output is correct |
43 |
Correct |
0 ms |
204 KB |
Output is correct |
44 |
Correct |
4 ms |
844 KB |
Output is correct |
45 |
Correct |
75 ms |
14072 KB |
Output is correct |
46 |
Correct |
0 ms |
204 KB |
Output is correct |
47 |
Correct |
0 ms |
204 KB |
Output is correct |
48 |
Correct |
0 ms |
204 KB |
Output is correct |
49 |
Correct |
44 ms |
6212 KB |
Output is correct |
50 |
Correct |
179 ms |
24004 KB |
Output is correct |
51 |
Correct |
166 ms |
23932 KB |
Output is correct |
52 |
Correct |
183 ms |
24004 KB |
Output is correct |
53 |
Correct |
69 ms |
14020 KB |
Output is correct |
54 |
Correct |
80 ms |
14068 KB |
Output is correct |
55 |
Correct |
167 ms |
24004 KB |
Output is correct |
56 |
Correct |
191 ms |
24180 KB |
Output is correct |
57 |
Correct |
68 ms |
14068 KB |
Output is correct |
58 |
Correct |
0 ms |
204 KB |
Output is correct |
59 |
Correct |
0 ms |
204 KB |
Output is correct |
60 |
Correct |
3 ms |
816 KB |
Output is correct |
61 |
Correct |
74 ms |
14068 KB |
Output is correct |
62 |
Correct |
0 ms |
292 KB |
Output is correct |
63 |
Correct |
0 ms |
204 KB |
Output is correct |
64 |
Correct |
1 ms |
204 KB |
Output is correct |
65 |
Correct |
43 ms |
6188 KB |
Output is correct |
66 |
Correct |
71 ms |
14020 KB |
Output is correct |
67 |
Correct |
66 ms |
14020 KB |
Output is correct |
68 |
Correct |
74 ms |
14148 KB |
Output is correct |
69 |
Correct |
67 ms |
14024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
7 ms |
1228 KB |
Output is correct |
7 |
Correct |
184 ms |
24056 KB |
Output is correct |
8 |
Correct |
1 ms |
292 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
7 ms |
1228 KB |
Output is correct |
13 |
Correct |
163 ms |
23912 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
3 ms |
844 KB |
Output is correct |
17 |
Correct |
77 ms |
14072 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
44 ms |
6244 KB |
Output is correct |
21 |
Correct |
167 ms |
24060 KB |
Output is correct |
22 |
Correct |
164 ms |
23944 KB |
Output is correct |
23 |
Correct |
177 ms |
23924 KB |
Output is correct |
24 |
Correct |
165 ms |
23932 KB |
Output is correct |
25 |
Correct |
68 ms |
14056 KB |
Output is correct |
26 |
Correct |
65 ms |
14020 KB |
Output is correct |
27 |
Correct |
174 ms |
23924 KB |
Output is correct |
28 |
Correct |
182 ms |
23924 KB |
Output is correct |
29 |
Correct |
0 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
0 ms |
204 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
0 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
9 ms |
1228 KB |
Output is correct |
37 |
Correct |
166 ms |
23912 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
0 ms |
292 KB |
Output is correct |
40 |
Correct |
8 ms |
1228 KB |
Output is correct |
41 |
Correct |
176 ms |
23992 KB |
Output is correct |
42 |
Correct |
0 ms |
204 KB |
Output is correct |
43 |
Correct |
0 ms |
204 KB |
Output is correct |
44 |
Correct |
4 ms |
844 KB |
Output is correct |
45 |
Correct |
75 ms |
14072 KB |
Output is correct |
46 |
Correct |
0 ms |
204 KB |
Output is correct |
47 |
Correct |
0 ms |
204 KB |
Output is correct |
48 |
Correct |
0 ms |
204 KB |
Output is correct |
49 |
Correct |
44 ms |
6212 KB |
Output is correct |
50 |
Correct |
179 ms |
24004 KB |
Output is correct |
51 |
Correct |
166 ms |
23932 KB |
Output is correct |
52 |
Correct |
183 ms |
24004 KB |
Output is correct |
53 |
Correct |
69 ms |
14020 KB |
Output is correct |
54 |
Correct |
80 ms |
14068 KB |
Output is correct |
55 |
Correct |
167 ms |
24004 KB |
Output is correct |
56 |
Correct |
191 ms |
24180 KB |
Output is correct |
57 |
Correct |
68 ms |
14068 KB |
Output is correct |
58 |
Correct |
0 ms |
204 KB |
Output is correct |
59 |
Correct |
0 ms |
204 KB |
Output is correct |
60 |
Correct |
1 ms |
204 KB |
Output is correct |
61 |
Correct |
45 ms |
6252 KB |
Output is correct |
62 |
Correct |
178 ms |
24048 KB |
Output is correct |
63 |
Correct |
165 ms |
23916 KB |
Output is correct |
64 |
Correct |
179 ms |
24056 KB |
Output is correct |
65 |
Correct |
0 ms |
204 KB |
Output is correct |
66 |
Correct |
43 ms |
6252 KB |
Output is correct |
67 |
Correct |
177 ms |
23964 KB |
Output is correct |
68 |
Correct |
174 ms |
24004 KB |
Output is correct |
69 |
Correct |
184 ms |
23924 KB |
Output is correct |
70 |
Correct |
0 ms |
204 KB |
Output is correct |
71 |
Correct |
1 ms |
204 KB |
Output is correct |
72 |
Correct |
0 ms |
204 KB |
Output is correct |
73 |
Correct |
47 ms |
6204 KB |
Output is correct |
74 |
Correct |
173 ms |
23924 KB |
Output is correct |
75 |
Correct |
170 ms |
23988 KB |
Output is correct |
76 |
Correct |
166 ms |
23928 KB |
Output is correct |
77 |
Correct |
158 ms |
23916 KB |
Output is correct |
78 |
Correct |
0 ms |
204 KB |
Output is correct |
79 |
Correct |
0 ms |
204 KB |
Output is correct |
80 |
Correct |
3 ms |
816 KB |
Output is correct |
81 |
Correct |
74 ms |
14068 KB |
Output is correct |
82 |
Correct |
0 ms |
292 KB |
Output is correct |
83 |
Correct |
0 ms |
204 KB |
Output is correct |
84 |
Correct |
1 ms |
204 KB |
Output is correct |
85 |
Correct |
43 ms |
6188 KB |
Output is correct |
86 |
Correct |
71 ms |
14020 KB |
Output is correct |
87 |
Correct |
66 ms |
14020 KB |
Output is correct |
88 |
Correct |
74 ms |
14148 KB |
Output is correct |
89 |
Correct |
67 ms |
14024 KB |
Output is correct |
90 |
Correct |
1 ms |
204 KB |
Output is correct |
91 |
Correct |
0 ms |
204 KB |
Output is correct |
92 |
Correct |
3 ms |
844 KB |
Output is correct |
93 |
Correct |
68 ms |
14064 KB |
Output is correct |
94 |
Correct |
1 ms |
204 KB |
Output is correct |
95 |
Correct |
0 ms |
204 KB |
Output is correct |
96 |
Correct |
0 ms |
204 KB |
Output is correct |
97 |
Correct |
16 ms |
3660 KB |
Output is correct |
98 |
Correct |
71 ms |
14060 KB |
Output is correct |
99 |
Correct |
66 ms |
14056 KB |
Output is correct |
100 |
Correct |
64 ms |
14068 KB |
Output is correct |
101 |
Correct |
66 ms |
14028 KB |
Output is correct |
102 |
Correct |
65 ms |
14072 KB |
Output is correct |
103 |
Correct |
65 ms |
14072 KB |
Output is correct |
104 |
Correct |
64 ms |
14076 KB |
Output is correct |
105 |
Correct |
73 ms |
14072 KB |
Output is correct |