#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;
using ll = long long;
using ld = long double;
const int mxN = 1e3+10;
int p[mxN];
vector<int> adj[mxN];
int type[mxN];
int get(int x) {
return p[x] < 0 ? x : p[x] = get(p[x]);
}
void uni(int a, int b) {
a = get(a); b = get(b);
if(a == b) return ;
if(p[a] > p[b]) swap(a, b);
p[a] += p[b];
p[b] = a;
}
int construct(vector<vector<int>> P) {
int n = P.size();
for(int i = 0; i < n; i++) p[i] = -1;
for(int i = 0; i < n; i++) {
for(int j = i+1; j < n; j++) {
if(P[i][j] == 1 && (get(i) != get(j))) {
uni(i, j);
adj[i].push_back(j);
adj[j].push_back(i);
type[i] = 1; type[j] = 1;
}
}
}
bool pos = true;
for(int i = 0; i < n; i++) {
vector<int> v;
for(int j = 0; j < n; j++) {
if(i == j) continue;
if(P[i][j] == 2 && (get(j) != get(i))) v.push_back(j);
}
if(v.empty()) continue;
if(v.size() == 1) {
pos = false;
break;
}
int x = get(i);
for(int i = 0; i + 1 < v.size(); i++) {
adj[v[i]].push_back(v[i+1]);
adj[v[i+1]].push_back(v[i]);
uni(v[i], v[i+1]);
type[v[i]] = 2;
type[v[i+1]] = 2;
}
adj[x].push_back(v[0]);
adj[v[0]].push_back(x);
adj[v[v.size()-1]].push_back(x);
adj[x].push_back(v[v.size()-1]);
uni(x, v[0]);
}
if(!pos) return 0;
for(int i = 0; i < n; i++) {
for(int j = i+1; j < n; j++) {
if(P[i][j]) {
if(get(i) != get(j)) pos = false;
else {
if(P[i][j] == 1) {
if(type[i] != 1 || type[j] != 1) pos = false;
}
else {
if(type[i] == 1 && type[j] == 1) pos = false;
}
}
}
else {
if(get(i) == get(j)) pos = false;
}
}
}
if(!pos) return 0;
vector<vector<int>> ans(n, vector<int>(n));
for(int i = 0; i < n; i++) {
for(auto it : adj[i]) {
ans[i][it] = ans[it][i] = 1;
}
}
build(ans);
return 1;
}
/*
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
vector<vector<int>> a = {{1, 1, 2, 2}, {1, 1, 2, 2}, {2, 2, 1, 2}, {2, 2, 2, 1}};
cout << construct(a);
}*/
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:52:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for(int i = 0; i + 1 < v.size(); i++) {
| ~~~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
4 ms |
1380 KB |
Output is correct |
7 |
Correct |
99 ms |
23980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
4 ms |
1380 KB |
Output is correct |
7 |
Correct |
99 ms |
23980 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
4 ms |
1356 KB |
Output is correct |
13 |
Correct |
97 ms |
23972 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
872 KB |
Output is correct |
17 |
Correct |
52 ms |
10324 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
24 ms |
6268 KB |
Output is correct |
21 |
Correct |
102 ms |
23960 KB |
Output is correct |
22 |
Correct |
95 ms |
24108 KB |
Output is correct |
23 |
Correct |
100 ms |
24144 KB |
Output is correct |
24 |
Correct |
95 ms |
24100 KB |
Output is correct |
25 |
Correct |
43 ms |
10572 KB |
Output is correct |
26 |
Correct |
41 ms |
10276 KB |
Output is correct |
27 |
Correct |
101 ms |
24084 KB |
Output is correct |
28 |
Correct |
93 ms |
24148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
4 ms |
1372 KB |
Output is correct |
9 |
Correct |
92 ms |
23976 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
4 ms |
1220 KB |
Output is correct |
13 |
Correct |
94 ms |
24104 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
860 KB |
Output is correct |
17 |
Correct |
45 ms |
10160 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
29 ms |
6272 KB |
Output is correct |
22 |
Correct |
102 ms |
24148 KB |
Output is correct |
23 |
Correct |
93 ms |
24148 KB |
Output is correct |
24 |
Correct |
101 ms |
24100 KB |
Output is correct |
25 |
Correct |
40 ms |
10076 KB |
Output is correct |
26 |
Correct |
41 ms |
10320 KB |
Output is correct |
27 |
Correct |
93 ms |
24144 KB |
Output is correct |
28 |
Correct |
99 ms |
24148 KB |
Output is correct |
29 |
Correct |
40 ms |
10064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
24 ms |
6424 KB |
Output is correct |
5 |
Correct |
100 ms |
24104 KB |
Output is correct |
6 |
Correct |
95 ms |
24104 KB |
Output is correct |
7 |
Correct |
100 ms |
24144 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
24 ms |
6236 KB |
Output is correct |
10 |
Correct |
107 ms |
24148 KB |
Output is correct |
11 |
Correct |
95 ms |
24148 KB |
Output is correct |
12 |
Correct |
114 ms |
24152 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Incorrect |
11 ms |
2856 KB |
Answer gives possible 0 while actual possible 1 |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
4 ms |
1380 KB |
Output is correct |
7 |
Correct |
99 ms |
23980 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
4 ms |
1356 KB |
Output is correct |
13 |
Correct |
97 ms |
23972 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
872 KB |
Output is correct |
17 |
Correct |
52 ms |
10324 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
24 ms |
6268 KB |
Output is correct |
21 |
Correct |
102 ms |
23960 KB |
Output is correct |
22 |
Correct |
95 ms |
24108 KB |
Output is correct |
23 |
Correct |
100 ms |
24144 KB |
Output is correct |
24 |
Correct |
95 ms |
24100 KB |
Output is correct |
25 |
Correct |
43 ms |
10572 KB |
Output is correct |
26 |
Correct |
41 ms |
10276 KB |
Output is correct |
27 |
Correct |
101 ms |
24084 KB |
Output is correct |
28 |
Correct |
93 ms |
24148 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
4 ms |
1372 KB |
Output is correct |
37 |
Correct |
92 ms |
23976 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
4 ms |
1220 KB |
Output is correct |
41 |
Correct |
94 ms |
24104 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
2 ms |
860 KB |
Output is correct |
45 |
Correct |
45 ms |
10160 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
29 ms |
6272 KB |
Output is correct |
50 |
Correct |
102 ms |
24148 KB |
Output is correct |
51 |
Correct |
93 ms |
24148 KB |
Output is correct |
52 |
Correct |
101 ms |
24100 KB |
Output is correct |
53 |
Correct |
40 ms |
10076 KB |
Output is correct |
54 |
Correct |
41 ms |
10320 KB |
Output is correct |
55 |
Correct |
93 ms |
24144 KB |
Output is correct |
56 |
Correct |
99 ms |
24148 KB |
Output is correct |
57 |
Correct |
40 ms |
10064 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
3 ms |
860 KB |
Output is correct |
61 |
Correct |
53 ms |
10332 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 |
Incorrect |
11 ms |
2856 KB |
Answer gives possible 0 while actual possible 1 |
66 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
4 ms |
1380 KB |
Output is correct |
7 |
Correct |
99 ms |
23980 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
4 ms |
1356 KB |
Output is correct |
13 |
Correct |
97 ms |
23972 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
872 KB |
Output is correct |
17 |
Correct |
52 ms |
10324 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
24 ms |
6268 KB |
Output is correct |
21 |
Correct |
102 ms |
23960 KB |
Output is correct |
22 |
Correct |
95 ms |
24108 KB |
Output is correct |
23 |
Correct |
100 ms |
24144 KB |
Output is correct |
24 |
Correct |
95 ms |
24100 KB |
Output is correct |
25 |
Correct |
43 ms |
10572 KB |
Output is correct |
26 |
Correct |
41 ms |
10276 KB |
Output is correct |
27 |
Correct |
101 ms |
24084 KB |
Output is correct |
28 |
Correct |
93 ms |
24148 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
4 ms |
1372 KB |
Output is correct |
37 |
Correct |
92 ms |
23976 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
4 ms |
1220 KB |
Output is correct |
41 |
Correct |
94 ms |
24104 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
2 ms |
860 KB |
Output is correct |
45 |
Correct |
45 ms |
10160 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
29 ms |
6272 KB |
Output is correct |
50 |
Correct |
102 ms |
24148 KB |
Output is correct |
51 |
Correct |
93 ms |
24148 KB |
Output is correct |
52 |
Correct |
101 ms |
24100 KB |
Output is correct |
53 |
Correct |
40 ms |
10076 KB |
Output is correct |
54 |
Correct |
41 ms |
10320 KB |
Output is correct |
55 |
Correct |
93 ms |
24144 KB |
Output is correct |
56 |
Correct |
99 ms |
24148 KB |
Output is correct |
57 |
Correct |
40 ms |
10064 KB |
Output is correct |
58 |
Correct |
0 ms |
344 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
1 ms |
348 KB |
Output is correct |
61 |
Correct |
24 ms |
6424 KB |
Output is correct |
62 |
Correct |
100 ms |
24104 KB |
Output is correct |
63 |
Correct |
95 ms |
24104 KB |
Output is correct |
64 |
Correct |
100 ms |
24144 KB |
Output is correct |
65 |
Correct |
0 ms |
348 KB |
Output is correct |
66 |
Correct |
24 ms |
6236 KB |
Output is correct |
67 |
Correct |
107 ms |
24148 KB |
Output is correct |
68 |
Correct |
95 ms |
24148 KB |
Output is correct |
69 |
Correct |
114 ms |
24152 KB |
Output is correct |
70 |
Correct |
1 ms |
348 KB |
Output is correct |
71 |
Correct |
0 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Incorrect |
11 ms |
2856 KB |
Answer gives possible 0 while actual possible 1 |
74 |
Halted |
0 ms |
0 KB |
- |