#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
int parent[1005], sz[1005], parent2[1005], sz2[1005];
int Find(int x) {
if (x==parent[x])
return x;
return parent[x]=Find(parent[x]);
}
int Find2(int x) {
if (x==parent2[x])
return x;
return parent2[x]=Find2(parent2[x]);
}
void Union(int a, int b) {
a=Find(a), b=Find(b);
if (a==b) return;
if (sz[a]<sz[b])
swap(a, b);
sz[a]+=sz[b];
parent[b]=a;
}
void Union2(int a, int b) {
a=Find2(a), b=Find2(b);
if (a==b) return;
if (sz2[a]<sz2[b])
swap(a, b);
sz2[a]+=sz2[b];
parent2[b]=a;
}
int construct(vector<vector<int> > paths) {
int n=paths.size();
vector<vector<int> > a(n, vector<int> (n, 0));
for (int i=0;i<n;i++)
sz[i]=1, parent[i]=i, sz2[i]=1, parent2[i]=i;
for (int i=0;i<n;i++)
for (int j=i+1;j<n;j++) {
if (paths[i][j]==3) return 0;
else if (paths[i][j]) Union(i, j);
}
vector<int> gr[n];
for (int i=0;i<n;i++) {
int lider=Find(i);
gr[lider].push_back(i);
}
for (int i=0;i<n;i++) {
if (!gr[i].size()) continue;
for (auto x:gr[i])
for (auto y:gr[i])
if (!paths[x][y])
return 0;
vector<int> roots;
for (int j=0;j<gr[i].size();j++) {
int x=gr[i][j];
for (int k=j+1;k<gr[i].size();k++) {
int y=gr[i][k];
if (paths[x][y]==1) Union2(x, y);
}
}
for (int j=0;j<gr[i].size();j++) {
int x=gr[i][j];
for (int k=j+1;k<gr[i].size();k++) {
int y=gr[i][k];
if (paths[x][y]==2&&Find2(x)==Find2(y)) return 0;
}
}
for (auto x:gr[i]) {
if (Find2(x)==x) roots.push_back(x);
else a[x][parent2[x]]=1, a[parent2[x]][x]=1;
}
if (roots.size()<=1) continue;
if (roots.size()==2) return 0;
int x=roots[0], y=roots[roots.size()-1];
for (int j=0;j<roots.size()-1;j++)
a[roots[j]][roots[j+1]]=1, a[roots[j+1]][roots[j]]=1;
a[x][y]=1, a[y][x]=1;
}
build(a);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:60:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for (int j=0;j<gr[i].size();j++) {
| ~^~~~~~~~~~~~~
supertrees.cpp:62:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for (int k=j+1;k<gr[i].size();k++) {
| ~^~~~~~~~~~~~~
supertrees.cpp:67:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for (int j=0;j<gr[i].size();j++) {
| ~^~~~~~~~~~~~~
supertrees.cpp:69:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for (int k=j+1;k<gr[i].size();k++) {
| ~^~~~~~~~~~~~~
supertrees.cpp:81:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | for (int j=0;j<roots.size()-1;j++)
| ~^~~~~~~~~~~~~~~
# |
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 |
6 ms |
1352 KB |
Output is correct |
7 |
Correct |
120 ms |
23484 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 |
6 ms |
1352 KB |
Output is correct |
7 |
Correct |
120 ms |
23484 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 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 |
5 ms |
1368 KB |
Output is correct |
13 |
Correct |
114 ms |
23384 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
59 ms |
14220 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
29 ms |
6300 KB |
Output is correct |
21 |
Correct |
118 ms |
24076 KB |
Output is correct |
22 |
Correct |
120 ms |
24128 KB |
Output is correct |
23 |
Correct |
123 ms |
24144 KB |
Output is correct |
24 |
Correct |
112 ms |
24144 KB |
Output is correct |
25 |
Correct |
49 ms |
14164 KB |
Output is correct |
26 |
Correct |
47 ms |
14164 KB |
Output is correct |
27 |
Correct |
118 ms |
24148 KB |
Output is correct |
28 |
Correct |
110 ms |
24200 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 |
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 |
5 ms |
1360 KB |
Output is correct |
9 |
Correct |
111 ms |
23428 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
5 ms |
1228 KB |
Output is correct |
13 |
Correct |
116 ms |
23380 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
57 ms |
14092 KB |
Output is correct |
18 |
Correct |
0 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 |
28 ms |
6232 KB |
Output is correct |
22 |
Correct |
114 ms |
23944 KB |
Output is correct |
23 |
Correct |
115 ms |
24144 KB |
Output is correct |
24 |
Correct |
128 ms |
24148 KB |
Output is correct |
25 |
Correct |
48 ms |
14164 KB |
Output is correct |
26 |
Correct |
50 ms |
14220 KB |
Output is correct |
27 |
Correct |
115 ms |
24192 KB |
Output is correct |
28 |
Correct |
121 ms |
24144 KB |
Output is correct |
29 |
Correct |
50 ms |
14260 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 |
28 ms |
6292 KB |
Output is correct |
5 |
Correct |
116 ms |
23320 KB |
Output is correct |
6 |
Correct |
110 ms |
23304 KB |
Output is correct |
7 |
Correct |
129 ms |
23476 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
29 ms |
6280 KB |
Output is correct |
10 |
Correct |
112 ms |
23376 KB |
Output is correct |
11 |
Correct |
115 ms |
23376 KB |
Output is correct |
12 |
Correct |
121 ms |
23380 KB |
Output is correct |
13 |
Correct |
0 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 |
Correct |
34 ms |
6480 KB |
Output is correct |
17 |
Correct |
121 ms |
23380 KB |
Output is correct |
18 |
Correct |
115 ms |
23380 KB |
Output is correct |
19 |
Correct |
147 ms |
23452 KB |
Output is correct |
20 |
Correct |
114 ms |
23284 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 |
6 ms |
1352 KB |
Output is correct |
7 |
Correct |
120 ms |
23484 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 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 |
5 ms |
1368 KB |
Output is correct |
13 |
Correct |
114 ms |
23384 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
59 ms |
14220 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
29 ms |
6300 KB |
Output is correct |
21 |
Correct |
118 ms |
24076 KB |
Output is correct |
22 |
Correct |
120 ms |
24128 KB |
Output is correct |
23 |
Correct |
123 ms |
24144 KB |
Output is correct |
24 |
Correct |
112 ms |
24144 KB |
Output is correct |
25 |
Correct |
49 ms |
14164 KB |
Output is correct |
26 |
Correct |
47 ms |
14164 KB |
Output is correct |
27 |
Correct |
118 ms |
24148 KB |
Output is correct |
28 |
Correct |
110 ms |
24200 KB |
Output is correct |
29 |
Correct |
0 ms |
344 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 |
5 ms |
1360 KB |
Output is correct |
37 |
Correct |
111 ms |
23428 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
1 ms |
344 KB |
Output is correct |
40 |
Correct |
5 ms |
1228 KB |
Output is correct |
41 |
Correct |
116 ms |
23380 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
3 ms |
860 KB |
Output is correct |
45 |
Correct |
57 ms |
14092 KB |
Output is correct |
46 |
Correct |
0 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 |
28 ms |
6232 KB |
Output is correct |
50 |
Correct |
114 ms |
23944 KB |
Output is correct |
51 |
Correct |
115 ms |
24144 KB |
Output is correct |
52 |
Correct |
128 ms |
24148 KB |
Output is correct |
53 |
Correct |
48 ms |
14164 KB |
Output is correct |
54 |
Correct |
50 ms |
14220 KB |
Output is correct |
55 |
Correct |
115 ms |
24192 KB |
Output is correct |
56 |
Correct |
121 ms |
24144 KB |
Output is correct |
57 |
Correct |
50 ms |
14260 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
1 ms |
348 KB |
Output is correct |
60 |
Correct |
3 ms |
860 KB |
Output is correct |
61 |
Correct |
56 ms |
14224 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 |
344 KB |
Output is correct |
65 |
Correct |
30 ms |
6224 KB |
Output is correct |
66 |
Correct |
53 ms |
14160 KB |
Output is correct |
67 |
Correct |
52 ms |
14164 KB |
Output is correct |
68 |
Correct |
58 ms |
14160 KB |
Output is correct |
69 |
Correct |
50 ms |
14160 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 |
6 ms |
1352 KB |
Output is correct |
7 |
Correct |
120 ms |
23484 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 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 |
5 ms |
1368 KB |
Output is correct |
13 |
Correct |
114 ms |
23384 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
59 ms |
14220 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
29 ms |
6300 KB |
Output is correct |
21 |
Correct |
118 ms |
24076 KB |
Output is correct |
22 |
Correct |
120 ms |
24128 KB |
Output is correct |
23 |
Correct |
123 ms |
24144 KB |
Output is correct |
24 |
Correct |
112 ms |
24144 KB |
Output is correct |
25 |
Correct |
49 ms |
14164 KB |
Output is correct |
26 |
Correct |
47 ms |
14164 KB |
Output is correct |
27 |
Correct |
118 ms |
24148 KB |
Output is correct |
28 |
Correct |
110 ms |
24200 KB |
Output is correct |
29 |
Correct |
0 ms |
344 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 |
5 ms |
1360 KB |
Output is correct |
37 |
Correct |
111 ms |
23428 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
1 ms |
344 KB |
Output is correct |
40 |
Correct |
5 ms |
1228 KB |
Output is correct |
41 |
Correct |
116 ms |
23380 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
3 ms |
860 KB |
Output is correct |
45 |
Correct |
57 ms |
14092 KB |
Output is correct |
46 |
Correct |
0 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 |
28 ms |
6232 KB |
Output is correct |
50 |
Correct |
114 ms |
23944 KB |
Output is correct |
51 |
Correct |
115 ms |
24144 KB |
Output is correct |
52 |
Correct |
128 ms |
24148 KB |
Output is correct |
53 |
Correct |
48 ms |
14164 KB |
Output is correct |
54 |
Correct |
50 ms |
14220 KB |
Output is correct |
55 |
Correct |
115 ms |
24192 KB |
Output is correct |
56 |
Correct |
121 ms |
24144 KB |
Output is correct |
57 |
Correct |
50 ms |
14260 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
28 ms |
6292 KB |
Output is correct |
62 |
Correct |
116 ms |
23320 KB |
Output is correct |
63 |
Correct |
110 ms |
23304 KB |
Output is correct |
64 |
Correct |
129 ms |
23476 KB |
Output is correct |
65 |
Correct |
0 ms |
344 KB |
Output is correct |
66 |
Correct |
29 ms |
6280 KB |
Output is correct |
67 |
Correct |
112 ms |
23376 KB |
Output is correct |
68 |
Correct |
115 ms |
23376 KB |
Output is correct |
69 |
Correct |
121 ms |
23380 KB |
Output is correct |
70 |
Correct |
0 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 |
Correct |
34 ms |
6480 KB |
Output is correct |
74 |
Correct |
121 ms |
23380 KB |
Output is correct |
75 |
Correct |
115 ms |
23380 KB |
Output is correct |
76 |
Correct |
147 ms |
23452 KB |
Output is correct |
77 |
Correct |
114 ms |
23284 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Correct |
1 ms |
348 KB |
Output is correct |
80 |
Correct |
3 ms |
860 KB |
Output is correct |
81 |
Correct |
56 ms |
14224 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 |
344 KB |
Output is correct |
85 |
Correct |
30 ms |
6224 KB |
Output is correct |
86 |
Correct |
53 ms |
14160 KB |
Output is correct |
87 |
Correct |
52 ms |
14164 KB |
Output is correct |
88 |
Correct |
58 ms |
14160 KB |
Output is correct |
89 |
Correct |
50 ms |
14160 KB |
Output is correct |
90 |
Correct |
1 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 |
56 ms |
14092 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 |
12 ms |
3932 KB |
Output is correct |
98 |
Correct |
49 ms |
14096 KB |
Output is correct |
99 |
Correct |
53 ms |
14160 KB |
Output is correct |
100 |
Correct |
50 ms |
14164 KB |
Output is correct |
101 |
Correct |
48 ms |
14160 KB |
Output is correct |
102 |
Correct |
49 ms |
14160 KB |
Output is correct |
103 |
Correct |
48 ms |
14160 KB |
Output is correct |
104 |
Correct |
49 ms |
14216 KB |
Output is correct |
105 |
Correct |
56 ms |
14164 KB |
Output is correct |