#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
int N, fa[maxn], sz[maxn];
bool ok = true;
int find(int u) {
return (fa[u] == u) ? u : fa[u] = find(fa[u]);
}
bool merge(int u, int v) {
u = find(u), v = find(v);
if (u == v) return false;
if (sz[v] > sz[u]) swap(u, v);
fa[v] = u;
sz[u] += sz[v];
return true;
}
bool vis[maxn];
vector<int> edits;
void dfs(int u, vector<vector<int>>& ans, vector<vector<int>>& p) {
vis[u] = true;
edits.push_back(u);
for (int i = 0; i < N; i++) {
if (!vis[i] && p[u][i] == 1 && find(u) == find(i)) {
ans[u][i] = ans[i][u] = 1;
dfs(i, ans, p);
}
}
}
void process(vector<int> v, vector<vector<int>>& ans, vector<vector<int>>& p) {
if ((int) v.size() == 1) return;
// for (int i = 0; i < v.size(); i++) cout << v[i] << endl;
int n = v.size(), cnt = 0;
vector<int> cyc;
vector<vector<int>> seen(N);
vector<bool> in_cyc(N, 0);
bool two = false;
for (int i : v) {
vector<bool> b(3, 0);
for (int j = 0; j < N; j++) {
if (i == j) continue;
b[p[i][j]] = true;
// cout << i << ' ' << j << ' ' << p[i][j] << '\n';
}
for (int j = 0; j < N; j++) {
if (p[i][j] == 1) merge(i, j);
}
if (b[2] && !b[1]) {
cyc.push_back(i);
// cout << "in cycle!" << ' ' << i << endl;
in_cyc[i] = true;
}
two = (two | b[2]);
}
// cout << "alive\n";
for (int i = 0; i < n; i++) if (!in_cyc[v[i]]) {
seen[find(v[i])].push_back(v[i]);
// cout << "corresponding find: " << v[i] << ' ' << find(v[i]) << '\n';
}
// cout << "alive\n";
for (int i = 0; i < N; i++) {
if (seen[i].empty()) continue;
dfs(seen[i].front(), ans, p);
cyc.push_back(seen[i].front());
in_cyc[seen[i].front()] = true;
seen[find(seen[i].front())].push_back(seen[i].front());
for (int j : edits) vis[j] = 0;
}
if (two && ((int) cyc.size() == 1 || (int) cyc.size() == 2)) {
ok = false;
return;
}
for (int i = 0; i < N; i++) if (!seen[i].empty()) cnt++;
if (cnt > (int) cyc.size()) {
ok = false;
return;
}
for (int i = 0; i < cyc.size(); i++) {
int nxt = (i + 1) % cyc.size();
ans[cyc[i]][cyc[nxt]] = ans[cyc[nxt]][cyc[i]] = 1;
}
}
int construct(std::vector<std::vector<int>> p) {
int n = p[0].size();
N = n;
vector<vector<int>> ans(n, vector<int> (n, 0));
for (int i = 0; i < n; i++) {
fa[i] = i, sz[i] = 1;
for (int j = 0; j < n; j++) {
if (p[i][j] >= 3) return 0;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (p[i][j]) merge(i, j);
}
}
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) {
if (!p[i][j] && find(i) == find(j)) return 0;
}
vector<vector<int>> g(n);
for (int i = 0; i < n; i++) g[find(i)].push_back(i);
for (int i = 0; i < n; i++) fa[i] = i, sz[i] = 1;
for (int i = 0; i < n; i++) {
if (!g[i].empty()) {
process(g[i], ans, p);
if (!ok) return 0;
}
}
for (int i = 0; i < n; i++) ans[i][i] = 0;
build(ans);
return 1;
}
Compilation message
supertrees.cpp: In function 'void process(std::vector<int>, std::vector<std::vector<int> >&, std::vector<std::vector<int> >&)':
supertrees.cpp:84:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for (int i = 0; i < cyc.size(); i++) {
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1176 KB |
Output is correct |
7 |
Correct |
163 ms |
22036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1176 KB |
Output is correct |
7 |
Correct |
163 ms |
22036 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 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 |
7 ms |
1148 KB |
Output is correct |
13 |
Correct |
151 ms |
21988 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
3 ms |
724 KB |
Output is correct |
17 |
Correct |
76 ms |
12040 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
40 ms |
5724 KB |
Output is correct |
21 |
Correct |
162 ms |
23596 KB |
Output is correct |
22 |
Correct |
168 ms |
23544 KB |
Output is correct |
23 |
Correct |
179 ms |
23608 KB |
Output is correct |
24 |
Correct |
156 ms |
23572 KB |
Output is correct |
25 |
Correct |
66 ms |
13672 KB |
Output is correct |
26 |
Correct |
70 ms |
13620 KB |
Output is correct |
27 |
Correct |
176 ms |
23660 KB |
Output is correct |
28 |
Correct |
220 ms |
23516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
6 ms |
1200 KB |
Output is correct |
9 |
Correct |
152 ms |
22008 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 |
1104 KB |
Output is correct |
13 |
Correct |
179 ms |
22024 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
3 ms |
724 KB |
Output is correct |
17 |
Correct |
71 ms |
12040 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
39 ms |
5772 KB |
Output is correct |
22 |
Correct |
196 ms |
23640 KB |
Output is correct |
23 |
Correct |
172 ms |
23552 KB |
Output is correct |
24 |
Correct |
165 ms |
23712 KB |
Output is correct |
25 |
Correct |
63 ms |
14084 KB |
Output is correct |
26 |
Correct |
62 ms |
14028 KB |
Output is correct |
27 |
Correct |
169 ms |
23552 KB |
Output is correct |
28 |
Correct |
165 ms |
23512 KB |
Output is correct |
29 |
Correct |
64 ms |
14088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
39 ms |
5676 KB |
Output is correct |
5 |
Correct |
166 ms |
23528 KB |
Output is correct |
6 |
Correct |
171 ms |
23592 KB |
Output is correct |
7 |
Correct |
167 ms |
23616 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
52 ms |
6264 KB |
Output is correct |
10 |
Correct |
159 ms |
23628 KB |
Output is correct |
11 |
Correct |
169 ms |
23572 KB |
Output is correct |
12 |
Correct |
164 ms |
23536 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
304 KB |
Output is correct |
16 |
Correct |
42 ms |
6244 KB |
Output is correct |
17 |
Correct |
185 ms |
23556 KB |
Output is correct |
18 |
Correct |
175 ms |
23616 KB |
Output is correct |
19 |
Correct |
183 ms |
23564 KB |
Output is correct |
20 |
Correct |
152 ms |
23632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1176 KB |
Output is correct |
7 |
Correct |
163 ms |
22036 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 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 |
7 ms |
1148 KB |
Output is correct |
13 |
Correct |
151 ms |
21988 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
3 ms |
724 KB |
Output is correct |
17 |
Correct |
76 ms |
12040 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
40 ms |
5724 KB |
Output is correct |
21 |
Correct |
162 ms |
23596 KB |
Output is correct |
22 |
Correct |
168 ms |
23544 KB |
Output is correct |
23 |
Correct |
179 ms |
23608 KB |
Output is correct |
24 |
Correct |
156 ms |
23572 KB |
Output is correct |
25 |
Correct |
66 ms |
13672 KB |
Output is correct |
26 |
Correct |
70 ms |
13620 KB |
Output is correct |
27 |
Correct |
176 ms |
23660 KB |
Output is correct |
28 |
Correct |
220 ms |
23516 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
6 ms |
1200 KB |
Output is correct |
37 |
Correct |
152 ms |
22008 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
7 ms |
1104 KB |
Output is correct |
41 |
Correct |
179 ms |
22024 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
3 ms |
724 KB |
Output is correct |
45 |
Correct |
71 ms |
12040 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
39 ms |
5772 KB |
Output is correct |
50 |
Correct |
196 ms |
23640 KB |
Output is correct |
51 |
Correct |
172 ms |
23552 KB |
Output is correct |
52 |
Correct |
165 ms |
23712 KB |
Output is correct |
53 |
Correct |
63 ms |
14084 KB |
Output is correct |
54 |
Correct |
62 ms |
14028 KB |
Output is correct |
55 |
Correct |
169 ms |
23552 KB |
Output is correct |
56 |
Correct |
165 ms |
23512 KB |
Output is correct |
57 |
Correct |
64 ms |
14088 KB |
Output is correct |
58 |
Correct |
1 ms |
212 KB |
Output is correct |
59 |
Correct |
0 ms |
308 KB |
Output is correct |
60 |
Correct |
3 ms |
852 KB |
Output is correct |
61 |
Correct |
94 ms |
14096 KB |
Output is correct |
62 |
Correct |
0 ms |
212 KB |
Output is correct |
63 |
Correct |
0 ms |
212 KB |
Output is correct |
64 |
Correct |
1 ms |
212 KB |
Output is correct |
65 |
Correct |
41 ms |
6180 KB |
Output is correct |
66 |
Correct |
85 ms |
14068 KB |
Output is correct |
67 |
Correct |
62 ms |
14068 KB |
Output is correct |
68 |
Correct |
63 ms |
14080 KB |
Output is correct |
69 |
Correct |
68 ms |
14084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1176 KB |
Output is correct |
7 |
Correct |
163 ms |
22036 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 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 |
7 ms |
1148 KB |
Output is correct |
13 |
Correct |
151 ms |
21988 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
3 ms |
724 KB |
Output is correct |
17 |
Correct |
76 ms |
12040 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
40 ms |
5724 KB |
Output is correct |
21 |
Correct |
162 ms |
23596 KB |
Output is correct |
22 |
Correct |
168 ms |
23544 KB |
Output is correct |
23 |
Correct |
179 ms |
23608 KB |
Output is correct |
24 |
Correct |
156 ms |
23572 KB |
Output is correct |
25 |
Correct |
66 ms |
13672 KB |
Output is correct |
26 |
Correct |
70 ms |
13620 KB |
Output is correct |
27 |
Correct |
176 ms |
23660 KB |
Output is correct |
28 |
Correct |
220 ms |
23516 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
6 ms |
1200 KB |
Output is correct |
37 |
Correct |
152 ms |
22008 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
7 ms |
1104 KB |
Output is correct |
41 |
Correct |
179 ms |
22024 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
3 ms |
724 KB |
Output is correct |
45 |
Correct |
71 ms |
12040 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
39 ms |
5772 KB |
Output is correct |
50 |
Correct |
196 ms |
23640 KB |
Output is correct |
51 |
Correct |
172 ms |
23552 KB |
Output is correct |
52 |
Correct |
165 ms |
23712 KB |
Output is correct |
53 |
Correct |
63 ms |
14084 KB |
Output is correct |
54 |
Correct |
62 ms |
14028 KB |
Output is correct |
55 |
Correct |
169 ms |
23552 KB |
Output is correct |
56 |
Correct |
165 ms |
23512 KB |
Output is correct |
57 |
Correct |
64 ms |
14088 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 |
212 KB |
Output is correct |
61 |
Correct |
39 ms |
5676 KB |
Output is correct |
62 |
Correct |
166 ms |
23528 KB |
Output is correct |
63 |
Correct |
171 ms |
23592 KB |
Output is correct |
64 |
Correct |
167 ms |
23616 KB |
Output is correct |
65 |
Correct |
1 ms |
212 KB |
Output is correct |
66 |
Correct |
52 ms |
6264 KB |
Output is correct |
67 |
Correct |
159 ms |
23628 KB |
Output is correct |
68 |
Correct |
169 ms |
23572 KB |
Output is correct |
69 |
Correct |
164 ms |
23536 KB |
Output is correct |
70 |
Correct |
1 ms |
212 KB |
Output is correct |
71 |
Correct |
1 ms |
212 KB |
Output is correct |
72 |
Correct |
1 ms |
304 KB |
Output is correct |
73 |
Correct |
42 ms |
6244 KB |
Output is correct |
74 |
Correct |
185 ms |
23556 KB |
Output is correct |
75 |
Correct |
175 ms |
23616 KB |
Output is correct |
76 |
Correct |
183 ms |
23564 KB |
Output is correct |
77 |
Correct |
152 ms |
23632 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
0 ms |
308 KB |
Output is correct |
80 |
Correct |
3 ms |
852 KB |
Output is correct |
81 |
Correct |
94 ms |
14096 KB |
Output is correct |
82 |
Correct |
0 ms |
212 KB |
Output is correct |
83 |
Correct |
0 ms |
212 KB |
Output is correct |
84 |
Correct |
1 ms |
212 KB |
Output is correct |
85 |
Correct |
41 ms |
6180 KB |
Output is correct |
86 |
Correct |
85 ms |
14068 KB |
Output is correct |
87 |
Correct |
62 ms |
14068 KB |
Output is correct |
88 |
Correct |
63 ms |
14080 KB |
Output is correct |
89 |
Correct |
68 ms |
14084 KB |
Output is correct |
90 |
Correct |
1 ms |
308 KB |
Output is correct |
91 |
Correct |
1 ms |
212 KB |
Output is correct |
92 |
Correct |
5 ms |
800 KB |
Output is correct |
93 |
Correct |
75 ms |
14080 KB |
Output is correct |
94 |
Correct |
1 ms |
212 KB |
Output is correct |
95 |
Correct |
0 ms |
300 KB |
Output is correct |
96 |
Correct |
1 ms |
304 KB |
Output is correct |
97 |
Correct |
15 ms |
3668 KB |
Output is correct |
98 |
Correct |
60 ms |
13996 KB |
Output is correct |
99 |
Correct |
69 ms |
14072 KB |
Output is correct |
100 |
Correct |
62 ms |
14084 KB |
Output is correct |
101 |
Correct |
59 ms |
14072 KB |
Output is correct |
102 |
Correct |
61 ms |
14068 KB |
Output is correct |
103 |
Correct |
62 ms |
14068 KB |
Output is correct |
104 |
Correct |
84 ms |
14072 KB |
Output is correct |
105 |
Correct |
64 ms |
14076 KB |
Output is correct |