#include "supertrees.h"
#include "bits/stdc++.h"
#define MAXN 1005
using namespace std;
int N;
int sizes[MAXN], par[MAXN];
vector<int> comps[MAXN], comps1[MAXN];
vector<vector<int>> answer;
int find(int u) {
if (u != par[u])
par[u] = find(par[u]);
return par[u];
}
void join(int u, int v) {
u = find(u);
v = find(v);
if (u == v) return;
//answer[u][v] = 1;
//answer[v][u] = 1;
if (sizes[u] > sizes[v]) {
par[v] = u;
sizes[u] += sizes[v];
} else {
par[u] = v;
sizes[v] += sizes[u];
}
}
void reset_dsu() {
for (int i = 0; i < N; i++) {
par[i] = i;
sizes[i] = 1;
}
}
int construct(vector<vector<int>> p) {
N = p.size();
answer = vector<vector<int>>(N, vector<int>(N, 0));
//run dsu
reset_dsu();
for (int i = 0; i < N; i++) {
for (int j = i+1; j < N; j++) {
if (p[i][j] == 3)
return 0;
if (p[i][j])
join(i, j);
}
}
//build components
for (int i = 0; i < N; i++)
comps[find(i)].push_back(i);
//run dsu again to find comps of 1
reset_dsu();
for (int i = 0; i < N; i++) {
for (int j = 0; j < comps[i].size(); j++) {
for (int k = j+1; k < comps[i].size(); k++) {
int x = comps[i][j], y = comps[i][k];
if (p[x][y] == 0) return 0;
if (p[x][y] == 1) join(x, y);
}
}
}
vector<int> reps;
for (int i = 0; i < N; i++)
comps1[find(i)].push_back(i);
//check validity
for (int i = 0; i < N; i++) {
for (int j = i+1; j < N; j++) {
if (find(i) == find(j) && p[i][j] != 1)
return 0;
}
}
//build paths
for (int i = 0; i < N; i++) {
for (int j = 0; j < ((int) comps1[i].size())-1; j++) {
answer[comps1[i][j]][comps1[i][j+1]] = 1;
answer[comps1[i][j+1]][comps1[i][j]] = 1;
}
}
//build rings
for (int i = 0; i < N; i++) {
vector<int> reps;
for (int j : comps[i]) {
if (j == find(j))
reps.push_back(j);
}
int rsize = reps.size();
if (rsize <= 1) continue;
if (rsize == 2)
return 0;
for (int j = 0; j < rsize - 1; j++) {
answer[reps[j]][reps[j+1]] = 1;
answer[reps[j+1]][reps[j]] = 1;
}
answer[reps[0]][reps[rsize-1]] = 1;
answer[reps[rsize-1]][reps[0]] = 1;
}
build(answer);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:60:27: 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 < comps[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~~~
supertrees.cpp:61:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for (int k = j+1; k < comps[i].size(); k++) {
| ~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
5 ms |
1156 KB |
Output is correct |
7 |
Correct |
117 ms |
22064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
5 ms |
1156 KB |
Output is correct |
7 |
Correct |
117 ms |
22064 KB |
Output is correct |
8 |
Correct |
1 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 |
344 KB |
Output is correct |
12 |
Correct |
5 ms |
1372 KB |
Output is correct |
13 |
Correct |
113 ms |
22096 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
5 ms |
860 KB |
Output is correct |
17 |
Correct |
61 ms |
12116 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
360 KB |
Output is correct |
20 |
Correct |
28 ms |
5836 KB |
Output is correct |
21 |
Correct |
119 ms |
22100 KB |
Output is correct |
22 |
Correct |
112 ms |
22092 KB |
Output is correct |
23 |
Correct |
121 ms |
22096 KB |
Output is correct |
24 |
Correct |
112 ms |
22100 KB |
Output is correct |
25 |
Correct |
52 ms |
12112 KB |
Output is correct |
26 |
Correct |
48 ms |
12116 KB |
Output is correct |
27 |
Correct |
126 ms |
22100 KB |
Output is correct |
28 |
Correct |
110 ms |
22208 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 |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
1372 KB |
Output is correct |
9 |
Correct |
112 ms |
22084 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 |
1372 KB |
Output is correct |
13 |
Correct |
122 ms |
22092 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 |
972 KB |
Output is correct |
17 |
Correct |
57 ms |
12112 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 |
5952 KB |
Output is correct |
22 |
Correct |
113 ms |
22084 KB |
Output is correct |
23 |
Correct |
118 ms |
22100 KB |
Output is correct |
24 |
Correct |
124 ms |
22092 KB |
Output is correct |
25 |
Correct |
53 ms |
12372 KB |
Output is correct |
26 |
Correct |
49 ms |
12116 KB |
Output is correct |
27 |
Correct |
111 ms |
22096 KB |
Output is correct |
28 |
Correct |
115 ms |
22088 KB |
Output is correct |
29 |
Correct |
50 ms |
12364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
28 ms |
5924 KB |
Output is correct |
5 |
Correct |
100 ms |
22100 KB |
Output is correct |
6 |
Correct |
110 ms |
22096 KB |
Output is correct |
7 |
Correct |
113 ms |
22084 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
29 ms |
5720 KB |
Output is correct |
10 |
Correct |
111 ms |
22096 KB |
Output is correct |
11 |
Correct |
112 ms |
22100 KB |
Output is correct |
12 |
Correct |
126 ms |
22096 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 |
28 ms |
5960 KB |
Output is correct |
17 |
Correct |
109 ms |
22180 KB |
Output is correct |
18 |
Correct |
116 ms |
22100 KB |
Output is correct |
19 |
Correct |
98 ms |
22036 KB |
Output is correct |
20 |
Correct |
103 ms |
22156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
5 ms |
1156 KB |
Output is correct |
7 |
Correct |
117 ms |
22064 KB |
Output is correct |
8 |
Correct |
1 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 |
344 KB |
Output is correct |
12 |
Correct |
5 ms |
1372 KB |
Output is correct |
13 |
Correct |
113 ms |
22096 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
5 ms |
860 KB |
Output is correct |
17 |
Correct |
61 ms |
12116 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
360 KB |
Output is correct |
20 |
Correct |
28 ms |
5836 KB |
Output is correct |
21 |
Correct |
119 ms |
22100 KB |
Output is correct |
22 |
Correct |
112 ms |
22092 KB |
Output is correct |
23 |
Correct |
121 ms |
22096 KB |
Output is correct |
24 |
Correct |
112 ms |
22100 KB |
Output is correct |
25 |
Correct |
52 ms |
12112 KB |
Output is correct |
26 |
Correct |
48 ms |
12116 KB |
Output is correct |
27 |
Correct |
126 ms |
22100 KB |
Output is correct |
28 |
Correct |
110 ms |
22208 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 |
344 KB |
Output is correct |
36 |
Correct |
5 ms |
1372 KB |
Output is correct |
37 |
Correct |
112 ms |
22084 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
5 ms |
1372 KB |
Output is correct |
41 |
Correct |
122 ms |
22092 KB |
Output is correct |
42 |
Correct |
0 ms |
344 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
3 ms |
972 KB |
Output is correct |
45 |
Correct |
57 ms |
12112 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 |
5952 KB |
Output is correct |
50 |
Correct |
113 ms |
22084 KB |
Output is correct |
51 |
Correct |
118 ms |
22100 KB |
Output is correct |
52 |
Correct |
124 ms |
22092 KB |
Output is correct |
53 |
Correct |
53 ms |
12372 KB |
Output is correct |
54 |
Correct |
49 ms |
12116 KB |
Output is correct |
55 |
Correct |
111 ms |
22096 KB |
Output is correct |
56 |
Correct |
115 ms |
22088 KB |
Output is correct |
57 |
Correct |
50 ms |
12364 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 |
856 KB |
Output is correct |
61 |
Correct |
57 ms |
12116 KB |
Output is correct |
62 |
Correct |
0 ms |
344 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
29 ms |
5876 KB |
Output is correct |
66 |
Correct |
49 ms |
12116 KB |
Output is correct |
67 |
Correct |
46 ms |
12112 KB |
Output is correct |
68 |
Correct |
54 ms |
12116 KB |
Output is correct |
69 |
Correct |
55 ms |
12344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
5 ms |
1156 KB |
Output is correct |
7 |
Correct |
117 ms |
22064 KB |
Output is correct |
8 |
Correct |
1 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 |
344 KB |
Output is correct |
12 |
Correct |
5 ms |
1372 KB |
Output is correct |
13 |
Correct |
113 ms |
22096 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
5 ms |
860 KB |
Output is correct |
17 |
Correct |
61 ms |
12116 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
360 KB |
Output is correct |
20 |
Correct |
28 ms |
5836 KB |
Output is correct |
21 |
Correct |
119 ms |
22100 KB |
Output is correct |
22 |
Correct |
112 ms |
22092 KB |
Output is correct |
23 |
Correct |
121 ms |
22096 KB |
Output is correct |
24 |
Correct |
112 ms |
22100 KB |
Output is correct |
25 |
Correct |
52 ms |
12112 KB |
Output is correct |
26 |
Correct |
48 ms |
12116 KB |
Output is correct |
27 |
Correct |
126 ms |
22100 KB |
Output is correct |
28 |
Correct |
110 ms |
22208 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 |
344 KB |
Output is correct |
36 |
Correct |
5 ms |
1372 KB |
Output is correct |
37 |
Correct |
112 ms |
22084 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
5 ms |
1372 KB |
Output is correct |
41 |
Correct |
122 ms |
22092 KB |
Output is correct |
42 |
Correct |
0 ms |
344 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
3 ms |
972 KB |
Output is correct |
45 |
Correct |
57 ms |
12112 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 |
5952 KB |
Output is correct |
50 |
Correct |
113 ms |
22084 KB |
Output is correct |
51 |
Correct |
118 ms |
22100 KB |
Output is correct |
52 |
Correct |
124 ms |
22092 KB |
Output is correct |
53 |
Correct |
53 ms |
12372 KB |
Output is correct |
54 |
Correct |
49 ms |
12116 KB |
Output is correct |
55 |
Correct |
111 ms |
22096 KB |
Output is correct |
56 |
Correct |
115 ms |
22088 KB |
Output is correct |
57 |
Correct |
50 ms |
12364 KB |
Output is correct |
58 |
Correct |
0 ms |
344 KB |
Output is correct |
59 |
Correct |
0 ms |
600 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
28 ms |
5924 KB |
Output is correct |
62 |
Correct |
100 ms |
22100 KB |
Output is correct |
63 |
Correct |
110 ms |
22096 KB |
Output is correct |
64 |
Correct |
113 ms |
22084 KB |
Output is correct |
65 |
Correct |
1 ms |
344 KB |
Output is correct |
66 |
Correct |
29 ms |
5720 KB |
Output is correct |
67 |
Correct |
111 ms |
22096 KB |
Output is correct |
68 |
Correct |
112 ms |
22100 KB |
Output is correct |
69 |
Correct |
126 ms |
22096 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 |
28 ms |
5960 KB |
Output is correct |
74 |
Correct |
109 ms |
22180 KB |
Output is correct |
75 |
Correct |
116 ms |
22100 KB |
Output is correct |
76 |
Correct |
98 ms |
22036 KB |
Output is correct |
77 |
Correct |
103 ms |
22156 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Correct |
0 ms |
348 KB |
Output is correct |
80 |
Correct |
3 ms |
856 KB |
Output is correct |
81 |
Correct |
57 ms |
12116 KB |
Output is correct |
82 |
Correct |
0 ms |
344 KB |
Output is correct |
83 |
Correct |
0 ms |
348 KB |
Output is correct |
84 |
Correct |
0 ms |
348 KB |
Output is correct |
85 |
Correct |
29 ms |
5876 KB |
Output is correct |
86 |
Correct |
49 ms |
12116 KB |
Output is correct |
87 |
Correct |
46 ms |
12112 KB |
Output is correct |
88 |
Correct |
54 ms |
12116 KB |
Output is correct |
89 |
Correct |
55 ms |
12344 KB |
Output is correct |
90 |
Correct |
0 ms |
344 KB |
Output is correct |
91 |
Correct |
0 ms |
348 KB |
Output is correct |
92 |
Correct |
2 ms |
904 KB |
Output is correct |
93 |
Correct |
52 ms |
12092 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 |
344 KB |
Output is correct |
97 |
Correct |
12 ms |
3272 KB |
Output is correct |
98 |
Correct |
55 ms |
12116 KB |
Output is correct |
99 |
Correct |
61 ms |
12108 KB |
Output is correct |
100 |
Correct |
45 ms |
12112 KB |
Output is correct |
101 |
Correct |
45 ms |
12112 KB |
Output is correct |
102 |
Correct |
47 ms |
12116 KB |
Output is correct |
103 |
Correct |
52 ms |
12116 KB |
Output is correct |
104 |
Correct |
52 ms |
12100 KB |
Output is correct |
105 |
Correct |
62 ms |
12116 KB |
Output is correct |