#include"supertrees.h"
#include<bits/stdc++.h>
using namespace std;
int N;
vector<vector<int>> P, ans, D;
vector<int> par;
int parent(int x) {
return (par[x] == x ? x : par[x] = parent(par[x]));
}
int unite(int x, int y) {
x = parent(x);
y = parent(y);
if(x == y) return 1;
if((int)D[x].size() < (int)D[y].size()) swap(x, y);
for(auto& u : D[x]) for(auto& v : D[y]) {
if(P[u][v] != 1) return 0;
}
for(auto& u : D[y]) D[x].emplace_back(u);
par[y] = x;
return 1;
}
int construct(vector<vector<int>> _p) {
swap(P, _p);
N = (int)P.size();
D.resize(N);
par.resize(N); iota(begin(par), end(par), 0);
for(int i = 0; i < N; ++i) D[i].push_back(i);
for(int i = 0; i < N; ++i) {
for(int j = i + 1; j < N; ++j) {
if(P[i][j] == 1) {
if(!unite(i, j)) {
return 0;
}
} else if(P[i][j] == 3) return 0;
}
}
vector<int> comps;
{
set<int> s;
for(int i = 0; i < N; ++i) {
s.insert(parent(i));
}
comps = vector<int>(begin(s), end(s));
}
vector<vector<int>> twos(N);
for(int i = 0; i < N; ++i) {
for(int j = 0; j < N; ++j) {
if(P[i][j] == 2) {
twos[i].push_back(j);
}
}
}
ans = vector<vector<int>>(N, vector<int>(N, 0));
map<vector<int>, vector<int>> cycles;
for(auto& u : comps) {
for(int i = 0; i < (int)D[u].size(); ++i) {
if(D[u][i] == u) swap(D[u][i], D[u][0]);
}
for(int i = 1; i < (int)D[u].size(); ++i) {
if(twos[u] != twos[D[u][i]]) return 0;
ans[u][D[u][i]] = 1;
ans[D[u][i]][u] = 1;
}
twos[u].push_back(u);
{
set<int> s;
for(auto& v : twos[u]) {
s.insert(parent(v));
}
twos[u] = vector<int>(begin(s), end(s));
}
cycles[twos[u]].emplace_back(u);
}
for(auto& u : cycles) {
if(u.first != u.second) return 0;
if(u.first.size() == 2) return 0;
if(u.first.size() == 1) continue;
ans[u.first[0]][u.first.back()] = 1;
ans[u.first.back()][u.first[0]] = 1;
for(int i = 1; i < (int)u.first.size(); ++i) {
ans[u.first[i]][u.first[i - 1]] = 1;
ans[u.first[i - 1]][u.first[i]] = 1;
}
}
build(ans);
return 1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1236 KB |
Output is correct |
7 |
Correct |
151 ms |
22608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1236 KB |
Output is correct |
7 |
Correct |
151 ms |
22608 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
1204 KB |
Output is correct |
13 |
Correct |
154 ms |
22944 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
300 KB |
Output is correct |
16 |
Correct |
3 ms |
596 KB |
Output is correct |
17 |
Correct |
66 ms |
8680 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
38 ms |
5972 KB |
Output is correct |
21 |
Correct |
154 ms |
22564 KB |
Output is correct |
22 |
Correct |
157 ms |
22592 KB |
Output is correct |
23 |
Correct |
158 ms |
22656 KB |
Output is correct |
24 |
Correct |
151 ms |
22816 KB |
Output is correct |
25 |
Correct |
74 ms |
8772 KB |
Output is correct |
26 |
Correct |
64 ms |
8796 KB |
Output is correct |
27 |
Correct |
187 ms |
22584 KB |
Output is correct |
28 |
Correct |
154 ms |
22732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
300 KB |
Output is correct |
3 |
Correct |
1 ms |
304 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
7 ms |
1236 KB |
Output is correct |
9 |
Correct |
150 ms |
22884 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
9 ms |
1364 KB |
Output is correct |
13 |
Correct |
222 ms |
26688 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
5 ms |
980 KB |
Output is correct |
17 |
Correct |
125 ms |
16808 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
39 ms |
6092 KB |
Output is correct |
22 |
Correct |
156 ms |
23056 KB |
Output is correct |
23 |
Correct |
161 ms |
22780 KB |
Output is correct |
24 |
Correct |
188 ms |
24712 KB |
Output is correct |
25 |
Correct |
68 ms |
12852 KB |
Output is correct |
26 |
Correct |
72 ms |
13160 KB |
Output is correct |
27 |
Correct |
153 ms |
22684 KB |
Output is correct |
28 |
Correct |
192 ms |
24536 KB |
Output is correct |
29 |
Correct |
66 ms |
12848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
296 KB |
Output is correct |
4 |
Correct |
38 ms |
6000 KB |
Output is correct |
5 |
Correct |
154 ms |
22640 KB |
Output is correct |
6 |
Correct |
155 ms |
22632 KB |
Output is correct |
7 |
Correct |
154 ms |
22552 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
39 ms |
6044 KB |
Output is correct |
10 |
Correct |
157 ms |
22988 KB |
Output is correct |
11 |
Correct |
156 ms |
22688 KB |
Output is correct |
12 |
Correct |
192 ms |
24712 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
39 ms |
6044 KB |
Output is correct |
17 |
Correct |
160 ms |
22848 KB |
Output is correct |
18 |
Correct |
181 ms |
22872 KB |
Output is correct |
19 |
Correct |
159 ms |
22416 KB |
Output is correct |
20 |
Correct |
152 ms |
22460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1236 KB |
Output is correct |
7 |
Correct |
151 ms |
22608 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
1204 KB |
Output is correct |
13 |
Correct |
154 ms |
22944 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
300 KB |
Output is correct |
16 |
Correct |
3 ms |
596 KB |
Output is correct |
17 |
Correct |
66 ms |
8680 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
38 ms |
5972 KB |
Output is correct |
21 |
Correct |
154 ms |
22564 KB |
Output is correct |
22 |
Correct |
157 ms |
22592 KB |
Output is correct |
23 |
Correct |
158 ms |
22656 KB |
Output is correct |
24 |
Correct |
151 ms |
22816 KB |
Output is correct |
25 |
Correct |
74 ms |
8772 KB |
Output is correct |
26 |
Correct |
64 ms |
8796 KB |
Output is correct |
27 |
Correct |
187 ms |
22584 KB |
Output is correct |
28 |
Correct |
154 ms |
22732 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
300 KB |
Output is correct |
31 |
Correct |
1 ms |
304 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
300 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
7 ms |
1236 KB |
Output is correct |
37 |
Correct |
150 ms |
22884 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
9 ms |
1364 KB |
Output is correct |
41 |
Correct |
222 ms |
26688 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
5 ms |
980 KB |
Output is correct |
45 |
Correct |
125 ms |
16808 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
0 ms |
212 KB |
Output is correct |
49 |
Correct |
39 ms |
6092 KB |
Output is correct |
50 |
Correct |
156 ms |
23056 KB |
Output is correct |
51 |
Correct |
161 ms |
22780 KB |
Output is correct |
52 |
Correct |
188 ms |
24712 KB |
Output is correct |
53 |
Correct |
68 ms |
12852 KB |
Output is correct |
54 |
Correct |
72 ms |
13160 KB |
Output is correct |
55 |
Correct |
153 ms |
22684 KB |
Output is correct |
56 |
Correct |
192 ms |
24536 KB |
Output is correct |
57 |
Correct |
66 ms |
12848 KB |
Output is correct |
58 |
Correct |
0 ms |
212 KB |
Output is correct |
59 |
Correct |
1 ms |
296 KB |
Output is correct |
60 |
Correct |
3 ms |
696 KB |
Output is correct |
61 |
Correct |
73 ms |
8796 KB |
Output is correct |
62 |
Correct |
0 ms |
212 KB |
Output is correct |
63 |
Correct |
1 ms |
296 KB |
Output is correct |
64 |
Correct |
0 ms |
212 KB |
Output is correct |
65 |
Correct |
38 ms |
6112 KB |
Output is correct |
66 |
Correct |
83 ms |
13196 KB |
Output is correct |
67 |
Correct |
61 ms |
8596 KB |
Output is correct |
68 |
Correct |
65 ms |
12676 KB |
Output is correct |
69 |
Correct |
67 ms |
12748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1236 KB |
Output is correct |
7 |
Correct |
151 ms |
22608 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
1204 KB |
Output is correct |
13 |
Correct |
154 ms |
22944 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
300 KB |
Output is correct |
16 |
Correct |
3 ms |
596 KB |
Output is correct |
17 |
Correct |
66 ms |
8680 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
38 ms |
5972 KB |
Output is correct |
21 |
Correct |
154 ms |
22564 KB |
Output is correct |
22 |
Correct |
157 ms |
22592 KB |
Output is correct |
23 |
Correct |
158 ms |
22656 KB |
Output is correct |
24 |
Correct |
151 ms |
22816 KB |
Output is correct |
25 |
Correct |
74 ms |
8772 KB |
Output is correct |
26 |
Correct |
64 ms |
8796 KB |
Output is correct |
27 |
Correct |
187 ms |
22584 KB |
Output is correct |
28 |
Correct |
154 ms |
22732 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
300 KB |
Output is correct |
31 |
Correct |
1 ms |
304 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
300 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
7 ms |
1236 KB |
Output is correct |
37 |
Correct |
150 ms |
22884 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
9 ms |
1364 KB |
Output is correct |
41 |
Correct |
222 ms |
26688 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
5 ms |
980 KB |
Output is correct |
45 |
Correct |
125 ms |
16808 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
0 ms |
212 KB |
Output is correct |
49 |
Correct |
39 ms |
6092 KB |
Output is correct |
50 |
Correct |
156 ms |
23056 KB |
Output is correct |
51 |
Correct |
161 ms |
22780 KB |
Output is correct |
52 |
Correct |
188 ms |
24712 KB |
Output is correct |
53 |
Correct |
68 ms |
12852 KB |
Output is correct |
54 |
Correct |
72 ms |
13160 KB |
Output is correct |
55 |
Correct |
153 ms |
22684 KB |
Output is correct |
56 |
Correct |
192 ms |
24536 KB |
Output is correct |
57 |
Correct |
66 ms |
12848 KB |
Output is correct |
58 |
Correct |
0 ms |
212 KB |
Output is correct |
59 |
Correct |
0 ms |
212 KB |
Output is correct |
60 |
Correct |
0 ms |
296 KB |
Output is correct |
61 |
Correct |
38 ms |
6000 KB |
Output is correct |
62 |
Correct |
154 ms |
22640 KB |
Output is correct |
63 |
Correct |
155 ms |
22632 KB |
Output is correct |
64 |
Correct |
154 ms |
22552 KB |
Output is correct |
65 |
Correct |
1 ms |
212 KB |
Output is correct |
66 |
Correct |
39 ms |
6044 KB |
Output is correct |
67 |
Correct |
157 ms |
22988 KB |
Output is correct |
68 |
Correct |
156 ms |
22688 KB |
Output is correct |
69 |
Correct |
192 ms |
24712 KB |
Output is correct |
70 |
Correct |
1 ms |
212 KB |
Output is correct |
71 |
Correct |
0 ms |
212 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
39 ms |
6044 KB |
Output is correct |
74 |
Correct |
160 ms |
22848 KB |
Output is correct |
75 |
Correct |
181 ms |
22872 KB |
Output is correct |
76 |
Correct |
159 ms |
22416 KB |
Output is correct |
77 |
Correct |
152 ms |
22460 KB |
Output is correct |
78 |
Correct |
0 ms |
212 KB |
Output is correct |
79 |
Correct |
1 ms |
296 KB |
Output is correct |
80 |
Correct |
3 ms |
696 KB |
Output is correct |
81 |
Correct |
73 ms |
8796 KB |
Output is correct |
82 |
Correct |
0 ms |
212 KB |
Output is correct |
83 |
Correct |
1 ms |
296 KB |
Output is correct |
84 |
Correct |
0 ms |
212 KB |
Output is correct |
85 |
Correct |
38 ms |
6112 KB |
Output is correct |
86 |
Correct |
83 ms |
13196 KB |
Output is correct |
87 |
Correct |
61 ms |
8596 KB |
Output is correct |
88 |
Correct |
65 ms |
12676 KB |
Output is correct |
89 |
Correct |
67 ms |
12748 KB |
Output is correct |
90 |
Correct |
1 ms |
212 KB |
Output is correct |
91 |
Correct |
0 ms |
300 KB |
Output is correct |
92 |
Correct |
3 ms |
672 KB |
Output is correct |
93 |
Correct |
64 ms |
8572 KB |
Output is correct |
94 |
Correct |
1 ms |
212 KB |
Output is correct |
95 |
Correct |
1 ms |
212 KB |
Output is correct |
96 |
Correct |
0 ms |
212 KB |
Output is correct |
97 |
Correct |
16 ms |
2612 KB |
Output is correct |
98 |
Correct |
61 ms |
8608 KB |
Output is correct |
99 |
Correct |
61 ms |
8588 KB |
Output is correct |
100 |
Correct |
62 ms |
8608 KB |
Output is correct |
101 |
Correct |
62 ms |
8600 KB |
Output is correct |
102 |
Correct |
62 ms |
8608 KB |
Output is correct |
103 |
Correct |
62 ms |
8596 KB |
Output is correct |
104 |
Correct |
62 ms |
8604 KB |
Output is correct |
105 |
Correct |
65 ms |
8612 KB |
Output is correct |