#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
struct DSU {
vector<int> pai, rank, sz;
DSU(int n) {
pai.resize(n);
rank.assign(n, 0);
sz.assign(n, 1);
iota(pai.begin(), pai.end(), 0);
}
int findSet(int v) {
return v == pai[v] ? v : pai[v] = findSet(pai[v]);
}
int join(int a, int b) {
a = findSet(a);
b = findSet(b);
if(a == b) return 0;
else if(rank[a] > rank[b]) swap(a, b);
pai[a] = b;
rank[b] += rank[a] == rank[b];
sz[b] += sz[a];
return 1;
}
};
int construct(vector<vector<int>> p) {
int n = p.size();
for(int u = 0; u < n; ++u) {
for(int v = 0; v < n; ++v) {
if(p[u][v] == 3)
return 0;
}
}
vector<int> id(n, -1);
auto g = vector<vector<int>>(n, vector<int>(n, 0));
DSU dsu(n), w(n);
int t = 0;
for(int v = 0; v < n; ++v) {
if(id[v] != -1) continue;
queue<int> q;
vector<int> comp;
id[v] = t;
q.push(v);
while(!q.empty()) {
int u = q.front();
q.pop();
comp.push_back(u);
for(int v = 0; v < n; ++v) {
if(p[u][v] == 1) dsu.join(u, v);
if(id[v] != -1 || p[u][v] == 0) continue;
id[v] = t;
q.push(v);
}
}
for(int i = 0; i < (int)comp.size(); ++i) {
int u = comp[i];
for(int j = i + 1; j < (int)comp.size(); ++j) {
int v = comp[j];
int U = dsu.findSet(u);
int V = dsu.findSet(v);
if(U == V && p[u][v] != 1 || U != V && p[u][v] != 2) return 0;
}
}
for(int i = 0; i < (int)comp.size(); ++i) {
int u = comp[i];
for(int j = i + 1; j < (int)comp.size(); ++j) {
int v = comp[j];
int U = dsu.findSet(u);
int V = dsu.findSet(v);
if(U == V) g[u][v] = g[v][u] = w.join(u, v);
}
}
vector<int> cycle;
for(int v : comp) if(dsu.findSet(v) == v) cycle.push_back(v);
if((int)cycle.size() == 2) return 0;
if((int)cycle.size() > 1) {
for(int i = 0; i < (int)cycle.size(); ++i) {
int u = cycle[i];
int v = cycle[(i + 1) % cycle.size()];
g[u][v] = g[v][u] = 1;
}
}
++t;
}
build(g);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:82:15: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
82 | if(U == V && p[u][v] != 1 || U != V && p[u][v] != 2) return 0;
| ~~~~~~~^~~~~~~~~~~~~~~
# |
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 |
9 ms |
1228 KB |
Output is correct |
7 |
Correct |
211 ms |
23976 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 |
9 ms |
1228 KB |
Output is correct |
7 |
Correct |
211 ms |
23976 KB |
Output is correct |
8 |
Correct |
1 ms |
292 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
10 ms |
1200 KB |
Output is correct |
13 |
Correct |
213 ms |
23924 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
5 ms |
844 KB |
Output is correct |
17 |
Correct |
99 ms |
14196 KB |
Output is correct |
18 |
Correct |
1 ms |
292 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
48 ms |
6248 KB |
Output is correct |
21 |
Correct |
267 ms |
23968 KB |
Output is correct |
22 |
Correct |
214 ms |
23936 KB |
Output is correct |
23 |
Correct |
220 ms |
23928 KB |
Output is correct |
24 |
Correct |
204 ms |
24004 KB |
Output is correct |
25 |
Correct |
82 ms |
14064 KB |
Output is correct |
26 |
Correct |
79 ms |
14068 KB |
Output is correct |
27 |
Correct |
222 ms |
23980 KB |
Output is correct |
28 |
Correct |
189 ms |
23988 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 |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
9 ms |
1204 KB |
Output is correct |
9 |
Correct |
189 ms |
24012 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 |
1228 KB |
Output is correct |
13 |
Correct |
193 ms |
23912 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
4 ms |
844 KB |
Output is correct |
17 |
Correct |
91 ms |
14064 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
48 ms |
6164 KB |
Output is correct |
22 |
Correct |
210 ms |
23992 KB |
Output is correct |
23 |
Correct |
196 ms |
24004 KB |
Output is correct |
24 |
Correct |
206 ms |
24004 KB |
Output is correct |
25 |
Correct |
81 ms |
14112 KB |
Output is correct |
26 |
Correct |
97 ms |
14072 KB |
Output is correct |
27 |
Correct |
218 ms |
23928 KB |
Output is correct |
28 |
Correct |
208 ms |
24004 KB |
Output is correct |
29 |
Correct |
86 ms |
14020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
52 ms |
6288 KB |
Output is correct |
5 |
Correct |
251 ms |
23916 KB |
Output is correct |
6 |
Correct |
198 ms |
23940 KB |
Output is correct |
7 |
Correct |
207 ms |
24004 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
53 ms |
6328 KB |
Output is correct |
10 |
Correct |
191 ms |
24004 KB |
Output is correct |
11 |
Correct |
209 ms |
24024 KB |
Output is correct |
12 |
Correct |
205 ms |
23912 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 |
47 ms |
6212 KB |
Output is correct |
17 |
Correct |
192 ms |
24124 KB |
Output is correct |
18 |
Correct |
205 ms |
23928 KB |
Output is correct |
19 |
Correct |
245 ms |
24004 KB |
Output is correct |
20 |
Correct |
199 ms |
24028 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 |
9 ms |
1228 KB |
Output is correct |
7 |
Correct |
211 ms |
23976 KB |
Output is correct |
8 |
Correct |
1 ms |
292 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
10 ms |
1200 KB |
Output is correct |
13 |
Correct |
213 ms |
23924 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
5 ms |
844 KB |
Output is correct |
17 |
Correct |
99 ms |
14196 KB |
Output is correct |
18 |
Correct |
1 ms |
292 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
48 ms |
6248 KB |
Output is correct |
21 |
Correct |
267 ms |
23968 KB |
Output is correct |
22 |
Correct |
214 ms |
23936 KB |
Output is correct |
23 |
Correct |
220 ms |
23928 KB |
Output is correct |
24 |
Correct |
204 ms |
24004 KB |
Output is correct |
25 |
Correct |
82 ms |
14064 KB |
Output is correct |
26 |
Correct |
79 ms |
14068 KB |
Output is correct |
27 |
Correct |
222 ms |
23980 KB |
Output is correct |
28 |
Correct |
189 ms |
23988 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 |
0 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
9 ms |
1204 KB |
Output is correct |
37 |
Correct |
189 ms |
24012 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
1 ms |
204 KB |
Output is correct |
40 |
Correct |
10 ms |
1228 KB |
Output is correct |
41 |
Correct |
193 ms |
23912 KB |
Output is correct |
42 |
Correct |
1 ms |
204 KB |
Output is correct |
43 |
Correct |
0 ms |
204 KB |
Output is correct |
44 |
Correct |
4 ms |
844 KB |
Output is correct |
45 |
Correct |
91 ms |
14064 KB |
Output is correct |
46 |
Correct |
0 ms |
204 KB |
Output is correct |
47 |
Correct |
0 ms |
204 KB |
Output is correct |
48 |
Correct |
1 ms |
204 KB |
Output is correct |
49 |
Correct |
48 ms |
6164 KB |
Output is correct |
50 |
Correct |
210 ms |
23992 KB |
Output is correct |
51 |
Correct |
196 ms |
24004 KB |
Output is correct |
52 |
Correct |
206 ms |
24004 KB |
Output is correct |
53 |
Correct |
81 ms |
14112 KB |
Output is correct |
54 |
Correct |
97 ms |
14072 KB |
Output is correct |
55 |
Correct |
218 ms |
23928 KB |
Output is correct |
56 |
Correct |
208 ms |
24004 KB |
Output is correct |
57 |
Correct |
86 ms |
14020 KB |
Output is correct |
58 |
Correct |
0 ms |
204 KB |
Output is correct |
59 |
Correct |
0 ms |
204 KB |
Output is correct |
60 |
Correct |
4 ms |
844 KB |
Output is correct |
61 |
Correct |
94 ms |
14072 KB |
Output is correct |
62 |
Correct |
1 ms |
204 KB |
Output is correct |
63 |
Correct |
1 ms |
204 KB |
Output is correct |
64 |
Correct |
0 ms |
204 KB |
Output is correct |
65 |
Correct |
48 ms |
6212 KB |
Output is correct |
66 |
Correct |
93 ms |
14108 KB |
Output is correct |
67 |
Correct |
89 ms |
14104 KB |
Output is correct |
68 |
Correct |
75 ms |
14060 KB |
Output is correct |
69 |
Correct |
77 ms |
14068 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 |
9 ms |
1228 KB |
Output is correct |
7 |
Correct |
211 ms |
23976 KB |
Output is correct |
8 |
Correct |
1 ms |
292 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
10 ms |
1200 KB |
Output is correct |
13 |
Correct |
213 ms |
23924 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
5 ms |
844 KB |
Output is correct |
17 |
Correct |
99 ms |
14196 KB |
Output is correct |
18 |
Correct |
1 ms |
292 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
48 ms |
6248 KB |
Output is correct |
21 |
Correct |
267 ms |
23968 KB |
Output is correct |
22 |
Correct |
214 ms |
23936 KB |
Output is correct |
23 |
Correct |
220 ms |
23928 KB |
Output is correct |
24 |
Correct |
204 ms |
24004 KB |
Output is correct |
25 |
Correct |
82 ms |
14064 KB |
Output is correct |
26 |
Correct |
79 ms |
14068 KB |
Output is correct |
27 |
Correct |
222 ms |
23980 KB |
Output is correct |
28 |
Correct |
189 ms |
23988 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 |
0 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
9 ms |
1204 KB |
Output is correct |
37 |
Correct |
189 ms |
24012 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
1 ms |
204 KB |
Output is correct |
40 |
Correct |
10 ms |
1228 KB |
Output is correct |
41 |
Correct |
193 ms |
23912 KB |
Output is correct |
42 |
Correct |
1 ms |
204 KB |
Output is correct |
43 |
Correct |
0 ms |
204 KB |
Output is correct |
44 |
Correct |
4 ms |
844 KB |
Output is correct |
45 |
Correct |
91 ms |
14064 KB |
Output is correct |
46 |
Correct |
0 ms |
204 KB |
Output is correct |
47 |
Correct |
0 ms |
204 KB |
Output is correct |
48 |
Correct |
1 ms |
204 KB |
Output is correct |
49 |
Correct |
48 ms |
6164 KB |
Output is correct |
50 |
Correct |
210 ms |
23992 KB |
Output is correct |
51 |
Correct |
196 ms |
24004 KB |
Output is correct |
52 |
Correct |
206 ms |
24004 KB |
Output is correct |
53 |
Correct |
81 ms |
14112 KB |
Output is correct |
54 |
Correct |
97 ms |
14072 KB |
Output is correct |
55 |
Correct |
218 ms |
23928 KB |
Output is correct |
56 |
Correct |
208 ms |
24004 KB |
Output is correct |
57 |
Correct |
86 ms |
14020 KB |
Output is correct |
58 |
Correct |
1 ms |
204 KB |
Output is correct |
59 |
Correct |
0 ms |
204 KB |
Output is correct |
60 |
Correct |
1 ms |
204 KB |
Output is correct |
61 |
Correct |
52 ms |
6288 KB |
Output is correct |
62 |
Correct |
251 ms |
23916 KB |
Output is correct |
63 |
Correct |
198 ms |
23940 KB |
Output is correct |
64 |
Correct |
207 ms |
24004 KB |
Output is correct |
65 |
Correct |
0 ms |
204 KB |
Output is correct |
66 |
Correct |
53 ms |
6328 KB |
Output is correct |
67 |
Correct |
191 ms |
24004 KB |
Output is correct |
68 |
Correct |
209 ms |
24024 KB |
Output is correct |
69 |
Correct |
205 ms |
23912 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 |
47 ms |
6212 KB |
Output is correct |
74 |
Correct |
192 ms |
24124 KB |
Output is correct |
75 |
Correct |
205 ms |
23928 KB |
Output is correct |
76 |
Correct |
245 ms |
24004 KB |
Output is correct |
77 |
Correct |
199 ms |
24028 KB |
Output is correct |
78 |
Correct |
0 ms |
204 KB |
Output is correct |
79 |
Correct |
0 ms |
204 KB |
Output is correct |
80 |
Correct |
4 ms |
844 KB |
Output is correct |
81 |
Correct |
94 ms |
14072 KB |
Output is correct |
82 |
Correct |
1 ms |
204 KB |
Output is correct |
83 |
Correct |
1 ms |
204 KB |
Output is correct |
84 |
Correct |
0 ms |
204 KB |
Output is correct |
85 |
Correct |
48 ms |
6212 KB |
Output is correct |
86 |
Correct |
93 ms |
14108 KB |
Output is correct |
87 |
Correct |
89 ms |
14104 KB |
Output is correct |
88 |
Correct |
75 ms |
14060 KB |
Output is correct |
89 |
Correct |
77 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 |
4 ms |
660 KB |
Output is correct |
93 |
Correct |
83 ms |
10056 KB |
Output is correct |
94 |
Correct |
0 ms |
204 KB |
Output is correct |
95 |
Correct |
1 ms |
292 KB |
Output is correct |
96 |
Correct |
1 ms |
204 KB |
Output is correct |
97 |
Correct |
33 ms |
2656 KB |
Output is correct |
98 |
Correct |
81 ms |
10100 KB |
Output is correct |
99 |
Correct |
79 ms |
10100 KB |
Output is correct |
100 |
Correct |
82 ms |
10092 KB |
Output is correct |
101 |
Correct |
73 ms |
10104 KB |
Output is correct |
102 |
Correct |
72 ms |
10088 KB |
Output is correct |
103 |
Correct |
72 ms |
10108 KB |
Output is correct |
104 |
Correct |
72 ms |
10104 KB |
Output is correct |
105 |
Correct |
88 ms |
10088 KB |
Output is correct |