#include "supertrees.h"
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int n;
bool check_contr(int u, vector<bool> &visited, vector<bool> &in_group,
vector<int> &group, vector<vector<int>> &p) {
visited[u] = in_group[u] = true;
group.push_back(u);
for(int v = 0; v < n; v++) {
if(p[u][v] == 0) {
if(in_group[v]) return true;
}
else if(!visited[v]) {
if(check_contr(v, visited, in_group, group, p)) return true;
}
}
return false;
}
int construct(vector<vector<int>> p) {
// // sample interaction
// int n = p.size();
// vector<vector<int>> answer;
// for (int i = 0; i < n; i++) {
// vector<int> row;
// row.resize(n);
// answer.push_back(row);
// }
// build(answer);
// return 1;
n = p.size();
// Impossible when any p_i == 3
for(auto &v : p) for(int x : v) if(x == 3) return 0;
// Check contradictions (a reaches b and b reaches c implies a reaches c)
vector<bool> visited(n), in_group(n);
vector<vector<int>> groups;
for(int i = 0; i < n; i++) {
if(!visited[i]) {
vector<int> group;
if(check_contr(i, visited, in_group, group, p)) return 0;
for(int x : group) in_group[x] = false;
groups.push_back(group);
}
}
vector<vector<int>> ans(n, vector<int>(n));
for(auto &g : groups) {
// Every vertex p values within a group are either 1 or 2.
// There is at most a single cycle in a group.
// Vertices part of the cycle should have p == 2
// for all other member of the groups except the subtree attached
// After making the cycle a root other members
// should have p == 1 to other vertices in the same subtree
// and p == 2 to other vertices.
vector<int> root(g.size(), -1);
for(int i = 0; i < g.size(); i++) {
if(root[i] == -1) {
// new subtree
root[i] = g[i];
for(int j = 0; j < g.size(); j++) {
if(i == j) continue;
if(p[g[i]][g[j]] != 2) {
if(root[j] != -1) {
return 0; // contradiction
}
root[j] = root[i];
}
}
}
else {
// check old subtree
for(int j = 0; j < g.size(); j++) {
if(i == j) continue;
if(p[g[i]][g[j]] != 2) {
if(root[j] == -1 || root[i] != root[j]) {
return 0; // contradiction
}
}
}
}
}
for(int i = 0; i < g.size(); i++) {
ans[g[i]][root[i]] = ans[root[i]][g[i]] = 1;
}
sort(root.begin(), root.end());
root.erase(unique(root.begin(), root.end()), root.end());
for(int i = 0; i < root.size(); i++) {
ans[root[i]][root[(i+1)%root.size()]] = ans[root[(i+1)%root.size()]][root[i]] = 1;
}
}
// ensure b[i][i] = 0
for(int i = 0; i < n; i++) {
ans[i][i] = 0;
}
build(ans);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:60:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for(int i = 0; i < g.size(); i++) {
| ~~^~~~~~~~~~
supertrees.cpp:64:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for(int j = 0; j < g.size(); j++) {
| ~~^~~~~~~~~~
supertrees.cpp:76:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | for(int j = 0; j < g.size(); j++) {
| ~~^~~~~~~~~~
supertrees.cpp:86:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | for(int i = 0; i < g.size(); i++) {
| ~~^~~~~~~~~~
supertrees.cpp:91:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | for(int i = 0; i < root.size(); i++) {
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1228 KB |
Output is correct |
7 |
Correct |
163 ms |
24060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1228 KB |
Output is correct |
7 |
Correct |
163 ms |
24060 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
304 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
1208 KB |
Output is correct |
13 |
Correct |
158 ms |
24052 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
3 ms |
672 KB |
Output is correct |
17 |
Correct |
64 ms |
10112 KB |
Output is correct |
18 |
Correct |
0 ms |
300 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
38 ms |
6204 KB |
Output is correct |
21 |
Correct |
155 ms |
23920 KB |
Output is correct |
22 |
Correct |
150 ms |
23972 KB |
Output is correct |
23 |
Correct |
159 ms |
23932 KB |
Output is correct |
24 |
Correct |
163 ms |
24088 KB |
Output is correct |
25 |
Correct |
58 ms |
10060 KB |
Output is correct |
26 |
Correct |
57 ms |
10096 KB |
Output is correct |
27 |
Correct |
156 ms |
24048 KB |
Output is correct |
28 |
Correct |
157 ms |
24048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
296 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Incorrect |
0 ms |
212 KB |
Answer gives possible 1 while actual possible 0 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
300 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
39 ms |
6272 KB |
Output is correct |
5 |
Correct |
150 ms |
24012 KB |
Output is correct |
6 |
Correct |
150 ms |
23920 KB |
Output is correct |
7 |
Correct |
155 ms |
23936 KB |
Output is correct |
8 |
Correct |
1 ms |
296 KB |
Output is correct |
9 |
Correct |
38 ms |
6240 KB |
Output is correct |
10 |
Correct |
161 ms |
23924 KB |
Output is correct |
11 |
Correct |
159 ms |
24052 KB |
Output is correct |
12 |
Correct |
157 ms |
24056 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
38 ms |
6260 KB |
Output is correct |
17 |
Correct |
159 ms |
23920 KB |
Output is correct |
18 |
Correct |
151 ms |
24012 KB |
Output is correct |
19 |
Correct |
150 ms |
24000 KB |
Output is correct |
20 |
Correct |
153 ms |
24068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1228 KB |
Output is correct |
7 |
Correct |
163 ms |
24060 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
304 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
1208 KB |
Output is correct |
13 |
Correct |
158 ms |
24052 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
3 ms |
672 KB |
Output is correct |
17 |
Correct |
64 ms |
10112 KB |
Output is correct |
18 |
Correct |
0 ms |
300 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
38 ms |
6204 KB |
Output is correct |
21 |
Correct |
155 ms |
23920 KB |
Output is correct |
22 |
Correct |
150 ms |
23972 KB |
Output is correct |
23 |
Correct |
159 ms |
23932 KB |
Output is correct |
24 |
Correct |
163 ms |
24088 KB |
Output is correct |
25 |
Correct |
58 ms |
10060 KB |
Output is correct |
26 |
Correct |
57 ms |
10096 KB |
Output is correct |
27 |
Correct |
156 ms |
24048 KB |
Output is correct |
28 |
Correct |
157 ms |
24048 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
296 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Incorrect |
0 ms |
212 KB |
Answer gives possible 1 while actual possible 0 |
33 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1228 KB |
Output is correct |
7 |
Correct |
163 ms |
24060 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
304 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
1208 KB |
Output is correct |
13 |
Correct |
158 ms |
24052 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
3 ms |
672 KB |
Output is correct |
17 |
Correct |
64 ms |
10112 KB |
Output is correct |
18 |
Correct |
0 ms |
300 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
38 ms |
6204 KB |
Output is correct |
21 |
Correct |
155 ms |
23920 KB |
Output is correct |
22 |
Correct |
150 ms |
23972 KB |
Output is correct |
23 |
Correct |
159 ms |
23932 KB |
Output is correct |
24 |
Correct |
163 ms |
24088 KB |
Output is correct |
25 |
Correct |
58 ms |
10060 KB |
Output is correct |
26 |
Correct |
57 ms |
10096 KB |
Output is correct |
27 |
Correct |
156 ms |
24048 KB |
Output is correct |
28 |
Correct |
157 ms |
24048 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
296 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Incorrect |
0 ms |
212 KB |
Answer gives possible 1 while actual possible 0 |
33 |
Halted |
0 ms |
0 KB |
- |