#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;
struct Solver {
int n;
vector<bool> vs;
vector<int> lineIndex, cycleIndex;
vector<vector<int>> p, line, cycle;
Solver(vector<vector<int>> p): p(p), n(p.size()) {
vs.resize(n);
lineIndex.resize(n);
line.resize(n);
cycle.resize(n);
cycleIndex.resize(n);
}
void dfs1(int a, int c) {
if (vs[a]) return;
vs[a] = 1;
lineIndex[a] = c;
line[c].push_back(a);
for (int i = 0; i < n; i++) {
if (p[a][i] == 1) dfs1(i, c);
}
}
void dfs2(int a, int c) {
if (vs[a]) return;
vs[a] = 1;
cycle[c].push_back(a);
cycleIndex[a] = c;
for (int i = 0; i < n; i++) {
if (p[a][i] == 2) dfs2(i, c);
}
}
int solve() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (p[i][j] == 3) return 0;
}
}
for (int i = 0; i < n; i++) {
if (!vs[i]) dfs1(i, i);
}
for (int i = 0; i < n; i++) {
if (lineIndex[i] == i) vs[i] = 0;
}
for (int i = 0; i < n; i++) {
if (!vs[i]) dfs2(i, i);
}
// construct res
vector<vector<int>> res(n, vector<int>(n));
for (int i = 0; i < n; i++) {
for (int j = 0; j + 1 < line[i].size(); j++) {
int x = line[i][j], y = line[i][j + 1];
res[x][y] = res[y][x] = 1;
}
if (cycle[i].size() > 1) cycle[i].push_back(cycle[i][0]);
for (int j = 0; j + 1 < cycle[i].size(); j++) {
int x = cycle[i][j], y = cycle[i][j + 1];
res[x][y] = res[y][x] = 1;
}
}
// check satisfaction
// p[i][j] = 0, p[j][k] = 0, p[i][k] > 0
// p[i][j] = 1 iff lineIndex[i] = lineIndex[j]
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) continue;
int a = cycleIndex[lineIndex[i]];
int b = cycleIndex[lineIndex[j]];
if (p[i][j] == 0 && a == b) {
return 0;
}
if (p[i][j] && a != b) {
return 0;
}
if (p[i][j] == 1 && lineIndex[i] != lineIndex[j]) {
return 0;
}
if (p[i][j] == 2 && lineIndex[i] == lineIndex[j]) {
return 0;
}
}
}
build(res);
// for (int i = 0; i < n; i++) {
// cout << lineIndex[i] << " ";
// }
// cout << endl;
// for (int i = 0; i < n; i++) {
// if (cycle[i].size()) {
// for (auto j: cycle[i]) cout << j << " ";
// cout << endl;
// }
// }
return 1;
}
};
int construct(vector<vector<int>> p) {
return Solver(p).solve();
}
Compilation message
supertrees.cpp: In constructor 'Solver::Solver(std::vector<std::vector<int> >)':
supertrees.cpp:9:25: warning: 'Solver::p' will be initialized after [-Wreorder]
9 | vector<vector<int>> p, line, cycle;
| ^
supertrees.cpp:6:9: warning: 'int Solver::n' [-Wreorder]
6 | int n;
| ^
supertrees.cpp:11:5: warning: when initialized here [-Wreorder]
11 | Solver(vector<vector<int>> p): p(p), n(p.size()) {
| ^~~~~~
supertrees.cpp: In member function 'int Solver::solve()':
supertrees.cpp:63:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for (int j = 0; j + 1 < line[i].size(); j++) {
| ~~~~~~^~~~~~~~~~~~~~~~
supertrees.cpp:69:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for (int j = 0; j + 1 < cycle[i].size(); j++) {
| ~~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
10 ms |
1536 KB |
Output is correct |
7 |
Correct |
244 ms |
30072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
10 ms |
1536 KB |
Output is correct |
7 |
Correct |
244 ms |
30072 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
10 ms |
1536 KB |
Output is correct |
13 |
Correct |
244 ms |
30200 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
6 ms |
1152 KB |
Output is correct |
17 |
Correct |
124 ms |
20216 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
256 KB |
Output is correct |
20 |
Correct |
62 ms |
7888 KB |
Output is correct |
21 |
Correct |
249 ms |
30200 KB |
Output is correct |
22 |
Correct |
241 ms |
30040 KB |
Output is correct |
23 |
Correct |
272 ms |
30072 KB |
Output is correct |
24 |
Correct |
257 ms |
30072 KB |
Output is correct |
25 |
Correct |
106 ms |
20092 KB |
Output is correct |
26 |
Correct |
104 ms |
20092 KB |
Output is correct |
27 |
Correct |
256 ms |
30200 KB |
Output is correct |
28 |
Correct |
260 ms |
30200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Incorrect |
0 ms |
256 KB |
Answer gives possible 1 while actual possible 0 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
61 ms |
7800 KB |
Output is correct |
5 |
Correct |
245 ms |
30072 KB |
Output is correct |
6 |
Correct |
244 ms |
30072 KB |
Output is correct |
7 |
Correct |
255 ms |
30200 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
62 ms |
7884 KB |
Output is correct |
10 |
Correct |
247 ms |
30072 KB |
Output is correct |
11 |
Correct |
243 ms |
30044 KB |
Output is correct |
12 |
Correct |
257 ms |
30200 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
62 ms |
7800 KB |
Output is correct |
17 |
Correct |
250 ms |
30200 KB |
Output is correct |
18 |
Correct |
248 ms |
30072 KB |
Output is correct |
19 |
Correct |
241 ms |
30060 KB |
Output is correct |
20 |
Correct |
243 ms |
30200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
10 ms |
1536 KB |
Output is correct |
7 |
Correct |
244 ms |
30072 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
10 ms |
1536 KB |
Output is correct |
13 |
Correct |
244 ms |
30200 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
6 ms |
1152 KB |
Output is correct |
17 |
Correct |
124 ms |
20216 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
256 KB |
Output is correct |
20 |
Correct |
62 ms |
7888 KB |
Output is correct |
21 |
Correct |
249 ms |
30200 KB |
Output is correct |
22 |
Correct |
241 ms |
30040 KB |
Output is correct |
23 |
Correct |
272 ms |
30072 KB |
Output is correct |
24 |
Correct |
257 ms |
30072 KB |
Output is correct |
25 |
Correct |
106 ms |
20092 KB |
Output is correct |
26 |
Correct |
104 ms |
20092 KB |
Output is correct |
27 |
Correct |
256 ms |
30200 KB |
Output is correct |
28 |
Correct |
260 ms |
30200 KB |
Output is correct |
29 |
Correct |
0 ms |
256 KB |
Output is correct |
30 |
Correct |
0 ms |
256 KB |
Output is correct |
31 |
Correct |
0 ms |
256 KB |
Output is correct |
32 |
Incorrect |
0 ms |
256 KB |
Answer gives possible 1 while actual possible 0 |
33 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
10 ms |
1536 KB |
Output is correct |
7 |
Correct |
244 ms |
30072 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
10 ms |
1536 KB |
Output is correct |
13 |
Correct |
244 ms |
30200 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
6 ms |
1152 KB |
Output is correct |
17 |
Correct |
124 ms |
20216 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
256 KB |
Output is correct |
20 |
Correct |
62 ms |
7888 KB |
Output is correct |
21 |
Correct |
249 ms |
30200 KB |
Output is correct |
22 |
Correct |
241 ms |
30040 KB |
Output is correct |
23 |
Correct |
272 ms |
30072 KB |
Output is correct |
24 |
Correct |
257 ms |
30072 KB |
Output is correct |
25 |
Correct |
106 ms |
20092 KB |
Output is correct |
26 |
Correct |
104 ms |
20092 KB |
Output is correct |
27 |
Correct |
256 ms |
30200 KB |
Output is correct |
28 |
Correct |
260 ms |
30200 KB |
Output is correct |
29 |
Correct |
0 ms |
256 KB |
Output is correct |
30 |
Correct |
0 ms |
256 KB |
Output is correct |
31 |
Correct |
0 ms |
256 KB |
Output is correct |
32 |
Incorrect |
0 ms |
256 KB |
Answer gives possible 1 while actual possible 0 |
33 |
Halted |
0 ms |
0 KB |
- |