#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;
struct DSU {
int n, comp;
vector<int> par, size;
void config(int _n) {
n = _n + 1;
comp = _n;
par.resize(n+1);
size.resize(n+1, 1);
for(int i=1; i<=n; i++) par[i] = i;
}
int get(int u) {
if(u == par[u]) return u;
return par[u] = get(par[u]);
}
bool uni(int u, int v) {
u = get(u), v = get(v);
if(u == v) return false;
if(size[u] < size[v]) swap(u, v);
size[u] += size[v];
par[v] = u;
comp--;
return true;
}
int getComp() { return comp; }
int getSize(int u) { return size[get(u)]; }
bool sameSet(int u, int v) { return get(u) == get(v); }
bool isLeader(int u) { return get(u) == u; }
};
vector<vector<int> > graph;
vector<bool> vis;
vector<int> G;
void dfs(int u) {
vis[u] = 1;
G.push_back(u);
for(int &v : graph[u]) {
if(vis[v]) continue;
dfs(v);
}
}
int construct(vector<vector<int> > p) {
int n = p.size();
vector<vector<int> > B(n, vector<int>(n, 0));
graph.resize(n);
vis.resize(n);
DSU dsu; dsu.config(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 && i != j) {
if(!dsu.sameSet(i, j)) {
dsu.uni(i, j);
B[i][j] = B[j][i] = 1;
}
}
}
}
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
if(p[i][j] == 1 && !dsu.sameSet(i, j)) return 0;
if(p[i][j] == 0 && dsu.sameSet(i, j)) return 0;
}
}
bool ok = 0;
for(int i=0; i<n; i++) {
for(int j=i+1; j<n; j++) {
if(p[i][j] == 2 && dsu.isLeader(i) && dsu.isLeader(j)) {
ok = 1;
graph[i].push_back(j);
graph[j].push_back(i);
}
}
}
for(int i=0; i<n && ok; i++) {
if(vis[i]) continue;
G.clear();
dfs(i);
if(G.size() == 2) return 0;
if(G.size() == 1) continue;
for(int i=0; i<G.size()-1; i++)
for(int j=i+1; j<G.size(); j++)
if(p[G[i]][G[j]] != 2) return 0;
G.push_back(G[0]);
for(int i=0; i<G.size()-1; i++)
B[G[i]][G[i+1]] = B[G[i+1]][G[i]] = 1;
}
build(B);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:103:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
103 | for(int i=0; i<G.size()-1; i++)
| ~^~~~~~~~~~~
supertrees.cpp:104:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
104 | for(int j=i+1; j<G.size(); j++)
| ~^~~~~~~~~
supertrees.cpp:108:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
108 | for(int i=0; i<G.size()-1; i++)
| ~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
1116 KB |
Output is correct |
7 |
Correct |
170 ms |
22096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
1116 KB |
Output is correct |
7 |
Correct |
170 ms |
22096 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
7 ms |
1176 KB |
Output is correct |
13 |
Correct |
163 ms |
22084 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
852 KB |
Output is correct |
17 |
Correct |
87 ms |
12296 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
40 ms |
5884 KB |
Output is correct |
21 |
Correct |
160 ms |
22032 KB |
Output is correct |
22 |
Correct |
157 ms |
22072 KB |
Output is correct |
23 |
Correct |
181 ms |
22020 KB |
Output is correct |
24 |
Correct |
199 ms |
22064 KB |
Output is correct |
25 |
Correct |
66 ms |
12292 KB |
Output is correct |
26 |
Correct |
60 ms |
12116 KB |
Output is correct |
27 |
Correct |
167 ms |
22148 KB |
Output is correct |
28 |
Correct |
163 ms |
22100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
436 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
6 ms |
1116 KB |
Output is correct |
9 |
Correct |
161 ms |
22156 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
7 ms |
1372 KB |
Output is correct |
13 |
Correct |
171 ms |
26196 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
4 ms |
860 KB |
Output is correct |
17 |
Correct |
81 ms |
15128 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
40 ms |
5732 KB |
Output is correct |
22 |
Correct |
162 ms |
22864 KB |
Output is correct |
23 |
Correct |
155 ms |
22044 KB |
Output is correct |
24 |
Correct |
169 ms |
25344 KB |
Output is correct |
25 |
Correct |
62 ms |
12292 KB |
Output is correct |
26 |
Correct |
65 ms |
12820 KB |
Output is correct |
27 |
Correct |
158 ms |
22100 KB |
Output is correct |
28 |
Correct |
181 ms |
24128 KB |
Output is correct |
29 |
Correct |
61 ms |
12116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
40 ms |
5784 KB |
Output is correct |
5 |
Correct |
178 ms |
22032 KB |
Output is correct |
6 |
Correct |
157 ms |
22096 KB |
Output is correct |
7 |
Correct |
182 ms |
22176 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
42 ms |
5852 KB |
Output is correct |
10 |
Correct |
158 ms |
22608 KB |
Output is correct |
11 |
Correct |
156 ms |
22128 KB |
Output is correct |
12 |
Correct |
176 ms |
25156 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
600 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
40 ms |
5776 KB |
Output is correct |
17 |
Correct |
159 ms |
22552 KB |
Output is correct |
18 |
Correct |
171 ms |
22656 KB |
Output is correct |
19 |
Correct |
178 ms |
22320 KB |
Output is correct |
20 |
Correct |
158 ms |
22100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
1116 KB |
Output is correct |
7 |
Correct |
170 ms |
22096 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
7 ms |
1176 KB |
Output is correct |
13 |
Correct |
163 ms |
22084 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
852 KB |
Output is correct |
17 |
Correct |
87 ms |
12296 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
40 ms |
5884 KB |
Output is correct |
21 |
Correct |
160 ms |
22032 KB |
Output is correct |
22 |
Correct |
157 ms |
22072 KB |
Output is correct |
23 |
Correct |
181 ms |
22020 KB |
Output is correct |
24 |
Correct |
199 ms |
22064 KB |
Output is correct |
25 |
Correct |
66 ms |
12292 KB |
Output is correct |
26 |
Correct |
60 ms |
12116 KB |
Output is correct |
27 |
Correct |
167 ms |
22148 KB |
Output is correct |
28 |
Correct |
163 ms |
22100 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
0 ms |
344 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
436 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
6 ms |
1116 KB |
Output is correct |
37 |
Correct |
161 ms |
22156 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
7 ms |
1372 KB |
Output is correct |
41 |
Correct |
171 ms |
26196 KB |
Output is correct |
42 |
Correct |
0 ms |
344 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
4 ms |
860 KB |
Output is correct |
45 |
Correct |
81 ms |
15128 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
344 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
40 ms |
5732 KB |
Output is correct |
50 |
Correct |
162 ms |
22864 KB |
Output is correct |
51 |
Correct |
155 ms |
22044 KB |
Output is correct |
52 |
Correct |
169 ms |
25344 KB |
Output is correct |
53 |
Correct |
62 ms |
12292 KB |
Output is correct |
54 |
Correct |
65 ms |
12820 KB |
Output is correct |
55 |
Correct |
158 ms |
22100 KB |
Output is correct |
56 |
Correct |
181 ms |
24128 KB |
Output is correct |
57 |
Correct |
61 ms |
12116 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
4 ms |
860 KB |
Output is correct |
61 |
Correct |
73 ms |
12116 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
52 ms |
5976 KB |
Output is correct |
66 |
Correct |
66 ms |
12824 KB |
Output is correct |
67 |
Correct |
63 ms |
12116 KB |
Output is correct |
68 |
Correct |
66 ms |
12308 KB |
Output is correct |
69 |
Correct |
62 ms |
12368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
1116 KB |
Output is correct |
7 |
Correct |
170 ms |
22096 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
7 ms |
1176 KB |
Output is correct |
13 |
Correct |
163 ms |
22084 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
852 KB |
Output is correct |
17 |
Correct |
87 ms |
12296 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
40 ms |
5884 KB |
Output is correct |
21 |
Correct |
160 ms |
22032 KB |
Output is correct |
22 |
Correct |
157 ms |
22072 KB |
Output is correct |
23 |
Correct |
181 ms |
22020 KB |
Output is correct |
24 |
Correct |
199 ms |
22064 KB |
Output is correct |
25 |
Correct |
66 ms |
12292 KB |
Output is correct |
26 |
Correct |
60 ms |
12116 KB |
Output is correct |
27 |
Correct |
167 ms |
22148 KB |
Output is correct |
28 |
Correct |
163 ms |
22100 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
0 ms |
344 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
436 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
6 ms |
1116 KB |
Output is correct |
37 |
Correct |
161 ms |
22156 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
7 ms |
1372 KB |
Output is correct |
41 |
Correct |
171 ms |
26196 KB |
Output is correct |
42 |
Correct |
0 ms |
344 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
4 ms |
860 KB |
Output is correct |
45 |
Correct |
81 ms |
15128 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
344 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
40 ms |
5732 KB |
Output is correct |
50 |
Correct |
162 ms |
22864 KB |
Output is correct |
51 |
Correct |
155 ms |
22044 KB |
Output is correct |
52 |
Correct |
169 ms |
25344 KB |
Output is correct |
53 |
Correct |
62 ms |
12292 KB |
Output is correct |
54 |
Correct |
65 ms |
12820 KB |
Output is correct |
55 |
Correct |
158 ms |
22100 KB |
Output is correct |
56 |
Correct |
181 ms |
24128 KB |
Output is correct |
57 |
Correct |
61 ms |
12116 KB |
Output is correct |
58 |
Correct |
0 ms |
344 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
40 ms |
5784 KB |
Output is correct |
62 |
Correct |
178 ms |
22032 KB |
Output is correct |
63 |
Correct |
157 ms |
22096 KB |
Output is correct |
64 |
Correct |
182 ms |
22176 KB |
Output is correct |
65 |
Correct |
0 ms |
348 KB |
Output is correct |
66 |
Correct |
42 ms |
5852 KB |
Output is correct |
67 |
Correct |
158 ms |
22608 KB |
Output is correct |
68 |
Correct |
156 ms |
22128 KB |
Output is correct |
69 |
Correct |
176 ms |
25156 KB |
Output is correct |
70 |
Correct |
0 ms |
344 KB |
Output is correct |
71 |
Correct |
0 ms |
600 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
40 ms |
5776 KB |
Output is correct |
74 |
Correct |
159 ms |
22552 KB |
Output is correct |
75 |
Correct |
171 ms |
22656 KB |
Output is correct |
76 |
Correct |
178 ms |
22320 KB |
Output is correct |
77 |
Correct |
158 ms |
22100 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Correct |
0 ms |
348 KB |
Output is correct |
80 |
Correct |
4 ms |
860 KB |
Output is correct |
81 |
Correct |
73 ms |
12116 KB |
Output is correct |
82 |
Correct |
0 ms |
348 KB |
Output is correct |
83 |
Correct |
0 ms |
348 KB |
Output is correct |
84 |
Correct |
0 ms |
348 KB |
Output is correct |
85 |
Correct |
52 ms |
5976 KB |
Output is correct |
86 |
Correct |
66 ms |
12824 KB |
Output is correct |
87 |
Correct |
63 ms |
12116 KB |
Output is correct |
88 |
Correct |
66 ms |
12308 KB |
Output is correct |
89 |
Correct |
62 ms |
12368 KB |
Output is correct |
90 |
Correct |
0 ms |
348 KB |
Output is correct |
91 |
Correct |
0 ms |
348 KB |
Output is correct |
92 |
Correct |
3 ms |
860 KB |
Output is correct |
93 |
Correct |
70 ms |
12296 KB |
Output is correct |
94 |
Correct |
0 ms |
348 KB |
Output is correct |
95 |
Correct |
0 ms |
348 KB |
Output is correct |
96 |
Correct |
0 ms |
348 KB |
Output is correct |
97 |
Correct |
15 ms |
3844 KB |
Output is correct |
98 |
Correct |
60 ms |
14088 KB |
Output is correct |
99 |
Correct |
61 ms |
14160 KB |
Output is correct |
100 |
Correct |
60 ms |
14252 KB |
Output is correct |
101 |
Correct |
59 ms |
14160 KB |
Output is correct |
102 |
Correct |
68 ms |
14164 KB |
Output is correct |
103 |
Correct |
68 ms |
14220 KB |
Output is correct |
104 |
Correct |
68 ms |
14216 KB |
Output is correct |
105 |
Correct |
68 ms |
14160 KB |
Output is correct |