#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
const int nax = 1000;
int par[nax], sz[nax];
vector<int>L[nax];
bool done[nax];
int find(int v) {
return par[v] = (v == par[v]) ? v : find(par[v]);
}
void unite(int u, int v) {
u = find(u), v = find(v);
if(u==v) return;
if(sz[u] < sz[v]) swap(u, v);
for(int x : L[v]) {
L[u].push_back(x);
}
sz[u] += sz[v];
par[v] = u;
}
int construct(vector<vector<int>> p) {
int n = p.size();
for(int i=0; i<n; ++i) {
par[i] = i, sz[i] = 1;
L[i] = {i};
}
for(int i=0; i<n; ++i) {
for(int j=0; j<n; ++j) if(p[i][j]) {
unite(i, j);
}
}
for(int i=0; i<n; ++i) {
for(int j=0; j<n; ++j) if(!p[i][j]) {
if(find(i) == find(j)) return 0;
}
}
vector<vector<int>>ans(n, vector<int>(n));
for(int i=0; i<n; ++i) if(i == find(i)) {
vector<int>heads;
for(int v : L[i]) if(!done[v]) {
done[v] = true;
set<int>curL;
for(int u=0; u<n; ++u) {
if(p[u][v]==1 && u!=v) {
ans[u][v] = ans[v][u] = 1;
done[u] = true;
curL.insert(u);
}
}
for(int x : curL) {
for(int y : curL) {
if(p[x][y]!=1) return 0;
}
}
curL.insert(v);
for(int x : L[i]) {
for(int y : curL) {
if(!curL.count(x)) {
if(p[x][y]!=2) return 0;
}
}
}
heads.push_back(v);
}
if(heads.size()==2) return 0;
if(heads.size()==1) continue;
for(int i=0; i<heads.size(); ++i) {
int v = heads[i];
int u = heads[(i+1)%heads.size()];
ans[u][v] = ans[v][u] = 1;
}
}
build(ans);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:83:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | for(int i=0; i<heads.size(); ++i) {
| ~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
8 ms |
1180 KB |
Output is correct |
7 |
Correct |
182 ms |
22012 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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
8 ms |
1180 KB |
Output is correct |
7 |
Correct |
182 ms |
22012 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 |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
1208 KB |
Output is correct |
13 |
Correct |
157 ms |
22048 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
3 ms |
724 KB |
Output is correct |
17 |
Correct |
76 ms |
10128 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
43 ms |
6256 KB |
Output is correct |
21 |
Correct |
163 ms |
24060 KB |
Output is correct |
22 |
Correct |
162 ms |
24080 KB |
Output is correct |
23 |
Correct |
179 ms |
24212 KB |
Output is correct |
24 |
Correct |
159 ms |
24036 KB |
Output is correct |
25 |
Correct |
66 ms |
10136 KB |
Output is correct |
26 |
Correct |
72 ms |
10188 KB |
Output is correct |
27 |
Correct |
173 ms |
24076 KB |
Output is correct |
28 |
Correct |
158 ms |
24012 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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
7 ms |
1232 KB |
Output is correct |
9 |
Correct |
160 ms |
23952 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 |
1212 KB |
Output is correct |
13 |
Correct |
170 ms |
23964 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
3 ms |
724 KB |
Output is correct |
17 |
Correct |
75 ms |
10124 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
42 ms |
6288 KB |
Output is correct |
22 |
Correct |
162 ms |
23980 KB |
Output is correct |
23 |
Correct |
164 ms |
23968 KB |
Output is correct |
24 |
Correct |
173 ms |
24012 KB |
Output is correct |
25 |
Correct |
68 ms |
14108 KB |
Output is correct |
26 |
Correct |
65 ms |
10176 KB |
Output is correct |
27 |
Correct |
162 ms |
23948 KB |
Output is correct |
28 |
Correct |
171 ms |
23996 KB |
Output is correct |
29 |
Correct |
70 ms |
14092 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 |
40 ms |
5788 KB |
Output is correct |
5 |
Correct |
164 ms |
22172 KB |
Output is correct |
6 |
Correct |
160 ms |
22092 KB |
Output is correct |
7 |
Correct |
172 ms |
22036 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
40 ms |
5756 KB |
Output is correct |
10 |
Correct |
161 ms |
22060 KB |
Output is correct |
11 |
Correct |
163 ms |
22060 KB |
Output is correct |
12 |
Correct |
168 ms |
22048 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
40 ms |
5800 KB |
Output is correct |
17 |
Correct |
161 ms |
22044 KB |
Output is correct |
18 |
Correct |
160 ms |
22060 KB |
Output is correct |
19 |
Correct |
158 ms |
22060 KB |
Output is correct |
20 |
Correct |
159 ms |
22060 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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
8 ms |
1180 KB |
Output is correct |
7 |
Correct |
182 ms |
22012 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 |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
1208 KB |
Output is correct |
13 |
Correct |
157 ms |
22048 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
3 ms |
724 KB |
Output is correct |
17 |
Correct |
76 ms |
10128 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
43 ms |
6256 KB |
Output is correct |
21 |
Correct |
163 ms |
24060 KB |
Output is correct |
22 |
Correct |
162 ms |
24080 KB |
Output is correct |
23 |
Correct |
179 ms |
24212 KB |
Output is correct |
24 |
Correct |
159 ms |
24036 KB |
Output is correct |
25 |
Correct |
66 ms |
10136 KB |
Output is correct |
26 |
Correct |
72 ms |
10188 KB |
Output is correct |
27 |
Correct |
173 ms |
24076 KB |
Output is correct |
28 |
Correct |
158 ms |
24012 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 |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
7 ms |
1232 KB |
Output is correct |
37 |
Correct |
160 ms |
23952 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 |
1212 KB |
Output is correct |
41 |
Correct |
170 ms |
23964 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
0 ms |
212 KB |
Output is correct |
44 |
Correct |
3 ms |
724 KB |
Output is correct |
45 |
Correct |
75 ms |
10124 KB |
Output is correct |
46 |
Correct |
0 ms |
332 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
0 ms |
212 KB |
Output is correct |
49 |
Correct |
42 ms |
6288 KB |
Output is correct |
50 |
Correct |
162 ms |
23980 KB |
Output is correct |
51 |
Correct |
164 ms |
23968 KB |
Output is correct |
52 |
Correct |
173 ms |
24012 KB |
Output is correct |
53 |
Correct |
68 ms |
14108 KB |
Output is correct |
54 |
Correct |
65 ms |
10176 KB |
Output is correct |
55 |
Correct |
162 ms |
23948 KB |
Output is correct |
56 |
Correct |
171 ms |
23996 KB |
Output is correct |
57 |
Correct |
70 ms |
14092 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
1 ms |
212 KB |
Output is correct |
60 |
Correct |
3 ms |
696 KB |
Output is correct |
61 |
Correct |
78 ms |
10136 KB |
Output is correct |
62 |
Correct |
0 ms |
212 KB |
Output is correct |
63 |
Correct |
0 ms |
340 KB |
Output is correct |
64 |
Correct |
0 ms |
332 KB |
Output is correct |
65 |
Correct |
41 ms |
6216 KB |
Output is correct |
66 |
Correct |
66 ms |
10184 KB |
Output is correct |
67 |
Correct |
66 ms |
10132 KB |
Output is correct |
68 |
Correct |
67 ms |
10132 KB |
Output is correct |
69 |
Correct |
68 ms |
14100 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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
8 ms |
1180 KB |
Output is correct |
7 |
Correct |
182 ms |
22012 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 |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
1208 KB |
Output is correct |
13 |
Correct |
157 ms |
22048 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
3 ms |
724 KB |
Output is correct |
17 |
Correct |
76 ms |
10128 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
43 ms |
6256 KB |
Output is correct |
21 |
Correct |
163 ms |
24060 KB |
Output is correct |
22 |
Correct |
162 ms |
24080 KB |
Output is correct |
23 |
Correct |
179 ms |
24212 KB |
Output is correct |
24 |
Correct |
159 ms |
24036 KB |
Output is correct |
25 |
Correct |
66 ms |
10136 KB |
Output is correct |
26 |
Correct |
72 ms |
10188 KB |
Output is correct |
27 |
Correct |
173 ms |
24076 KB |
Output is correct |
28 |
Correct |
158 ms |
24012 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 |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
7 ms |
1232 KB |
Output is correct |
37 |
Correct |
160 ms |
23952 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 |
1212 KB |
Output is correct |
41 |
Correct |
170 ms |
23964 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
0 ms |
212 KB |
Output is correct |
44 |
Correct |
3 ms |
724 KB |
Output is correct |
45 |
Correct |
75 ms |
10124 KB |
Output is correct |
46 |
Correct |
0 ms |
332 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
0 ms |
212 KB |
Output is correct |
49 |
Correct |
42 ms |
6288 KB |
Output is correct |
50 |
Correct |
162 ms |
23980 KB |
Output is correct |
51 |
Correct |
164 ms |
23968 KB |
Output is correct |
52 |
Correct |
173 ms |
24012 KB |
Output is correct |
53 |
Correct |
68 ms |
14108 KB |
Output is correct |
54 |
Correct |
65 ms |
10176 KB |
Output is correct |
55 |
Correct |
162 ms |
23948 KB |
Output is correct |
56 |
Correct |
171 ms |
23996 KB |
Output is correct |
57 |
Correct |
70 ms |
14092 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 |
40 ms |
5788 KB |
Output is correct |
62 |
Correct |
164 ms |
22172 KB |
Output is correct |
63 |
Correct |
160 ms |
22092 KB |
Output is correct |
64 |
Correct |
172 ms |
22036 KB |
Output is correct |
65 |
Correct |
0 ms |
212 KB |
Output is correct |
66 |
Correct |
40 ms |
5756 KB |
Output is correct |
67 |
Correct |
161 ms |
22060 KB |
Output is correct |
68 |
Correct |
163 ms |
22060 KB |
Output is correct |
69 |
Correct |
168 ms |
22048 KB |
Output is correct |
70 |
Correct |
0 ms |
212 KB |
Output is correct |
71 |
Correct |
0 ms |
212 KB |
Output is correct |
72 |
Correct |
1 ms |
212 KB |
Output is correct |
73 |
Correct |
40 ms |
5800 KB |
Output is correct |
74 |
Correct |
161 ms |
22044 KB |
Output is correct |
75 |
Correct |
160 ms |
22060 KB |
Output is correct |
76 |
Correct |
158 ms |
22060 KB |
Output is correct |
77 |
Correct |
159 ms |
22060 KB |
Output is correct |
78 |
Correct |
1 ms |
340 KB |
Output is correct |
79 |
Correct |
1 ms |
212 KB |
Output is correct |
80 |
Correct |
3 ms |
696 KB |
Output is correct |
81 |
Correct |
78 ms |
10136 KB |
Output is correct |
82 |
Correct |
0 ms |
212 KB |
Output is correct |
83 |
Correct |
0 ms |
340 KB |
Output is correct |
84 |
Correct |
0 ms |
332 KB |
Output is correct |
85 |
Correct |
41 ms |
6216 KB |
Output is correct |
86 |
Correct |
66 ms |
10184 KB |
Output is correct |
87 |
Correct |
66 ms |
10132 KB |
Output is correct |
88 |
Correct |
67 ms |
10132 KB |
Output is correct |
89 |
Correct |
68 ms |
14100 KB |
Output is correct |
90 |
Correct |
0 ms |
212 KB |
Output is correct |
91 |
Correct |
0 ms |
212 KB |
Output is correct |
92 |
Correct |
3 ms |
724 KB |
Output is correct |
93 |
Correct |
73 ms |
10132 KB |
Output is correct |
94 |
Correct |
1 ms |
340 KB |
Output is correct |
95 |
Correct |
0 ms |
212 KB |
Output is correct |
96 |
Correct |
0 ms |
212 KB |
Output is correct |
97 |
Correct |
16 ms |
2772 KB |
Output is correct |
98 |
Correct |
66 ms |
10140 KB |
Output is correct |
99 |
Correct |
65 ms |
10188 KB |
Output is correct |
100 |
Correct |
66 ms |
10148 KB |
Output is correct |
101 |
Correct |
70 ms |
14112 KB |
Output is correct |
102 |
Correct |
73 ms |
14100 KB |
Output is correct |
103 |
Correct |
73 ms |
14156 KB |
Output is correct |
104 |
Correct |
73 ms |
14180 KB |
Output is correct |
105 |
Correct |
74 ms |
10128 KB |
Output is correct |