#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); ++i)
int construct_forest(const vector<vector<int>>& p) {
int n{int(p.size())};
vector<bool> vis(n);
rep(i, n) if(!vis[i])
rep(j, n)
if(p[i][j]) {
if(p[i] != p[j])
return 0;
vis[j] = true;
}
return 1;
}
vector<vector<int>> build_forest(const vector<vector<int>>& p) {
int n{int(p.size())};
vector<bool> vis(n);
vector<vector<int>> adj(n, vector<int>(n));
rep(i, n) if(!vis[i])
rep(j, n) if(p[i][j] && i != j)
adj[i][j] = adj[j][i] = 1, vis[j] = true;
return adj;
}
int add_cycles(const vector<vector<int>>& p, vector<vector<int>>& adj) {
int n{int(p.size())};
vector<bool> vis(n);
vector<int> tree_head;
vector<int> tree_of(n);
int t{};
rep(i, n) if(!vis[i]) {
tree_head.push_back(i);
rep(j, n) if(p[i][j] == 1)
tree_of[j] = t, vis[j] = true;
++t;
}
rep(i, n) rep(j, n)
if(p[i][j] == 1) assert(tree_of[i] == tree_of[j]);
else assert(tree_of[i] != tree_of[j]);
rep(i, n) rep(j, n)
if(p[i][j] == 2 && p[tree_head[tree_of[i]]][j] != 2)
return 0;
else if(p[tree_head[tree_of[i]]][j] == 2 && p[i][j] != 2)
return 0;
fill(vis.begin(), vis.end(), false);
vector<vector<int>> tree_comp;
for(int i: tree_head) if(!vis[i]) {
tree_comp.emplace_back();
for(int j: tree_head) if(p[i][j] != 0) {
rep(k, n) {
if(p[i][k] == 2 && p[k][j] == 0)
return 0;
else if(p[i][k] == 0 && p[k][j] == 2)
return 0;
vis[j] = true;
}
tree_comp.back().push_back(j);
}
}
for(vector<int>& comp: tree_comp) {
if(int(comp.size()) == 2)
return 0;
rep(i, int(comp.size())) {
int j = comp[(i + 1) % int(comp.size())];
if(comp[i] != j)
adj[comp[i]][j] = adj[j][comp[i]] = 1;
}
}
return 1;
}
int construct(vector<vector<int>> p) {
for(auto& x: p) for(auto& y: x) if(y == 3) return 0;
int n{int(p.size())};
vector<vector<int>> p2{p};
rep(i, n) rep(j, n) p2[i][j] %= 2;
if(!construct_forest(p2))
return 0;
vector<vector<int>> adj{build_forest(p2)};
if(!add_cycles(p, adj))
return 0;
build(adj);
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 |
288 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
10 ms |
1356 KB |
Output is correct |
7 |
Correct |
224 ms |
27896 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 |
288 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
10 ms |
1356 KB |
Output is correct |
7 |
Correct |
224 ms |
27896 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 |
10 ms |
1388 KB |
Output is correct |
13 |
Correct |
234 ms |
27900 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 |
844 KB |
Output is correct |
17 |
Correct |
121 ms |
14076 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
58 ms |
7144 KB |
Output is correct |
21 |
Correct |
227 ms |
27844 KB |
Output is correct |
22 |
Correct |
228 ms |
27844 KB |
Output is correct |
23 |
Correct |
234 ms |
27972 KB |
Output is correct |
24 |
Correct |
229 ms |
27972 KB |
Output is correct |
25 |
Correct |
93 ms |
14020 KB |
Output is correct |
26 |
Correct |
92 ms |
14088 KB |
Output is correct |
27 |
Correct |
240 ms |
28036 KB |
Output is correct |
28 |
Correct |
234 ms |
27912 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 |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
10 ms |
1356 KB |
Output is correct |
9 |
Correct |
234 ms |
28076 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 |
1356 KB |
Output is correct |
13 |
Correct |
229 ms |
27884 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 |
972 KB |
Output is correct |
17 |
Correct |
111 ms |
18028 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
292 KB |
Output is correct |
21 |
Correct |
59 ms |
7156 KB |
Output is correct |
22 |
Correct |
237 ms |
27840 KB |
Output is correct |
23 |
Correct |
231 ms |
28012 KB |
Output is correct |
24 |
Correct |
242 ms |
27972 KB |
Output is correct |
25 |
Correct |
105 ms |
17988 KB |
Output is correct |
26 |
Correct |
107 ms |
18088 KB |
Output is correct |
27 |
Correct |
255 ms |
27884 KB |
Output is correct |
28 |
Correct |
264 ms |
27844 KB |
Output is correct |
29 |
Correct |
104 ms |
18028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
292 KB |
Output is correct |
2 |
Correct |
1 ms |
268 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
58 ms |
7144 KB |
Output is correct |
5 |
Correct |
228 ms |
27860 KB |
Output is correct |
6 |
Correct |
234 ms |
27940 KB |
Output is correct |
7 |
Correct |
243 ms |
27880 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
60 ms |
7232 KB |
Output is correct |
10 |
Correct |
239 ms |
27892 KB |
Output is correct |
11 |
Correct |
236 ms |
28048 KB |
Output is correct |
12 |
Correct |
241 ms |
27860 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 |
58 ms |
7140 KB |
Output is correct |
17 |
Correct |
232 ms |
27880 KB |
Output is correct |
18 |
Correct |
239 ms |
27972 KB |
Output is correct |
19 |
Correct |
229 ms |
27848 KB |
Output is correct |
20 |
Correct |
232 ms |
27972 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 |
288 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
10 ms |
1356 KB |
Output is correct |
7 |
Correct |
224 ms |
27896 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 |
10 ms |
1388 KB |
Output is correct |
13 |
Correct |
234 ms |
27900 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 |
844 KB |
Output is correct |
17 |
Correct |
121 ms |
14076 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
58 ms |
7144 KB |
Output is correct |
21 |
Correct |
227 ms |
27844 KB |
Output is correct |
22 |
Correct |
228 ms |
27844 KB |
Output is correct |
23 |
Correct |
234 ms |
27972 KB |
Output is correct |
24 |
Correct |
229 ms |
27972 KB |
Output is correct |
25 |
Correct |
93 ms |
14020 KB |
Output is correct |
26 |
Correct |
92 ms |
14088 KB |
Output is correct |
27 |
Correct |
240 ms |
28036 KB |
Output is correct |
28 |
Correct |
234 ms |
27912 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 |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
10 ms |
1356 KB |
Output is correct |
37 |
Correct |
234 ms |
28076 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 |
1356 KB |
Output is correct |
41 |
Correct |
229 ms |
27884 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 |
972 KB |
Output is correct |
45 |
Correct |
111 ms |
18028 KB |
Output is correct |
46 |
Correct |
1 ms |
204 KB |
Output is correct |
47 |
Correct |
1 ms |
204 KB |
Output is correct |
48 |
Correct |
1 ms |
292 KB |
Output is correct |
49 |
Correct |
59 ms |
7156 KB |
Output is correct |
50 |
Correct |
237 ms |
27840 KB |
Output is correct |
51 |
Correct |
231 ms |
28012 KB |
Output is correct |
52 |
Correct |
242 ms |
27972 KB |
Output is correct |
53 |
Correct |
105 ms |
17988 KB |
Output is correct |
54 |
Correct |
107 ms |
18088 KB |
Output is correct |
55 |
Correct |
255 ms |
27884 KB |
Output is correct |
56 |
Correct |
264 ms |
27844 KB |
Output is correct |
57 |
Correct |
104 ms |
18028 KB |
Output is correct |
58 |
Correct |
1 ms |
204 KB |
Output is correct |
59 |
Correct |
1 ms |
204 KB |
Output is correct |
60 |
Correct |
5 ms |
816 KB |
Output is correct |
61 |
Correct |
97 ms |
14020 KB |
Output is correct |
62 |
Correct |
1 ms |
204 KB |
Output is correct |
63 |
Correct |
1 ms |
288 KB |
Output is correct |
64 |
Correct |
1 ms |
204 KB |
Output is correct |
65 |
Correct |
59 ms |
7148 KB |
Output is correct |
66 |
Correct |
102 ms |
18028 KB |
Output is correct |
67 |
Correct |
99 ms |
14020 KB |
Output is correct |
68 |
Correct |
101 ms |
18040 KB |
Output is correct |
69 |
Correct |
106 ms |
18040 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 |
288 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
10 ms |
1356 KB |
Output is correct |
7 |
Correct |
224 ms |
27896 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 |
10 ms |
1388 KB |
Output is correct |
13 |
Correct |
234 ms |
27900 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 |
844 KB |
Output is correct |
17 |
Correct |
121 ms |
14076 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
58 ms |
7144 KB |
Output is correct |
21 |
Correct |
227 ms |
27844 KB |
Output is correct |
22 |
Correct |
228 ms |
27844 KB |
Output is correct |
23 |
Correct |
234 ms |
27972 KB |
Output is correct |
24 |
Correct |
229 ms |
27972 KB |
Output is correct |
25 |
Correct |
93 ms |
14020 KB |
Output is correct |
26 |
Correct |
92 ms |
14088 KB |
Output is correct |
27 |
Correct |
240 ms |
28036 KB |
Output is correct |
28 |
Correct |
234 ms |
27912 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 |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
10 ms |
1356 KB |
Output is correct |
37 |
Correct |
234 ms |
28076 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 |
1356 KB |
Output is correct |
41 |
Correct |
229 ms |
27884 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 |
972 KB |
Output is correct |
45 |
Correct |
111 ms |
18028 KB |
Output is correct |
46 |
Correct |
1 ms |
204 KB |
Output is correct |
47 |
Correct |
1 ms |
204 KB |
Output is correct |
48 |
Correct |
1 ms |
292 KB |
Output is correct |
49 |
Correct |
59 ms |
7156 KB |
Output is correct |
50 |
Correct |
237 ms |
27840 KB |
Output is correct |
51 |
Correct |
231 ms |
28012 KB |
Output is correct |
52 |
Correct |
242 ms |
27972 KB |
Output is correct |
53 |
Correct |
105 ms |
17988 KB |
Output is correct |
54 |
Correct |
107 ms |
18088 KB |
Output is correct |
55 |
Correct |
255 ms |
27884 KB |
Output is correct |
56 |
Correct |
264 ms |
27844 KB |
Output is correct |
57 |
Correct |
104 ms |
18028 KB |
Output is correct |
58 |
Correct |
1 ms |
292 KB |
Output is correct |
59 |
Correct |
1 ms |
268 KB |
Output is correct |
60 |
Correct |
1 ms |
204 KB |
Output is correct |
61 |
Correct |
58 ms |
7144 KB |
Output is correct |
62 |
Correct |
228 ms |
27860 KB |
Output is correct |
63 |
Correct |
234 ms |
27940 KB |
Output is correct |
64 |
Correct |
243 ms |
27880 KB |
Output is correct |
65 |
Correct |
1 ms |
204 KB |
Output is correct |
66 |
Correct |
60 ms |
7232 KB |
Output is correct |
67 |
Correct |
239 ms |
27892 KB |
Output is correct |
68 |
Correct |
236 ms |
28048 KB |
Output is correct |
69 |
Correct |
241 ms |
27860 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 |
58 ms |
7140 KB |
Output is correct |
74 |
Correct |
232 ms |
27880 KB |
Output is correct |
75 |
Correct |
239 ms |
27972 KB |
Output is correct |
76 |
Correct |
229 ms |
27848 KB |
Output is correct |
77 |
Correct |
232 ms |
27972 KB |
Output is correct |
78 |
Correct |
1 ms |
204 KB |
Output is correct |
79 |
Correct |
1 ms |
204 KB |
Output is correct |
80 |
Correct |
5 ms |
816 KB |
Output is correct |
81 |
Correct |
97 ms |
14020 KB |
Output is correct |
82 |
Correct |
1 ms |
204 KB |
Output is correct |
83 |
Correct |
1 ms |
288 KB |
Output is correct |
84 |
Correct |
1 ms |
204 KB |
Output is correct |
85 |
Correct |
59 ms |
7148 KB |
Output is correct |
86 |
Correct |
102 ms |
18028 KB |
Output is correct |
87 |
Correct |
99 ms |
14020 KB |
Output is correct |
88 |
Correct |
101 ms |
18040 KB |
Output is correct |
89 |
Correct |
106 ms |
18040 KB |
Output is correct |
90 |
Correct |
1 ms |
204 KB |
Output is correct |
91 |
Correct |
1 ms |
332 KB |
Output is correct |
92 |
Correct |
5 ms |
588 KB |
Output is correct |
93 |
Correct |
124 ms |
10104 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 |
23 ms |
2676 KB |
Output is correct |
98 |
Correct |
89 ms |
10088 KB |
Output is correct |
99 |
Correct |
92 ms |
10088 KB |
Output is correct |
100 |
Correct |
95 ms |
10052 KB |
Output is correct |
101 |
Correct |
110 ms |
10092 KB |
Output is correct |
102 |
Correct |
87 ms |
10100 KB |
Output is correct |
103 |
Correct |
89 ms |
10012 KB |
Output is correct |
104 |
Correct |
110 ms |
10092 KB |
Output is correct |
105 |
Correct |
117 ms |
10088 KB |
Output is correct |