#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
int construct(vector<vector<int>> p) {
int n = p.size(), cur = 0, bad = 0, sp = 0;
vector<vector<int>> ans(n, vector<int>(n));
vector<int> seen(n), solved(n), cycle;
function<void(int)> dfs = [&](int node) {
if (bad)
return;
seen[node] = cur;
int c1 = 0, c2 = 0;
for (int i = 0; i < n; ++i)
if (i != node) {
if (p[node][i] == 1)
++c1;
else if (p[node][i] == 2)
++c2;
else if (p[node][i] == 3)
bad = 1;
if (!seen[i] && p[node][i])
dfs(i);
if (!p[node][i] && seen[i] == cur)
bad = 1;
}
if (!c1 && c2)
cycle.emplace_back(node);
if (c1 && c2)
++sp;
if (!solved[node] && c1) {
if (c2)
cycle.emplace_back(node);
solved[node] = 1;
int cur = node;
for (int i = 0; i < n; ++i)
if (i != node && p[node][i] == 1)
ans[cur][i] = ans[i][cur] = solved[i] = 1, cur = i;
}
};
for (int i = 0; i < n; ++i)
if (!seen[i]) {
cycle.clear();
++cur;
sp = 0;
dfs(i);
if (bad)
return 0;
if (!cycle.empty()) {
if (cycle.size() + sp == 2)
return 0;
int root=cycle[0], cur = root;
for (auto x : cycle) {
if (x !=root) {
ans[cur][x] = ans[x][cur] = 1;
cur = x;
}
}
ans[cur][root] = ans[root][cur] = 1;
}
}
build(ans);
return 1;
}
# |
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 |
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 |
10 ms |
1192 KB |
Output is correct |
7 |
Correct |
182 ms |
22012 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 |
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 |
10 ms |
1192 KB |
Output is correct |
7 |
Correct |
182 ms |
22012 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
7 ms |
1188 KB |
Output is correct |
13 |
Correct |
161 ms |
21956 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 |
716 KB |
Output is correct |
17 |
Correct |
70 ms |
12292 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
63 ms |
5900 KB |
Output is correct |
21 |
Correct |
169 ms |
22572 KB |
Output is correct |
22 |
Correct |
167 ms |
23380 KB |
Output is correct |
23 |
Correct |
170 ms |
22024 KB |
Output is correct |
24 |
Correct |
162 ms |
23108 KB |
Output is correct |
25 |
Correct |
68 ms |
12028 KB |
Output is correct |
26 |
Correct |
68 ms |
13124 KB |
Output is correct |
27 |
Correct |
225 ms |
23156 KB |
Output is correct |
28 |
Correct |
188 ms |
22020 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 |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 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 |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
8 ms |
1100 KB |
Output is correct |
9 |
Correct |
167 ms |
22044 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 |
1100 KB |
Output is correct |
13 |
Correct |
178 ms |
22084 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 |
716 KB |
Output is correct |
17 |
Correct |
72 ms |
12028 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
45 ms |
5756 KB |
Output is correct |
22 |
Correct |
205 ms |
23556 KB |
Output is correct |
23 |
Correct |
163 ms |
22016 KB |
Output is correct |
24 |
Correct |
179 ms |
21972 KB |
Output is correct |
25 |
Correct |
95 ms |
13632 KB |
Output is correct |
26 |
Correct |
67 ms |
13680 KB |
Output is correct |
27 |
Correct |
165 ms |
22060 KB |
Output is correct |
28 |
Correct |
217 ms |
23580 KB |
Output is correct |
29 |
Correct |
67 ms |
13636 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 |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
42 ms |
5700 KB |
Output is correct |
5 |
Correct |
175 ms |
22004 KB |
Output is correct |
6 |
Correct |
175 ms |
22008 KB |
Output is correct |
7 |
Correct |
181 ms |
22088 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
42 ms |
5688 KB |
Output is correct |
10 |
Correct |
168 ms |
22028 KB |
Output is correct |
11 |
Correct |
163 ms |
21960 KB |
Output is correct |
12 |
Correct |
179 ms |
22016 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
43 ms |
5756 KB |
Output is correct |
17 |
Correct |
160 ms |
23968 KB |
Output is correct |
18 |
Correct |
172 ms |
24004 KB |
Output is correct |
19 |
Correct |
164 ms |
23972 KB |
Output is correct |
20 |
Correct |
162 ms |
23936 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 |
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 |
10 ms |
1192 KB |
Output is correct |
7 |
Correct |
182 ms |
22012 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
7 ms |
1188 KB |
Output is correct |
13 |
Correct |
161 ms |
21956 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 |
716 KB |
Output is correct |
17 |
Correct |
70 ms |
12292 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
63 ms |
5900 KB |
Output is correct |
21 |
Correct |
169 ms |
22572 KB |
Output is correct |
22 |
Correct |
167 ms |
23380 KB |
Output is correct |
23 |
Correct |
170 ms |
22024 KB |
Output is correct |
24 |
Correct |
162 ms |
23108 KB |
Output is correct |
25 |
Correct |
68 ms |
12028 KB |
Output is correct |
26 |
Correct |
68 ms |
13124 KB |
Output is correct |
27 |
Correct |
225 ms |
23156 KB |
Output is correct |
28 |
Correct |
188 ms |
22020 KB |
Output is correct |
29 |
Correct |
0 ms |
204 KB |
Output is correct |
30 |
Correct |
0 ms |
204 KB |
Output is correct |
31 |
Correct |
0 ms |
204 KB |
Output is correct |
32 |
Correct |
0 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 |
0 ms |
204 KB |
Output is correct |
36 |
Correct |
8 ms |
1100 KB |
Output is correct |
37 |
Correct |
167 ms |
22044 KB |
Output is correct |
38 |
Correct |
0 ms |
204 KB |
Output is correct |
39 |
Correct |
0 ms |
204 KB |
Output is correct |
40 |
Correct |
7 ms |
1100 KB |
Output is correct |
41 |
Correct |
178 ms |
22084 KB |
Output is correct |
42 |
Correct |
0 ms |
204 KB |
Output is correct |
43 |
Correct |
0 ms |
204 KB |
Output is correct |
44 |
Correct |
3 ms |
716 KB |
Output is correct |
45 |
Correct |
72 ms |
12028 KB |
Output is correct |
46 |
Correct |
1 ms |
204 KB |
Output is correct |
47 |
Correct |
1 ms |
204 KB |
Output is correct |
48 |
Correct |
0 ms |
204 KB |
Output is correct |
49 |
Correct |
45 ms |
5756 KB |
Output is correct |
50 |
Correct |
205 ms |
23556 KB |
Output is correct |
51 |
Correct |
163 ms |
22016 KB |
Output is correct |
52 |
Correct |
179 ms |
21972 KB |
Output is correct |
53 |
Correct |
95 ms |
13632 KB |
Output is correct |
54 |
Correct |
67 ms |
13680 KB |
Output is correct |
55 |
Correct |
165 ms |
22060 KB |
Output is correct |
56 |
Correct |
217 ms |
23580 KB |
Output is correct |
57 |
Correct |
67 ms |
13636 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 |
788 KB |
Output is correct |
61 |
Correct |
73 ms |
13684 KB |
Output is correct |
62 |
Correct |
0 ms |
204 KB |
Output is correct |
63 |
Correct |
0 ms |
204 KB |
Output is correct |
64 |
Correct |
0 ms |
204 KB |
Output is correct |
65 |
Correct |
46 ms |
5700 KB |
Output is correct |
66 |
Correct |
68 ms |
14192 KB |
Output is correct |
67 |
Correct |
66 ms |
14060 KB |
Output is correct |
68 |
Correct |
73 ms |
14072 KB |
Output is correct |
69 |
Correct |
67 ms |
14060 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 |
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 |
10 ms |
1192 KB |
Output is correct |
7 |
Correct |
182 ms |
22012 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
7 ms |
1188 KB |
Output is correct |
13 |
Correct |
161 ms |
21956 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 |
716 KB |
Output is correct |
17 |
Correct |
70 ms |
12292 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
63 ms |
5900 KB |
Output is correct |
21 |
Correct |
169 ms |
22572 KB |
Output is correct |
22 |
Correct |
167 ms |
23380 KB |
Output is correct |
23 |
Correct |
170 ms |
22024 KB |
Output is correct |
24 |
Correct |
162 ms |
23108 KB |
Output is correct |
25 |
Correct |
68 ms |
12028 KB |
Output is correct |
26 |
Correct |
68 ms |
13124 KB |
Output is correct |
27 |
Correct |
225 ms |
23156 KB |
Output is correct |
28 |
Correct |
188 ms |
22020 KB |
Output is correct |
29 |
Correct |
0 ms |
204 KB |
Output is correct |
30 |
Correct |
0 ms |
204 KB |
Output is correct |
31 |
Correct |
0 ms |
204 KB |
Output is correct |
32 |
Correct |
0 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 |
0 ms |
204 KB |
Output is correct |
36 |
Correct |
8 ms |
1100 KB |
Output is correct |
37 |
Correct |
167 ms |
22044 KB |
Output is correct |
38 |
Correct |
0 ms |
204 KB |
Output is correct |
39 |
Correct |
0 ms |
204 KB |
Output is correct |
40 |
Correct |
7 ms |
1100 KB |
Output is correct |
41 |
Correct |
178 ms |
22084 KB |
Output is correct |
42 |
Correct |
0 ms |
204 KB |
Output is correct |
43 |
Correct |
0 ms |
204 KB |
Output is correct |
44 |
Correct |
3 ms |
716 KB |
Output is correct |
45 |
Correct |
72 ms |
12028 KB |
Output is correct |
46 |
Correct |
1 ms |
204 KB |
Output is correct |
47 |
Correct |
1 ms |
204 KB |
Output is correct |
48 |
Correct |
0 ms |
204 KB |
Output is correct |
49 |
Correct |
45 ms |
5756 KB |
Output is correct |
50 |
Correct |
205 ms |
23556 KB |
Output is correct |
51 |
Correct |
163 ms |
22016 KB |
Output is correct |
52 |
Correct |
179 ms |
21972 KB |
Output is correct |
53 |
Correct |
95 ms |
13632 KB |
Output is correct |
54 |
Correct |
67 ms |
13680 KB |
Output is correct |
55 |
Correct |
165 ms |
22060 KB |
Output is correct |
56 |
Correct |
217 ms |
23580 KB |
Output is correct |
57 |
Correct |
67 ms |
13636 KB |
Output is correct |
58 |
Correct |
0 ms |
204 KB |
Output is correct |
59 |
Correct |
0 ms |
204 KB |
Output is correct |
60 |
Correct |
0 ms |
204 KB |
Output is correct |
61 |
Correct |
42 ms |
5700 KB |
Output is correct |
62 |
Correct |
175 ms |
22004 KB |
Output is correct |
63 |
Correct |
175 ms |
22008 KB |
Output is correct |
64 |
Correct |
181 ms |
22088 KB |
Output is correct |
65 |
Correct |
0 ms |
204 KB |
Output is correct |
66 |
Correct |
42 ms |
5688 KB |
Output is correct |
67 |
Correct |
168 ms |
22028 KB |
Output is correct |
68 |
Correct |
163 ms |
21960 KB |
Output is correct |
69 |
Correct |
179 ms |
22016 KB |
Output is correct |
70 |
Correct |
0 ms |
204 KB |
Output is correct |
71 |
Correct |
0 ms |
204 KB |
Output is correct |
72 |
Correct |
0 ms |
204 KB |
Output is correct |
73 |
Correct |
43 ms |
5756 KB |
Output is correct |
74 |
Correct |
160 ms |
23968 KB |
Output is correct |
75 |
Correct |
172 ms |
24004 KB |
Output is correct |
76 |
Correct |
164 ms |
23972 KB |
Output is correct |
77 |
Correct |
162 ms |
23936 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 |
788 KB |
Output is correct |
81 |
Correct |
73 ms |
13684 KB |
Output is correct |
82 |
Correct |
0 ms |
204 KB |
Output is correct |
83 |
Correct |
0 ms |
204 KB |
Output is correct |
84 |
Correct |
0 ms |
204 KB |
Output is correct |
85 |
Correct |
46 ms |
5700 KB |
Output is correct |
86 |
Correct |
68 ms |
14192 KB |
Output is correct |
87 |
Correct |
66 ms |
14060 KB |
Output is correct |
88 |
Correct |
73 ms |
14072 KB |
Output is correct |
89 |
Correct |
67 ms |
14060 KB |
Output is correct |
90 |
Correct |
0 ms |
288 KB |
Output is correct |
91 |
Correct |
1 ms |
204 KB |
Output is correct |
92 |
Correct |
3 ms |
844 KB |
Output is correct |
93 |
Correct |
66 ms |
14064 KB |
Output is correct |
94 |
Correct |
0 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 |
17 ms |
3660 KB |
Output is correct |
98 |
Correct |
66 ms |
14112 KB |
Output is correct |
99 |
Correct |
69 ms |
14060 KB |
Output is correct |
100 |
Correct |
66 ms |
14020 KB |
Output is correct |
101 |
Correct |
65 ms |
14020 KB |
Output is correct |
102 |
Correct |
74 ms |
13996 KB |
Output is correct |
103 |
Correct |
68 ms |
13988 KB |
Output is correct |
104 |
Correct |
62 ms |
14076 KB |
Output is correct |
105 |
Correct |
79 ms |
14060 KB |
Output is correct |