#include "supertrees.h"
#include <vector>
#include <queue>
#include <set>
using namespace std;
struct DSU {
vector<int> par, sz;
DSU(int n) : par(n), sz(n) {
for (int i=0; i<n; i++) {
par[i] = i;
sz[i] = 1;
}
}
int findRoot(int u) {
if (par[u] != u)
par[u] = findRoot(par[u]);
return par[u];
}
bool unite(int u, int v) {
u = findRoot(u), v = findRoot(v);
if (u == v)
return false;
if (sz[u] < sz[v])
swap(u, v);
par[v] = u;
sz[u] += sz[v];
return true;
}
};
int construct(std::vector<std::vector<int>> p) {
int n = p.size();
std::vector<std::vector<int>> answer;
for (int i = 0; i < n; i++) {
std::vector<int> row;
row.resize(n);
answer.push_back(row);
}
vector<vector<int>> adj(n);
for (int i=0; i<n; i++)
for (int j=0; j<n; j++) {
if (p[i][j] == 3)
return 0;
if (p[i][j] == 1)
adj[i].push_back(j);
}
vector<int> id(n, -1);
vector<vector<int>> comp;
queue<int> q;
for (int i=0; i<n; i++)
if (id[i] == -1) {
comp.emplace_back();
q.push(i);
id[i] = (int) comp.size() - 1;
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v : comp.back())
if (p[u][v] != 1)
return 0;
comp.back().push_back(u);
for (int v : adj[u])
if (id[v] == -1) {
q.push(v);
id[v] = (int) comp.size() - 1;
}
}
}
int m = comp.size();
DSU ds(m);
vector<vector<bool>> two(m, vector<bool>(m));
for (int i=0; i<m; i++) {
for (int j=1; j<(int)comp[i].size(); j++)
answer[comp[i][j-1]][comp[i][j]] = answer[comp[i][j]][comp[i][j-1]] = 1;
for (int u : comp[i])
for (int v=0; v<n; v++)
if (p[u][v] == 2) {
two[id[u]][id[v]] = two[id[v]][id[u]] = true;
ds.unite(id[u], id[v]);
}
}
vector<vector<int>> big(m);
for (int i=0; i<m; i++) {
for (int v : big[ds.par[i]])
if (!two[i][v])
return 0;
big[ds.par[i]].push_back(i);
}
auto get = [&] (int id) {
return comp[id][0];
};
for (auto &c : big) {
if (c.size() == 2)
return 0;
for (int i=1; i<(int)c.size(); i++)
answer[get(c[i-1])][get(c[i])] = answer[get(c[i])][get(c[i-1])] = 1;
if (c.size() > 2)
answer[get(c.back())][get(c[0])] = answer[get(c[0])][get(c.back())] = 1;
}
build(answer);
return 1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
11 ms |
1408 KB |
Output is correct |
7 |
Correct |
263 ms |
26104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
11 ms |
1408 KB |
Output is correct |
7 |
Correct |
263 ms |
26104 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
360 KB |
Output is correct |
12 |
Correct |
11 ms |
1280 KB |
Output is correct |
13 |
Correct |
247 ms |
22392 KB |
Output is correct |
14 |
Correct |
1 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
6 ms |
896 KB |
Output is correct |
17 |
Correct |
124 ms |
14584 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
63 ms |
6008 KB |
Output is correct |
21 |
Correct |
255 ms |
22732 KB |
Output is correct |
22 |
Correct |
252 ms |
22264 KB |
Output is correct |
23 |
Correct |
266 ms |
24312 KB |
Output is correct |
24 |
Correct |
257 ms |
22392 KB |
Output is correct |
25 |
Correct |
114 ms |
12668 KB |
Output is correct |
26 |
Correct |
110 ms |
12280 KB |
Output is correct |
27 |
Correct |
273 ms |
25208 KB |
Output is correct |
28 |
Correct |
271 ms |
22520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
10 ms |
1280 KB |
Output is correct |
9 |
Correct |
252 ms |
22392 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
12 ms |
1280 KB |
Output is correct |
13 |
Correct |
274 ms |
22392 KB |
Output is correct |
14 |
Correct |
1 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
6 ms |
768 KB |
Output is correct |
17 |
Correct |
133 ms |
12536 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
0 ms |
256 KB |
Output is correct |
21 |
Correct |
63 ms |
5996 KB |
Output is correct |
22 |
Correct |
257 ms |
22392 KB |
Output is correct |
23 |
Correct |
256 ms |
22392 KB |
Output is correct |
24 |
Correct |
274 ms |
22528 KB |
Output is correct |
25 |
Correct |
112 ms |
12536 KB |
Output is correct |
26 |
Correct |
117 ms |
12492 KB |
Output is correct |
27 |
Correct |
262 ms |
22392 KB |
Output is correct |
28 |
Correct |
282 ms |
22392 KB |
Output is correct |
29 |
Correct |
117 ms |
12536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
64 ms |
5880 KB |
Output is correct |
5 |
Correct |
252 ms |
22768 KB |
Output is correct |
6 |
Correct |
264 ms |
22240 KB |
Output is correct |
7 |
Correct |
268 ms |
24140 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
66 ms |
5880 KB |
Output is correct |
10 |
Correct |
292 ms |
22392 KB |
Output is correct |
11 |
Correct |
256 ms |
22392 KB |
Output is correct |
12 |
Correct |
271 ms |
22392 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
64 ms |
5872 KB |
Output is correct |
17 |
Correct |
273 ms |
22424 KB |
Output is correct |
18 |
Correct |
267 ms |
22264 KB |
Output is correct |
19 |
Correct |
262 ms |
22392 KB |
Output is correct |
20 |
Correct |
254 ms |
22484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
11 ms |
1408 KB |
Output is correct |
7 |
Correct |
263 ms |
26104 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
360 KB |
Output is correct |
12 |
Correct |
11 ms |
1280 KB |
Output is correct |
13 |
Correct |
247 ms |
22392 KB |
Output is correct |
14 |
Correct |
1 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
6 ms |
896 KB |
Output is correct |
17 |
Correct |
124 ms |
14584 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
63 ms |
6008 KB |
Output is correct |
21 |
Correct |
255 ms |
22732 KB |
Output is correct |
22 |
Correct |
252 ms |
22264 KB |
Output is correct |
23 |
Correct |
266 ms |
24312 KB |
Output is correct |
24 |
Correct |
257 ms |
22392 KB |
Output is correct |
25 |
Correct |
114 ms |
12668 KB |
Output is correct |
26 |
Correct |
110 ms |
12280 KB |
Output is correct |
27 |
Correct |
273 ms |
25208 KB |
Output is correct |
28 |
Correct |
271 ms |
22520 KB |
Output is correct |
29 |
Correct |
1 ms |
256 KB |
Output is correct |
30 |
Correct |
0 ms |
256 KB |
Output is correct |
31 |
Correct |
1 ms |
256 KB |
Output is correct |
32 |
Correct |
1 ms |
256 KB |
Output is correct |
33 |
Correct |
1 ms |
256 KB |
Output is correct |
34 |
Correct |
1 ms |
256 KB |
Output is correct |
35 |
Correct |
1 ms |
384 KB |
Output is correct |
36 |
Correct |
10 ms |
1280 KB |
Output is correct |
37 |
Correct |
252 ms |
22392 KB |
Output is correct |
38 |
Correct |
1 ms |
384 KB |
Output is correct |
39 |
Correct |
1 ms |
256 KB |
Output is correct |
40 |
Correct |
12 ms |
1280 KB |
Output is correct |
41 |
Correct |
274 ms |
22392 KB |
Output is correct |
42 |
Correct |
1 ms |
256 KB |
Output is correct |
43 |
Correct |
0 ms |
256 KB |
Output is correct |
44 |
Correct |
6 ms |
768 KB |
Output is correct |
45 |
Correct |
133 ms |
12536 KB |
Output is correct |
46 |
Correct |
1 ms |
256 KB |
Output is correct |
47 |
Correct |
0 ms |
256 KB |
Output is correct |
48 |
Correct |
0 ms |
256 KB |
Output is correct |
49 |
Correct |
63 ms |
5996 KB |
Output is correct |
50 |
Correct |
257 ms |
22392 KB |
Output is correct |
51 |
Correct |
256 ms |
22392 KB |
Output is correct |
52 |
Correct |
274 ms |
22528 KB |
Output is correct |
53 |
Correct |
112 ms |
12536 KB |
Output is correct |
54 |
Correct |
117 ms |
12492 KB |
Output is correct |
55 |
Correct |
262 ms |
22392 KB |
Output is correct |
56 |
Correct |
282 ms |
22392 KB |
Output is correct |
57 |
Correct |
117 ms |
12536 KB |
Output is correct |
58 |
Correct |
0 ms |
256 KB |
Output is correct |
59 |
Correct |
0 ms |
256 KB |
Output is correct |
60 |
Correct |
6 ms |
896 KB |
Output is correct |
61 |
Correct |
125 ms |
14200 KB |
Output is correct |
62 |
Correct |
1 ms |
256 KB |
Output is correct |
63 |
Correct |
1 ms |
256 KB |
Output is correct |
64 |
Correct |
0 ms |
256 KB |
Output is correct |
65 |
Correct |
63 ms |
5880 KB |
Output is correct |
66 |
Correct |
126 ms |
12408 KB |
Output is correct |
67 |
Correct |
111 ms |
12152 KB |
Output is correct |
68 |
Correct |
114 ms |
12408 KB |
Output is correct |
69 |
Correct |
114 ms |
12536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
11 ms |
1408 KB |
Output is correct |
7 |
Correct |
263 ms |
26104 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
360 KB |
Output is correct |
12 |
Correct |
11 ms |
1280 KB |
Output is correct |
13 |
Correct |
247 ms |
22392 KB |
Output is correct |
14 |
Correct |
1 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
6 ms |
896 KB |
Output is correct |
17 |
Correct |
124 ms |
14584 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
63 ms |
6008 KB |
Output is correct |
21 |
Correct |
255 ms |
22732 KB |
Output is correct |
22 |
Correct |
252 ms |
22264 KB |
Output is correct |
23 |
Correct |
266 ms |
24312 KB |
Output is correct |
24 |
Correct |
257 ms |
22392 KB |
Output is correct |
25 |
Correct |
114 ms |
12668 KB |
Output is correct |
26 |
Correct |
110 ms |
12280 KB |
Output is correct |
27 |
Correct |
273 ms |
25208 KB |
Output is correct |
28 |
Correct |
271 ms |
22520 KB |
Output is correct |
29 |
Correct |
1 ms |
256 KB |
Output is correct |
30 |
Correct |
0 ms |
256 KB |
Output is correct |
31 |
Correct |
1 ms |
256 KB |
Output is correct |
32 |
Correct |
1 ms |
256 KB |
Output is correct |
33 |
Correct |
1 ms |
256 KB |
Output is correct |
34 |
Correct |
1 ms |
256 KB |
Output is correct |
35 |
Correct |
1 ms |
384 KB |
Output is correct |
36 |
Correct |
10 ms |
1280 KB |
Output is correct |
37 |
Correct |
252 ms |
22392 KB |
Output is correct |
38 |
Correct |
1 ms |
384 KB |
Output is correct |
39 |
Correct |
1 ms |
256 KB |
Output is correct |
40 |
Correct |
12 ms |
1280 KB |
Output is correct |
41 |
Correct |
274 ms |
22392 KB |
Output is correct |
42 |
Correct |
1 ms |
256 KB |
Output is correct |
43 |
Correct |
0 ms |
256 KB |
Output is correct |
44 |
Correct |
6 ms |
768 KB |
Output is correct |
45 |
Correct |
133 ms |
12536 KB |
Output is correct |
46 |
Correct |
1 ms |
256 KB |
Output is correct |
47 |
Correct |
0 ms |
256 KB |
Output is correct |
48 |
Correct |
0 ms |
256 KB |
Output is correct |
49 |
Correct |
63 ms |
5996 KB |
Output is correct |
50 |
Correct |
257 ms |
22392 KB |
Output is correct |
51 |
Correct |
256 ms |
22392 KB |
Output is correct |
52 |
Correct |
274 ms |
22528 KB |
Output is correct |
53 |
Correct |
112 ms |
12536 KB |
Output is correct |
54 |
Correct |
117 ms |
12492 KB |
Output is correct |
55 |
Correct |
262 ms |
22392 KB |
Output is correct |
56 |
Correct |
282 ms |
22392 KB |
Output is correct |
57 |
Correct |
117 ms |
12536 KB |
Output is correct |
58 |
Correct |
0 ms |
256 KB |
Output is correct |
59 |
Correct |
1 ms |
384 KB |
Output is correct |
60 |
Correct |
1 ms |
256 KB |
Output is correct |
61 |
Correct |
64 ms |
5880 KB |
Output is correct |
62 |
Correct |
252 ms |
22768 KB |
Output is correct |
63 |
Correct |
264 ms |
22240 KB |
Output is correct |
64 |
Correct |
268 ms |
24140 KB |
Output is correct |
65 |
Correct |
1 ms |
256 KB |
Output is correct |
66 |
Correct |
66 ms |
5880 KB |
Output is correct |
67 |
Correct |
292 ms |
22392 KB |
Output is correct |
68 |
Correct |
256 ms |
22392 KB |
Output is correct |
69 |
Correct |
271 ms |
22392 KB |
Output is correct |
70 |
Correct |
1 ms |
384 KB |
Output is correct |
71 |
Correct |
1 ms |
256 KB |
Output is correct |
72 |
Correct |
1 ms |
256 KB |
Output is correct |
73 |
Correct |
64 ms |
5872 KB |
Output is correct |
74 |
Correct |
273 ms |
22424 KB |
Output is correct |
75 |
Correct |
267 ms |
22264 KB |
Output is correct |
76 |
Correct |
262 ms |
22392 KB |
Output is correct |
77 |
Correct |
254 ms |
22484 KB |
Output is correct |
78 |
Correct |
0 ms |
256 KB |
Output is correct |
79 |
Correct |
0 ms |
256 KB |
Output is correct |
80 |
Correct |
6 ms |
896 KB |
Output is correct |
81 |
Correct |
125 ms |
14200 KB |
Output is correct |
82 |
Correct |
1 ms |
256 KB |
Output is correct |
83 |
Correct |
1 ms |
256 KB |
Output is correct |
84 |
Correct |
0 ms |
256 KB |
Output is correct |
85 |
Correct |
63 ms |
5880 KB |
Output is correct |
86 |
Correct |
126 ms |
12408 KB |
Output is correct |
87 |
Correct |
111 ms |
12152 KB |
Output is correct |
88 |
Correct |
114 ms |
12408 KB |
Output is correct |
89 |
Correct |
114 ms |
12536 KB |
Output is correct |
90 |
Correct |
0 ms |
256 KB |
Output is correct |
91 |
Correct |
1 ms |
288 KB |
Output is correct |
92 |
Correct |
5 ms |
768 KB |
Output is correct |
93 |
Correct |
114 ms |
12268 KB |
Output is correct |
94 |
Correct |
0 ms |
256 KB |
Output is correct |
95 |
Correct |
0 ms |
256 KB |
Output is correct |
96 |
Correct |
0 ms |
256 KB |
Output is correct |
97 |
Correct |
28 ms |
3268 KB |
Output is correct |
98 |
Correct |
115 ms |
12240 KB |
Output is correct |
99 |
Correct |
114 ms |
12152 KB |
Output is correct |
100 |
Correct |
110 ms |
12152 KB |
Output is correct |
101 |
Correct |
112 ms |
12152 KB |
Output is correct |
102 |
Correct |
118 ms |
12152 KB |
Output is correct |
103 |
Correct |
114 ms |
12156 KB |
Output is correct |
104 |
Correct |
123 ms |
12280 KB |
Output is correct |
105 |
Correct |
116 ms |
12152 KB |
Output is correct |