#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(),x.end()
#define endl "\n"
typedef long long ll;
struct DSU {
int n;
vector<int> link, sz;
DSU(int n) : n(n), link(n), sz(n, 1) {
iota(all(link), 0);
}
int find(int x) {
return link[x] == x ? x : link[x] = find(link[x]);
}
bool unite(int x, int y) {
x = find(x), y = find(y);
if (x == y) return false;
if (sz[x] < sz[y]) swap(x, y);
link[y] = x;
sz[x] += sz[y];
return true;
}
};
int construct(std::vector<std::vector<int>> p) {
int n = p.size();
vector<vector<int>> ans(n, vector<int>(n));
DSU dsu1(n), dsu2(n);
for (int i = 0; i < n; i++)
for (int j = i+1; j < n; j++)
if (p[i][j] == 1)
if (dsu1.unite(i, j))
ans[i][j] = 1, ans[j][i] = 1;
for (int i = 0; i < n; i++)
for (int j = i+1; j < n; j++)
if (p[i][j] == 2)
dsu2.unite(dsu1.find(i), dsu1.find(j));
for (int i = 0; i < n; i++) {
int p = -1;
int f = -1;
int c = 0;
for (int j = 0; j < n; j++)
if (dsu2.find(j) == i) {
c++;
if (f == -1)
f = j;
if (p != -1)
ans[j][p] = 1, ans[p][j] = 1;
p = j;
}
if (c == 2)
return 0;
if (f != p)
ans[f][p] = 1, ans[p][f] = 1;
}
for (int i = 0; i < n; i++)
for (int j = i+1; j < n; j++) {
if (p[i][j] == 0 and (dsu1.find(i) == dsu1.find(j) or dsu2.find(dsu1.find(i)) == dsu2.find(dsu1.find(j))))
return 0;
if (p[i][j] == 1 and dsu1.find(i) != dsu1.find(j))
return 0;
if (p[i][j] == 2 and (dsu1.find(i) == dsu1.find(j) or dsu2.find(dsu1.find(i)) != dsu2.find(dsu1.find(j))))
return 0;
if (p[i][j] == 3)
return 0;
}
build(ans);
return 1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
12 ms |
1128 KB |
Output is correct |
7 |
Correct |
248 ms |
22056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
12 ms |
1128 KB |
Output is correct |
7 |
Correct |
248 ms |
22056 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
11 ms |
1100 KB |
Output is correct |
13 |
Correct |
238 ms |
22056 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
5 ms |
716 KB |
Output is correct |
17 |
Correct |
129 ms |
12032 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
60 ms |
5760 KB |
Output is correct |
21 |
Correct |
256 ms |
22088 KB |
Output is correct |
22 |
Correct |
239 ms |
22028 KB |
Output is correct |
23 |
Correct |
253 ms |
21972 KB |
Output is correct |
24 |
Correct |
251 ms |
22052 KB |
Output is correct |
25 |
Correct |
105 ms |
12040 KB |
Output is correct |
26 |
Correct |
109 ms |
12040 KB |
Output is correct |
27 |
Correct |
262 ms |
21992 KB |
Output is correct |
28 |
Correct |
243 ms |
22044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
13 ms |
1200 KB |
Output is correct |
9 |
Correct |
240 ms |
23988 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
11 ms |
1168 KB |
Output is correct |
13 |
Correct |
251 ms |
23912 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
6 ms |
764 KB |
Output is correct |
17 |
Correct |
131 ms |
14060 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
292 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
60 ms |
6256 KB |
Output is correct |
22 |
Correct |
249 ms |
23972 KB |
Output is correct |
23 |
Correct |
247 ms |
23984 KB |
Output is correct |
24 |
Correct |
256 ms |
23928 KB |
Output is correct |
25 |
Correct |
107 ms |
14020 KB |
Output is correct |
26 |
Correct |
117 ms |
14076 KB |
Output is correct |
27 |
Correct |
242 ms |
23972 KB |
Output is correct |
28 |
Correct |
257 ms |
24004 KB |
Output is correct |
29 |
Correct |
101 ms |
14060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
61 ms |
5744 KB |
Output is correct |
5 |
Correct |
244 ms |
22020 KB |
Output is correct |
6 |
Correct |
249 ms |
22084 KB |
Output is correct |
7 |
Correct |
258 ms |
22028 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
59 ms |
5764 KB |
Output is correct |
10 |
Correct |
242 ms |
22024 KB |
Output is correct |
11 |
Correct |
291 ms |
22044 KB |
Output is correct |
12 |
Correct |
269 ms |
22212 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
60 ms |
5692 KB |
Output is correct |
17 |
Correct |
253 ms |
22020 KB |
Output is correct |
18 |
Correct |
255 ms |
21996 KB |
Output is correct |
19 |
Correct |
245 ms |
22084 KB |
Output is correct |
20 |
Correct |
238 ms |
22048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
12 ms |
1128 KB |
Output is correct |
7 |
Correct |
248 ms |
22056 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
11 ms |
1100 KB |
Output is correct |
13 |
Correct |
238 ms |
22056 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
5 ms |
716 KB |
Output is correct |
17 |
Correct |
129 ms |
12032 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
60 ms |
5760 KB |
Output is correct |
21 |
Correct |
256 ms |
22088 KB |
Output is correct |
22 |
Correct |
239 ms |
22028 KB |
Output is correct |
23 |
Correct |
253 ms |
21972 KB |
Output is correct |
24 |
Correct |
251 ms |
22052 KB |
Output is correct |
25 |
Correct |
105 ms |
12040 KB |
Output is correct |
26 |
Correct |
109 ms |
12040 KB |
Output is correct |
27 |
Correct |
262 ms |
21992 KB |
Output is correct |
28 |
Correct |
243 ms |
22044 KB |
Output is correct |
29 |
Correct |
1 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 |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
13 ms |
1200 KB |
Output is correct |
37 |
Correct |
240 ms |
23988 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
1 ms |
204 KB |
Output is correct |
40 |
Correct |
11 ms |
1168 KB |
Output is correct |
41 |
Correct |
251 ms |
23912 KB |
Output is correct |
42 |
Correct |
1 ms |
204 KB |
Output is correct |
43 |
Correct |
1 ms |
204 KB |
Output is correct |
44 |
Correct |
6 ms |
764 KB |
Output is correct |
45 |
Correct |
131 ms |
14060 KB |
Output is correct |
46 |
Correct |
1 ms |
204 KB |
Output is correct |
47 |
Correct |
1 ms |
292 KB |
Output is correct |
48 |
Correct |
1 ms |
204 KB |
Output is correct |
49 |
Correct |
60 ms |
6256 KB |
Output is correct |
50 |
Correct |
249 ms |
23972 KB |
Output is correct |
51 |
Correct |
247 ms |
23984 KB |
Output is correct |
52 |
Correct |
256 ms |
23928 KB |
Output is correct |
53 |
Correct |
107 ms |
14020 KB |
Output is correct |
54 |
Correct |
117 ms |
14076 KB |
Output is correct |
55 |
Correct |
242 ms |
23972 KB |
Output is correct |
56 |
Correct |
257 ms |
24004 KB |
Output is correct |
57 |
Correct |
101 ms |
14060 KB |
Output is correct |
58 |
Correct |
1 ms |
204 KB |
Output is correct |
59 |
Correct |
1 ms |
204 KB |
Output is correct |
60 |
Correct |
6 ms |
844 KB |
Output is correct |
61 |
Correct |
145 ms |
14064 KB |
Output is correct |
62 |
Correct |
1 ms |
204 KB |
Output is correct |
63 |
Correct |
1 ms |
204 KB |
Output is correct |
64 |
Correct |
1 ms |
296 KB |
Output is correct |
65 |
Correct |
66 ms |
6248 KB |
Output is correct |
66 |
Correct |
101 ms |
14072 KB |
Output is correct |
67 |
Correct |
106 ms |
14064 KB |
Output is correct |
68 |
Correct |
110 ms |
14048 KB |
Output is correct |
69 |
Correct |
112 ms |
14068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
12 ms |
1128 KB |
Output is correct |
7 |
Correct |
248 ms |
22056 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
11 ms |
1100 KB |
Output is correct |
13 |
Correct |
238 ms |
22056 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
5 ms |
716 KB |
Output is correct |
17 |
Correct |
129 ms |
12032 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
60 ms |
5760 KB |
Output is correct |
21 |
Correct |
256 ms |
22088 KB |
Output is correct |
22 |
Correct |
239 ms |
22028 KB |
Output is correct |
23 |
Correct |
253 ms |
21972 KB |
Output is correct |
24 |
Correct |
251 ms |
22052 KB |
Output is correct |
25 |
Correct |
105 ms |
12040 KB |
Output is correct |
26 |
Correct |
109 ms |
12040 KB |
Output is correct |
27 |
Correct |
262 ms |
21992 KB |
Output is correct |
28 |
Correct |
243 ms |
22044 KB |
Output is correct |
29 |
Correct |
1 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 |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
13 ms |
1200 KB |
Output is correct |
37 |
Correct |
240 ms |
23988 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
1 ms |
204 KB |
Output is correct |
40 |
Correct |
11 ms |
1168 KB |
Output is correct |
41 |
Correct |
251 ms |
23912 KB |
Output is correct |
42 |
Correct |
1 ms |
204 KB |
Output is correct |
43 |
Correct |
1 ms |
204 KB |
Output is correct |
44 |
Correct |
6 ms |
764 KB |
Output is correct |
45 |
Correct |
131 ms |
14060 KB |
Output is correct |
46 |
Correct |
1 ms |
204 KB |
Output is correct |
47 |
Correct |
1 ms |
292 KB |
Output is correct |
48 |
Correct |
1 ms |
204 KB |
Output is correct |
49 |
Correct |
60 ms |
6256 KB |
Output is correct |
50 |
Correct |
249 ms |
23972 KB |
Output is correct |
51 |
Correct |
247 ms |
23984 KB |
Output is correct |
52 |
Correct |
256 ms |
23928 KB |
Output is correct |
53 |
Correct |
107 ms |
14020 KB |
Output is correct |
54 |
Correct |
117 ms |
14076 KB |
Output is correct |
55 |
Correct |
242 ms |
23972 KB |
Output is correct |
56 |
Correct |
257 ms |
24004 KB |
Output is correct |
57 |
Correct |
101 ms |
14060 KB |
Output is correct |
58 |
Correct |
1 ms |
204 KB |
Output is correct |
59 |
Correct |
1 ms |
204 KB |
Output is correct |
60 |
Correct |
1 ms |
204 KB |
Output is correct |
61 |
Correct |
61 ms |
5744 KB |
Output is correct |
62 |
Correct |
244 ms |
22020 KB |
Output is correct |
63 |
Correct |
249 ms |
22084 KB |
Output is correct |
64 |
Correct |
258 ms |
22028 KB |
Output is correct |
65 |
Correct |
1 ms |
204 KB |
Output is correct |
66 |
Correct |
59 ms |
5764 KB |
Output is correct |
67 |
Correct |
242 ms |
22024 KB |
Output is correct |
68 |
Correct |
291 ms |
22044 KB |
Output is correct |
69 |
Correct |
269 ms |
22212 KB |
Output is correct |
70 |
Correct |
1 ms |
204 KB |
Output is correct |
71 |
Correct |
1 ms |
204 KB |
Output is correct |
72 |
Correct |
1 ms |
204 KB |
Output is correct |
73 |
Correct |
60 ms |
5692 KB |
Output is correct |
74 |
Correct |
253 ms |
22020 KB |
Output is correct |
75 |
Correct |
255 ms |
21996 KB |
Output is correct |
76 |
Correct |
245 ms |
22084 KB |
Output is correct |
77 |
Correct |
238 ms |
22048 KB |
Output is correct |
78 |
Correct |
1 ms |
204 KB |
Output is correct |
79 |
Correct |
1 ms |
204 KB |
Output is correct |
80 |
Correct |
6 ms |
844 KB |
Output is correct |
81 |
Correct |
145 ms |
14064 KB |
Output is correct |
82 |
Correct |
1 ms |
204 KB |
Output is correct |
83 |
Correct |
1 ms |
204 KB |
Output is correct |
84 |
Correct |
1 ms |
296 KB |
Output is correct |
85 |
Correct |
66 ms |
6248 KB |
Output is correct |
86 |
Correct |
101 ms |
14072 KB |
Output is correct |
87 |
Correct |
106 ms |
14064 KB |
Output is correct |
88 |
Correct |
110 ms |
14048 KB |
Output is correct |
89 |
Correct |
112 ms |
14068 KB |
Output is correct |
90 |
Correct |
1 ms |
204 KB |
Output is correct |
91 |
Correct |
1 ms |
204 KB |
Output is correct |
92 |
Correct |
7 ms |
796 KB |
Output is correct |
93 |
Correct |
146 ms |
14072 KB |
Output is correct |
94 |
Correct |
1 ms |
204 KB |
Output is correct |
95 |
Correct |
1 ms |
204 KB |
Output is correct |
96 |
Correct |
1 ms |
204 KB |
Output is correct |
97 |
Correct |
27 ms |
3696 KB |
Output is correct |
98 |
Correct |
120 ms |
14020 KB |
Output is correct |
99 |
Correct |
119 ms |
14020 KB |
Output is correct |
100 |
Correct |
111 ms |
14060 KB |
Output is correct |
101 |
Correct |
123 ms |
14064 KB |
Output is correct |
102 |
Correct |
110 ms |
13984 KB |
Output is correct |
103 |
Correct |
109 ms |
14204 KB |
Output is correct |
104 |
Correct |
118 ms |
14024 KB |
Output is correct |
105 |
Correct |
147 ms |
14068 KB |
Output is correct |