#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
const int INF = 5e8, N = 1e5+10, M = 998244353, LOG = 16;
const ll LINF = 1e18;
struct DSU {
vector<int> pr, sz;
DSU (int n) : pr(n), sz(n, 1) {
iota(all(pr), 0);
}
int find_par(int x) {
return pr[x] = (pr[x] == x ? x : find_par(pr[x]));
}
bool unite(int a, int b) {
a = find_par(a), b = find_par(b);
if(a == b) return false;
if(sz[a] < sz[b]) swap(a, b);
pr[b] = a;
sz[a] += sz[b];
return true;
}
};
int construct(vector<vector<int>> p) {
for(const auto& vi : p) if(count(all(vi), 3)) return 0;
int n = p.size();
vector<vector<int>> ans(n, vector<int>(n));
DSU sets(n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if(i != j and p[i][j] == 1 and sets.unite(i, j))
ans[i][j] = ans[j][i] = 1;
}
}
set<int> parents;
DSU cmp(n);
for(int i = 0; i < n; ++i)
parents.insert(sets.find_par(i));
while(!parents.empty()) {
vector<int> cur {*parents.begin()};
for(int x : parents)
if(p[cur[0]][x] == 2) {
cmp.unite(x, cur[0]);
cur.push_back(x);
}
if(cur.size() == 2) return 0;
for(int x : cur) parents.erase(x);
for(int i = 0; i < cur.size(); i++) {
int a = cur[i], b = cur[(i+1)%cur.size()];
if(a != b)
ans[a][b] = ans[b][a] = 1;
}
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
int x = sets.find_par(i), y = sets.find_par(j);
if(p[i][j] == 2)
if(x == y or cmp.find_par(x) != cmp.find_par(y))
return 0;
if(p[i][j] == 1 and x != y) return 0;
if(p[i][j] == 0 and cmp.find_par(x) == cmp.find_par(y))
return 0;
}
build(ans);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:66:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | for(int i = 0; i < cur.size(); i++) {
| ~~^~~~~~~~~~~~
# |
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 |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
9 ms |
1108 KB |
Output is correct |
7 |
Correct |
173 ms |
22004 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 |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
9 ms |
1108 KB |
Output is correct |
7 |
Correct |
173 ms |
22004 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
8 ms |
1188 KB |
Output is correct |
13 |
Correct |
186 ms |
22032 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
716 KB |
Output is correct |
17 |
Correct |
94 ms |
12036 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
44 ms |
5708 KB |
Output is correct |
21 |
Correct |
178 ms |
22088 KB |
Output is correct |
22 |
Correct |
180 ms |
22092 KB |
Output is correct |
23 |
Correct |
200 ms |
22148 KB |
Output is correct |
24 |
Correct |
167 ms |
21948 KB |
Output is correct |
25 |
Correct |
74 ms |
12048 KB |
Output is correct |
26 |
Correct |
69 ms |
12160 KB |
Output is correct |
27 |
Correct |
187 ms |
22024 KB |
Output is correct |
28 |
Correct |
181 ms |
22052 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 |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
296 KB |
Output is correct |
8 |
Correct |
8 ms |
1272 KB |
Output is correct |
9 |
Correct |
177 ms |
23932 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
300 KB |
Output is correct |
12 |
Correct |
8 ms |
1264 KB |
Output is correct |
13 |
Correct |
180 ms |
24004 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
852 KB |
Output is correct |
17 |
Correct |
80 ms |
14080 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
49 ms |
6200 KB |
Output is correct |
22 |
Correct |
180 ms |
23924 KB |
Output is correct |
23 |
Correct |
186 ms |
23924 KB |
Output is correct |
24 |
Correct |
188 ms |
23932 KB |
Output is correct |
25 |
Correct |
70 ms |
14116 KB |
Output is correct |
26 |
Correct |
71 ms |
14064 KB |
Output is correct |
27 |
Correct |
176 ms |
23936 KB |
Output is correct |
28 |
Correct |
195 ms |
23940 KB |
Output is correct |
29 |
Correct |
70 ms |
14080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
50 ms |
5752 KB |
Output is correct |
5 |
Correct |
192 ms |
22016 KB |
Output is correct |
6 |
Correct |
177 ms |
22060 KB |
Output is correct |
7 |
Correct |
190 ms |
22000 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
48 ms |
5764 KB |
Output is correct |
10 |
Correct |
180 ms |
22020 KB |
Output is correct |
11 |
Correct |
181 ms |
22028 KB |
Output is correct |
12 |
Correct |
194 ms |
22144 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 |
46 ms |
5736 KB |
Output is correct |
17 |
Correct |
174 ms |
22020 KB |
Output is correct |
18 |
Correct |
182 ms |
22016 KB |
Output is correct |
19 |
Correct |
186 ms |
22020 KB |
Output is correct |
20 |
Correct |
168 ms |
22032 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 |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
9 ms |
1108 KB |
Output is correct |
7 |
Correct |
173 ms |
22004 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
8 ms |
1188 KB |
Output is correct |
13 |
Correct |
186 ms |
22032 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
716 KB |
Output is correct |
17 |
Correct |
94 ms |
12036 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
44 ms |
5708 KB |
Output is correct |
21 |
Correct |
178 ms |
22088 KB |
Output is correct |
22 |
Correct |
180 ms |
22092 KB |
Output is correct |
23 |
Correct |
200 ms |
22148 KB |
Output is correct |
24 |
Correct |
167 ms |
21948 KB |
Output is correct |
25 |
Correct |
74 ms |
12048 KB |
Output is correct |
26 |
Correct |
69 ms |
12160 KB |
Output is correct |
27 |
Correct |
187 ms |
22024 KB |
Output is correct |
28 |
Correct |
181 ms |
22052 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
296 KB |
Output is correct |
36 |
Correct |
8 ms |
1272 KB |
Output is correct |
37 |
Correct |
177 ms |
23932 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
300 KB |
Output is correct |
40 |
Correct |
8 ms |
1264 KB |
Output is correct |
41 |
Correct |
180 ms |
24004 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
4 ms |
852 KB |
Output is correct |
45 |
Correct |
80 ms |
14080 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
49 ms |
6200 KB |
Output is correct |
50 |
Correct |
180 ms |
23924 KB |
Output is correct |
51 |
Correct |
186 ms |
23924 KB |
Output is correct |
52 |
Correct |
188 ms |
23932 KB |
Output is correct |
53 |
Correct |
70 ms |
14116 KB |
Output is correct |
54 |
Correct |
71 ms |
14064 KB |
Output is correct |
55 |
Correct |
176 ms |
23936 KB |
Output is correct |
56 |
Correct |
195 ms |
23940 KB |
Output is correct |
57 |
Correct |
70 ms |
14080 KB |
Output is correct |
58 |
Correct |
1 ms |
212 KB |
Output is correct |
59 |
Correct |
1 ms |
212 KB |
Output is correct |
60 |
Correct |
4 ms |
824 KB |
Output is correct |
61 |
Correct |
87 ms |
13992 KB |
Output is correct |
62 |
Correct |
1 ms |
300 KB |
Output is correct |
63 |
Correct |
1 ms |
296 KB |
Output is correct |
64 |
Correct |
1 ms |
212 KB |
Output is correct |
65 |
Correct |
52 ms |
6180 KB |
Output is correct |
66 |
Correct |
79 ms |
14116 KB |
Output is correct |
67 |
Correct |
71 ms |
14096 KB |
Output is correct |
68 |
Correct |
70 ms |
14128 KB |
Output is correct |
69 |
Correct |
72 ms |
14208 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 |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
9 ms |
1108 KB |
Output is correct |
7 |
Correct |
173 ms |
22004 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
8 ms |
1188 KB |
Output is correct |
13 |
Correct |
186 ms |
22032 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
716 KB |
Output is correct |
17 |
Correct |
94 ms |
12036 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
44 ms |
5708 KB |
Output is correct |
21 |
Correct |
178 ms |
22088 KB |
Output is correct |
22 |
Correct |
180 ms |
22092 KB |
Output is correct |
23 |
Correct |
200 ms |
22148 KB |
Output is correct |
24 |
Correct |
167 ms |
21948 KB |
Output is correct |
25 |
Correct |
74 ms |
12048 KB |
Output is correct |
26 |
Correct |
69 ms |
12160 KB |
Output is correct |
27 |
Correct |
187 ms |
22024 KB |
Output is correct |
28 |
Correct |
181 ms |
22052 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
296 KB |
Output is correct |
36 |
Correct |
8 ms |
1272 KB |
Output is correct |
37 |
Correct |
177 ms |
23932 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
300 KB |
Output is correct |
40 |
Correct |
8 ms |
1264 KB |
Output is correct |
41 |
Correct |
180 ms |
24004 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
4 ms |
852 KB |
Output is correct |
45 |
Correct |
80 ms |
14080 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
49 ms |
6200 KB |
Output is correct |
50 |
Correct |
180 ms |
23924 KB |
Output is correct |
51 |
Correct |
186 ms |
23924 KB |
Output is correct |
52 |
Correct |
188 ms |
23932 KB |
Output is correct |
53 |
Correct |
70 ms |
14116 KB |
Output is correct |
54 |
Correct |
71 ms |
14064 KB |
Output is correct |
55 |
Correct |
176 ms |
23936 KB |
Output is correct |
56 |
Correct |
195 ms |
23940 KB |
Output is correct |
57 |
Correct |
70 ms |
14080 KB |
Output is correct |
58 |
Correct |
1 ms |
212 KB |
Output is correct |
59 |
Correct |
0 ms |
212 KB |
Output is correct |
60 |
Correct |
0 ms |
212 KB |
Output is correct |
61 |
Correct |
50 ms |
5752 KB |
Output is correct |
62 |
Correct |
192 ms |
22016 KB |
Output is correct |
63 |
Correct |
177 ms |
22060 KB |
Output is correct |
64 |
Correct |
190 ms |
22000 KB |
Output is correct |
65 |
Correct |
0 ms |
212 KB |
Output is correct |
66 |
Correct |
48 ms |
5764 KB |
Output is correct |
67 |
Correct |
180 ms |
22020 KB |
Output is correct |
68 |
Correct |
181 ms |
22028 KB |
Output is correct |
69 |
Correct |
194 ms |
22144 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 |
46 ms |
5736 KB |
Output is correct |
74 |
Correct |
174 ms |
22020 KB |
Output is correct |
75 |
Correct |
182 ms |
22016 KB |
Output is correct |
76 |
Correct |
186 ms |
22020 KB |
Output is correct |
77 |
Correct |
168 ms |
22032 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
1 ms |
212 KB |
Output is correct |
80 |
Correct |
4 ms |
824 KB |
Output is correct |
81 |
Correct |
87 ms |
13992 KB |
Output is correct |
82 |
Correct |
1 ms |
300 KB |
Output is correct |
83 |
Correct |
1 ms |
296 KB |
Output is correct |
84 |
Correct |
1 ms |
212 KB |
Output is correct |
85 |
Correct |
52 ms |
6180 KB |
Output is correct |
86 |
Correct |
79 ms |
14116 KB |
Output is correct |
87 |
Correct |
71 ms |
14096 KB |
Output is correct |
88 |
Correct |
70 ms |
14128 KB |
Output is correct |
89 |
Correct |
72 ms |
14208 KB |
Output is correct |
90 |
Correct |
1 ms |
300 KB |
Output is correct |
91 |
Correct |
1 ms |
300 KB |
Output is correct |
92 |
Correct |
4 ms |
696 KB |
Output is correct |
93 |
Correct |
72 ms |
10040 KB |
Output is correct |
94 |
Correct |
0 ms |
212 KB |
Output is correct |
95 |
Correct |
1 ms |
212 KB |
Output is correct |
96 |
Correct |
1 ms |
212 KB |
Output is correct |
97 |
Correct |
16 ms |
2688 KB |
Output is correct |
98 |
Correct |
71 ms |
10088 KB |
Output is correct |
99 |
Correct |
69 ms |
10112 KB |
Output is correct |
100 |
Correct |
74 ms |
10096 KB |
Output is correct |
101 |
Correct |
66 ms |
10108 KB |
Output is correct |
102 |
Correct |
82 ms |
10112 KB |
Output is correct |
103 |
Correct |
62 ms |
10096 KB |
Output is correct |
104 |
Correct |
65 ms |
10108 KB |
Output is correct |
105 |
Correct |
75 ms |
10116 KB |
Output is correct |