#include "supertrees.h"
#ifdef ngu
#include "grader.cpp"
#endif // ngu
#include <vector>
#include<bits/stdc++.h>
using namespace std;
int construct(std::vector<std::vector<int>> p) {
int n = p.size();
auto valid = [&] (vector<int> a, vector<int> b, int c) {
for(int &j : a) for(int &k : b) if (p[j][k] != c) return false;
return true;
};
vector<int> vis(n), arr;
auto dfs = [&] (auto self, int u) -> void {
vis[u] = 1;
arr.push_back(u);
for(int v = 0; v < n; v++) if (!vis[v] && p[u][v] == 1) self(self, v);
};
std::vector<std::vector<int>> answer(n, vector<int> (n));
auto add = [&] (int u, int v) {
answer[u][v] = answer[v][u] = 1;
};
vector<vector<int> > connected;
for(int i = 0; i < n; i++) if (!vis[i]) {
arr.clear();
dfs(dfs, i);
if (!valid(arr, arr, 1)) return 0;
connected.push_back(arr);
for(int j = 1; j < arr.size(); j++) {
answer[arr[j]][arr[j - 1]] = answer[arr[j - 1]][arr[j]] = 1;
}
}
for(int i = 0; i < n; i++) vis[i] = 0;
for(auto &arr : connected) {
for(auto &j : arr) for(int k = 0; k < n; k++) if (p[j][k] != p[arr[0]][k]) return 0;
}
auto _dfs = [&] (auto self, int u, int c) -> void {
vis[u] = 1;
arr.push_back(u);
for(int v = 0; v < connected.size(); v++) if (!vis[v] && p[connected[u][0]][connected[v][0]] == c) {
self(self, v, c);
}
};
vector<int> dd(n);
for(int i = 0; i < connected.size(); i++) if (!vis[i]) {
arr.clear();
_dfs(_dfs, i, 2);
for(int &k : arr) for(int &j : arr) if (j != k) {
if (p[connected[k][0]][connected[j][0]] != 2) return 0;
}
if (arr.size() == 1) continue;
if (arr.size() < 3) return 0;
for(int i = 1; i < arr.size(); i++) {
add(connected[arr[i]][0], connected[arr[i - 1]][0]);
}
add(connected[arr[0]][0], connected[arr.back()][0]);
dd[i] = 1;
}
for(int i = 0; i < connected.size(); i++) vis[i] = 0;
for(int i = 0; i < connected.size(); i++) if (!vis[i]) {
arr.clear();
_dfs(_dfs, i, 3);
for(int &k : arr) for(int &j : arr) if (j != k) {
if (p[connected[k][0]][connected[j][0]] != 2) return 0;
}
if (arr.size() == 1) continue;
if (arr.size() < 4) return 0;
for(int i = 1; i < arr.size(); i++) {
add(connected[arr[i]][0], connected[arr[i - 1]][0]);
}
add(connected[arr[0]][0], connected[arr.back()][0]);
add(connected[arr[1]][0], connected[arr.back()][0]);
if (dd[i]) return 0;
}
build(answer);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:39:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int j = 1; j < arr.size(); j++) {
| ~~^~~~~~~~~~~~
supertrees.cpp:60:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for(int i = 0; i < connected.size(); i++) if (!vis[i]) {
| ~~^~~~~~~~~~~~~~~~~~
supertrees.cpp:68:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for(int i = 1; i < arr.size(); i++) {
| ~~^~~~~~~~~~~~
supertrees.cpp:75:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for(int i = 0; i < connected.size(); i++) vis[i] = 0;
| ~~^~~~~~~~~~~~~~~~~~
supertrees.cpp:77:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for(int i = 0; i < connected.size(); i++) if (!vis[i]) {
| ~~^~~~~~~~~~~~~~~~~~
supertrees.cpp:85:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
85 | for(int i = 1; i < arr.size(); i++) {
| ~~^~~~~~~~~~~~
supertrees.cpp: In instantiation of 'construct(std::vector<std::vector<int> >)::<lambda(auto:24, int, int)> [with auto:24 = construct(std::vector<std::vector<int> >)::<lambda(auto:24, int, int)>]':
supertrees.cpp:62:20: required from here
supertrees.cpp:53:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for(int v = 0; v < connected.size(); v++) if (!vis[v] && p[connected[u][0]][connected[v][0]] == c) {
| ~~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
440 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
6 ms |
1184 KB |
Output is correct |
7 |
Correct |
150 ms |
24000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
440 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
6 ms |
1184 KB |
Output is correct |
7 |
Correct |
150 ms |
24000 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 |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
7 ms |
1372 KB |
Output is correct |
13 |
Correct |
158 ms |
24092 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
4 ms |
860 KB |
Output is correct |
17 |
Correct |
63 ms |
14160 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
44 ms |
6644 KB |
Output is correct |
21 |
Correct |
149 ms |
24144 KB |
Output is correct |
22 |
Correct |
151 ms |
24104 KB |
Output is correct |
23 |
Correct |
153 ms |
24084 KB |
Output is correct |
24 |
Correct |
162 ms |
23996 KB |
Output is correct |
25 |
Correct |
61 ms |
14164 KB |
Output is correct |
26 |
Correct |
62 ms |
14160 KB |
Output is correct |
27 |
Correct |
167 ms |
24148 KB |
Output is correct |
28 |
Correct |
148 ms |
24200 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 |
1 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 |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
7 ms |
1364 KB |
Output is correct |
9 |
Correct |
152 ms |
24116 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
7 ms |
1368 KB |
Output is correct |
13 |
Correct |
153 ms |
24228 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
4 ms |
860 KB |
Output is correct |
17 |
Correct |
66 ms |
14076 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
40 ms |
6372 KB |
Output is correct |
22 |
Correct |
164 ms |
23828 KB |
Output is correct |
23 |
Correct |
174 ms |
23888 KB |
Output is correct |
24 |
Correct |
155 ms |
23892 KB |
Output is correct |
25 |
Correct |
63 ms |
13936 KB |
Output is correct |
26 |
Correct |
64 ms |
14088 KB |
Output is correct |
27 |
Correct |
152 ms |
24016 KB |
Output is correct |
28 |
Correct |
156 ms |
23952 KB |
Output is correct |
29 |
Correct |
73 ms |
13984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
38 ms |
6212 KB |
Output is correct |
5 |
Correct |
149 ms |
23960 KB |
Output is correct |
6 |
Correct |
156 ms |
24144 KB |
Output is correct |
7 |
Correct |
159 ms |
24184 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
38 ms |
6224 KB |
Output is correct |
10 |
Correct |
169 ms |
24144 KB |
Output is correct |
11 |
Correct |
150 ms |
24144 KB |
Output is correct |
12 |
Correct |
156 ms |
24052 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
38 ms |
6212 KB |
Output is correct |
17 |
Correct |
162 ms |
22868 KB |
Output is correct |
18 |
Correct |
155 ms |
24180 KB |
Output is correct |
19 |
Correct |
153 ms |
23656 KB |
Output is correct |
20 |
Correct |
169 ms |
24188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
440 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
6 ms |
1184 KB |
Output is correct |
7 |
Correct |
150 ms |
24000 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 |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
7 ms |
1372 KB |
Output is correct |
13 |
Correct |
158 ms |
24092 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
4 ms |
860 KB |
Output is correct |
17 |
Correct |
63 ms |
14160 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
44 ms |
6644 KB |
Output is correct |
21 |
Correct |
149 ms |
24144 KB |
Output is correct |
22 |
Correct |
151 ms |
24104 KB |
Output is correct |
23 |
Correct |
153 ms |
24084 KB |
Output is correct |
24 |
Correct |
162 ms |
23996 KB |
Output is correct |
25 |
Correct |
61 ms |
14164 KB |
Output is correct |
26 |
Correct |
62 ms |
14160 KB |
Output is correct |
27 |
Correct |
167 ms |
24148 KB |
Output is correct |
28 |
Correct |
148 ms |
24200 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
7 ms |
1364 KB |
Output is correct |
37 |
Correct |
152 ms |
24116 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
1 ms |
344 KB |
Output is correct |
40 |
Correct |
7 ms |
1368 KB |
Output is correct |
41 |
Correct |
153 ms |
24228 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
4 ms |
860 KB |
Output is correct |
45 |
Correct |
66 ms |
14076 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
40 ms |
6372 KB |
Output is correct |
50 |
Correct |
164 ms |
23828 KB |
Output is correct |
51 |
Correct |
174 ms |
23888 KB |
Output is correct |
52 |
Correct |
155 ms |
23892 KB |
Output is correct |
53 |
Correct |
63 ms |
13936 KB |
Output is correct |
54 |
Correct |
64 ms |
14088 KB |
Output is correct |
55 |
Correct |
152 ms |
24016 KB |
Output is correct |
56 |
Correct |
156 ms |
23952 KB |
Output is correct |
57 |
Correct |
73 ms |
13984 KB |
Output is correct |
58 |
Correct |
1 ms |
432 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
3 ms |
860 KB |
Output is correct |
61 |
Correct |
67 ms |
13904 KB |
Output is correct |
62 |
Correct |
1 ms |
348 KB |
Output is correct |
63 |
Correct |
1 ms |
432 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
39 ms |
6260 KB |
Output is correct |
66 |
Correct |
60 ms |
13908 KB |
Output is correct |
67 |
Correct |
59 ms |
13908 KB |
Output is correct |
68 |
Correct |
68 ms |
13908 KB |
Output is correct |
69 |
Correct |
67 ms |
13908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
440 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
6 ms |
1184 KB |
Output is correct |
7 |
Correct |
150 ms |
24000 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 |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
7 ms |
1372 KB |
Output is correct |
13 |
Correct |
158 ms |
24092 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
4 ms |
860 KB |
Output is correct |
17 |
Correct |
63 ms |
14160 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
44 ms |
6644 KB |
Output is correct |
21 |
Correct |
149 ms |
24144 KB |
Output is correct |
22 |
Correct |
151 ms |
24104 KB |
Output is correct |
23 |
Correct |
153 ms |
24084 KB |
Output is correct |
24 |
Correct |
162 ms |
23996 KB |
Output is correct |
25 |
Correct |
61 ms |
14164 KB |
Output is correct |
26 |
Correct |
62 ms |
14160 KB |
Output is correct |
27 |
Correct |
167 ms |
24148 KB |
Output is correct |
28 |
Correct |
148 ms |
24200 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
7 ms |
1364 KB |
Output is correct |
37 |
Correct |
152 ms |
24116 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
1 ms |
344 KB |
Output is correct |
40 |
Correct |
7 ms |
1368 KB |
Output is correct |
41 |
Correct |
153 ms |
24228 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
4 ms |
860 KB |
Output is correct |
45 |
Correct |
66 ms |
14076 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
40 ms |
6372 KB |
Output is correct |
50 |
Correct |
164 ms |
23828 KB |
Output is correct |
51 |
Correct |
174 ms |
23888 KB |
Output is correct |
52 |
Correct |
155 ms |
23892 KB |
Output is correct |
53 |
Correct |
63 ms |
13936 KB |
Output is correct |
54 |
Correct |
64 ms |
14088 KB |
Output is correct |
55 |
Correct |
152 ms |
24016 KB |
Output is correct |
56 |
Correct |
156 ms |
23952 KB |
Output is correct |
57 |
Correct |
73 ms |
13984 KB |
Output is correct |
58 |
Correct |
0 ms |
344 KB |
Output is correct |
59 |
Correct |
0 ms |
344 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
38 ms |
6212 KB |
Output is correct |
62 |
Correct |
149 ms |
23960 KB |
Output is correct |
63 |
Correct |
156 ms |
24144 KB |
Output is correct |
64 |
Correct |
159 ms |
24184 KB |
Output is correct |
65 |
Correct |
1 ms |
344 KB |
Output is correct |
66 |
Correct |
38 ms |
6224 KB |
Output is correct |
67 |
Correct |
169 ms |
24144 KB |
Output is correct |
68 |
Correct |
150 ms |
24144 KB |
Output is correct |
69 |
Correct |
156 ms |
24052 KB |
Output is correct |
70 |
Correct |
0 ms |
344 KB |
Output is correct |
71 |
Correct |
0 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
38 ms |
6212 KB |
Output is correct |
74 |
Correct |
162 ms |
22868 KB |
Output is correct |
75 |
Correct |
155 ms |
24180 KB |
Output is correct |
76 |
Correct |
153 ms |
23656 KB |
Output is correct |
77 |
Correct |
169 ms |
24188 KB |
Output is correct |
78 |
Correct |
1 ms |
432 KB |
Output is correct |
79 |
Correct |
0 ms |
348 KB |
Output is correct |
80 |
Correct |
3 ms |
860 KB |
Output is correct |
81 |
Correct |
67 ms |
13904 KB |
Output is correct |
82 |
Correct |
1 ms |
348 KB |
Output is correct |
83 |
Correct |
1 ms |
432 KB |
Output is correct |
84 |
Correct |
0 ms |
348 KB |
Output is correct |
85 |
Correct |
39 ms |
6260 KB |
Output is correct |
86 |
Correct |
60 ms |
13908 KB |
Output is correct |
87 |
Correct |
59 ms |
13908 KB |
Output is correct |
88 |
Correct |
68 ms |
13908 KB |
Output is correct |
89 |
Correct |
67 ms |
13908 KB |
Output is correct |
90 |
Correct |
1 ms |
344 KB |
Output is correct |
91 |
Correct |
0 ms |
600 KB |
Output is correct |
92 |
Correct |
3 ms |
860 KB |
Output is correct |
93 |
Correct |
65 ms |
14340 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 |
19 ms |
3932 KB |
Output is correct |
98 |
Correct |
67 ms |
14072 KB |
Output is correct |
99 |
Correct |
64 ms |
14204 KB |
Output is correct |
100 |
Correct |
60 ms |
14160 KB |
Output is correct |
101 |
Correct |
65 ms |
14072 KB |
Output is correct |
102 |
Correct |
64 ms |
14344 KB |
Output is correct |
103 |
Correct |
65 ms |
14164 KB |
Output is correct |
104 |
Correct |
62 ms |
14160 KB |
Output is correct |
105 |
Correct |
71 ms |
14264 KB |
Output is correct |