#include "supertrees.h"
#include <iostream>
#include <vector>
#include <algorithm>
#define mp make_pair
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
vector<vector<int> > out;
int n;
bool check_components(vector<vector<int> > p) {
vector<int> comp(n, -1);
for (int i = 0; i < n; i++) {
if (p[i][i] != 1)
return false;
if (comp[i] == -1) {
for (int j = 0; j < n; j++) {
if (p[i][j] != 0 and comp[j] != -1)
return false;
if (p[i][j] != 0)
comp[j] = i;
}
}
else {
for (int j = 0; j < n; j++)
if (p[i][j] != 0 and comp[j] != comp[i])
return false;
}
}
return true;
}
void make_line(vector<int> a) {
if (a.size() == 1)
return;
for (int i = 0; i < a.size() - 1; i++)
{
int f = a[i], s = a[i + 1];
out[f][s] = 1, out[s][f] = 1;
}
}
vector<vector<int> > p;
vector<bool> vis;
vector<int> ch;
void dfs(int c) {
ch.push_back(c);
vis[c] = true;
for (int i = 0; i < n; i++) {
if (p[i][c] == 1 and !vis[i])
dfs(i);
}
}
int construct(std::vector<std::vector<int>> p) {
n = p.size();
::p = p;
std::vector<std::vector<int>> answer;
if (check_components(p) == false)
return 0;
vector<bool> visited(n);
out = vector<vector<int> >(n, vector<int>(n, 0));
for (int v = 0; v < n; v++) {
if (visited[v])
continue;
vector<int> mine;
for (int j = 0; j < n; j++)
if (p[v][j] != 0)
mine.push_back(j), visited[j] = true;
if (mine.size() == 1)
continue;
vector<int> everyone_else = mine;
vector<vector<int> > chains;
vis = vector<bool>(n);
for (int a : everyone_else) {
// now we decompose into sub-chains
ch.clear();
if (!vis[a]) {
dfs(a);
chains.push_back(ch);
}
}
// now check if chains are good
for (int i = 0; i < chains.size(); i++) {
for (int a : chains[i])
for (int b : chains[i])
if (p[a][b] != 1)
return 0;
for (int j = i + 1; j < chains.size(); j++) {
for (int a : chains[i])
for (int b : chains[j])
if (p[a][b] != 2)
return 0;
}
}
for (vector<int> chai : chains)
make_line(chai);
// connecting chains and cyc
if (chains.size() > 1) {
vector<int> fin;
for (int i = 0; i < chains.size(); i++)
fin.push_back(chains[i][0]);
fin.push_back(chains[0][0]);
if (chains.size() == 2)
return 0;
make_line(fin);
}
}
build(out);
return 1;
}
Compilation message
supertrees.cpp: In function 'void make_line(std::vector<int>)':
supertrees.cpp:41:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for (int i = 0; i < a.size() - 1; i++)
| ~~^~~~~~~~~~~~~~
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:90:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
90 | for (int i = 0; i < chains.size(); i++) {
| ~~^~~~~~~~~~~~~~~
supertrees.cpp:95:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | for (int j = i + 1; j < chains.size(); j++) {
| ~~^~~~~~~~~~~~~~~
supertrees.cpp:108:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
108 | for (int i = 0; i < chains.size(); i++)
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1364 KB |
Output is correct |
7 |
Correct |
155 ms |
26020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1364 KB |
Output is correct |
7 |
Correct |
155 ms |
26020 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
6 ms |
1236 KB |
Output is correct |
13 |
Correct |
170 ms |
26040 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
3 ms |
852 KB |
Output is correct |
17 |
Correct |
65 ms |
16012 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
42 ms |
7128 KB |
Output is correct |
21 |
Correct |
163 ms |
26828 KB |
Output is correct |
22 |
Correct |
155 ms |
26772 KB |
Output is correct |
23 |
Correct |
161 ms |
26708 KB |
Output is correct |
24 |
Correct |
166 ms |
26764 KB |
Output is correct |
25 |
Correct |
63 ms |
16732 KB |
Output is correct |
26 |
Correct |
64 ms |
16832 KB |
Output is correct |
27 |
Correct |
163 ms |
26716 KB |
Output is correct |
28 |
Correct |
161 ms |
26652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
244 KB |
Output is correct |
8 |
Correct |
7 ms |
1236 KB |
Output is correct |
9 |
Correct |
155 ms |
25964 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
1364 KB |
Output is correct |
13 |
Correct |
158 ms |
26008 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
3 ms |
852 KB |
Output is correct |
17 |
Correct |
65 ms |
16004 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
58 ms |
6712 KB |
Output is correct |
22 |
Correct |
167 ms |
26032 KB |
Output is correct |
23 |
Correct |
162 ms |
25880 KB |
Output is correct |
24 |
Correct |
186 ms |
26020 KB |
Output is correct |
25 |
Correct |
79 ms |
16004 KB |
Output is correct |
26 |
Correct |
62 ms |
16000 KB |
Output is correct |
27 |
Correct |
156 ms |
25888 KB |
Output is correct |
28 |
Correct |
170 ms |
25956 KB |
Output is correct |
29 |
Correct |
66 ms |
16076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
39 ms |
6716 KB |
Output is correct |
5 |
Correct |
170 ms |
26008 KB |
Output is correct |
6 |
Correct |
155 ms |
25880 KB |
Output is correct |
7 |
Correct |
162 ms |
26008 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
42 ms |
6672 KB |
Output is correct |
10 |
Correct |
160 ms |
26000 KB |
Output is correct |
11 |
Correct |
155 ms |
25924 KB |
Output is correct |
12 |
Correct |
190 ms |
26000 KB |
Output is correct |
13 |
Correct |
0 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 |
39 ms |
6676 KB |
Output is correct |
17 |
Correct |
169 ms |
26012 KB |
Output is correct |
18 |
Correct |
165 ms |
26000 KB |
Output is correct |
19 |
Correct |
156 ms |
25944 KB |
Output is correct |
20 |
Correct |
167 ms |
25996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1364 KB |
Output is correct |
7 |
Correct |
155 ms |
26020 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
6 ms |
1236 KB |
Output is correct |
13 |
Correct |
170 ms |
26040 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
3 ms |
852 KB |
Output is correct |
17 |
Correct |
65 ms |
16012 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
42 ms |
7128 KB |
Output is correct |
21 |
Correct |
163 ms |
26828 KB |
Output is correct |
22 |
Correct |
155 ms |
26772 KB |
Output is correct |
23 |
Correct |
161 ms |
26708 KB |
Output is correct |
24 |
Correct |
166 ms |
26764 KB |
Output is correct |
25 |
Correct |
63 ms |
16732 KB |
Output is correct |
26 |
Correct |
64 ms |
16832 KB |
Output is correct |
27 |
Correct |
163 ms |
26716 KB |
Output is correct |
28 |
Correct |
161 ms |
26652 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
244 KB |
Output is correct |
36 |
Correct |
7 ms |
1236 KB |
Output is correct |
37 |
Correct |
155 ms |
25964 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
7 ms |
1364 KB |
Output is correct |
41 |
Correct |
158 ms |
26008 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
0 ms |
212 KB |
Output is correct |
44 |
Correct |
3 ms |
852 KB |
Output is correct |
45 |
Correct |
65 ms |
16004 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
0 ms |
212 KB |
Output is correct |
49 |
Correct |
58 ms |
6712 KB |
Output is correct |
50 |
Correct |
167 ms |
26032 KB |
Output is correct |
51 |
Correct |
162 ms |
25880 KB |
Output is correct |
52 |
Correct |
186 ms |
26020 KB |
Output is correct |
53 |
Correct |
79 ms |
16004 KB |
Output is correct |
54 |
Correct |
62 ms |
16000 KB |
Output is correct |
55 |
Correct |
156 ms |
25888 KB |
Output is correct |
56 |
Correct |
170 ms |
25956 KB |
Output is correct |
57 |
Correct |
66 ms |
16076 KB |
Output is correct |
58 |
Correct |
0 ms |
212 KB |
Output is correct |
59 |
Correct |
1 ms |
212 KB |
Output is correct |
60 |
Correct |
3 ms |
948 KB |
Output is correct |
61 |
Correct |
66 ms |
17964 KB |
Output is correct |
62 |
Correct |
0 ms |
212 KB |
Output is correct |
63 |
Correct |
0 ms |
212 KB |
Output is correct |
64 |
Correct |
0 ms |
212 KB |
Output is correct |
65 |
Correct |
40 ms |
7148 KB |
Output is correct |
66 |
Correct |
63 ms |
18040 KB |
Output is correct |
67 |
Correct |
65 ms |
18036 KB |
Output is correct |
68 |
Correct |
65 ms |
17924 KB |
Output is correct |
69 |
Correct |
63 ms |
18040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1364 KB |
Output is correct |
7 |
Correct |
155 ms |
26020 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
6 ms |
1236 KB |
Output is correct |
13 |
Correct |
170 ms |
26040 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
3 ms |
852 KB |
Output is correct |
17 |
Correct |
65 ms |
16012 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
42 ms |
7128 KB |
Output is correct |
21 |
Correct |
163 ms |
26828 KB |
Output is correct |
22 |
Correct |
155 ms |
26772 KB |
Output is correct |
23 |
Correct |
161 ms |
26708 KB |
Output is correct |
24 |
Correct |
166 ms |
26764 KB |
Output is correct |
25 |
Correct |
63 ms |
16732 KB |
Output is correct |
26 |
Correct |
64 ms |
16832 KB |
Output is correct |
27 |
Correct |
163 ms |
26716 KB |
Output is correct |
28 |
Correct |
161 ms |
26652 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
244 KB |
Output is correct |
36 |
Correct |
7 ms |
1236 KB |
Output is correct |
37 |
Correct |
155 ms |
25964 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
7 ms |
1364 KB |
Output is correct |
41 |
Correct |
158 ms |
26008 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
0 ms |
212 KB |
Output is correct |
44 |
Correct |
3 ms |
852 KB |
Output is correct |
45 |
Correct |
65 ms |
16004 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
0 ms |
212 KB |
Output is correct |
49 |
Correct |
58 ms |
6712 KB |
Output is correct |
50 |
Correct |
167 ms |
26032 KB |
Output is correct |
51 |
Correct |
162 ms |
25880 KB |
Output is correct |
52 |
Correct |
186 ms |
26020 KB |
Output is correct |
53 |
Correct |
79 ms |
16004 KB |
Output is correct |
54 |
Correct |
62 ms |
16000 KB |
Output is correct |
55 |
Correct |
156 ms |
25888 KB |
Output is correct |
56 |
Correct |
170 ms |
25956 KB |
Output is correct |
57 |
Correct |
66 ms |
16076 KB |
Output is correct |
58 |
Correct |
0 ms |
212 KB |
Output is correct |
59 |
Correct |
0 ms |
212 KB |
Output is correct |
60 |
Correct |
0 ms |
212 KB |
Output is correct |
61 |
Correct |
39 ms |
6716 KB |
Output is correct |
62 |
Correct |
170 ms |
26008 KB |
Output is correct |
63 |
Correct |
155 ms |
25880 KB |
Output is correct |
64 |
Correct |
162 ms |
26008 KB |
Output is correct |
65 |
Correct |
0 ms |
212 KB |
Output is correct |
66 |
Correct |
42 ms |
6672 KB |
Output is correct |
67 |
Correct |
160 ms |
26000 KB |
Output is correct |
68 |
Correct |
155 ms |
25924 KB |
Output is correct |
69 |
Correct |
190 ms |
26000 KB |
Output is correct |
70 |
Correct |
0 ms |
212 KB |
Output is correct |
71 |
Correct |
0 ms |
212 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
39 ms |
6676 KB |
Output is correct |
74 |
Correct |
169 ms |
26012 KB |
Output is correct |
75 |
Correct |
165 ms |
26000 KB |
Output is correct |
76 |
Correct |
156 ms |
25944 KB |
Output is correct |
77 |
Correct |
167 ms |
25996 KB |
Output is correct |
78 |
Correct |
0 ms |
212 KB |
Output is correct |
79 |
Correct |
1 ms |
212 KB |
Output is correct |
80 |
Correct |
3 ms |
948 KB |
Output is correct |
81 |
Correct |
66 ms |
17964 KB |
Output is correct |
82 |
Correct |
0 ms |
212 KB |
Output is correct |
83 |
Correct |
0 ms |
212 KB |
Output is correct |
84 |
Correct |
0 ms |
212 KB |
Output is correct |
85 |
Correct |
40 ms |
7148 KB |
Output is correct |
86 |
Correct |
63 ms |
18040 KB |
Output is correct |
87 |
Correct |
65 ms |
18036 KB |
Output is correct |
88 |
Correct |
65 ms |
17924 KB |
Output is correct |
89 |
Correct |
63 ms |
18040 KB |
Output is correct |
90 |
Correct |
0 ms |
212 KB |
Output is correct |
91 |
Correct |
0 ms |
296 KB |
Output is correct |
92 |
Correct |
3 ms |
980 KB |
Output is correct |
93 |
Correct |
65 ms |
18040 KB |
Output is correct |
94 |
Correct |
1 ms |
212 KB |
Output is correct |
95 |
Correct |
0 ms |
212 KB |
Output is correct |
96 |
Correct |
0 ms |
212 KB |
Output is correct |
97 |
Correct |
17 ms |
4716 KB |
Output is correct |
98 |
Correct |
64 ms |
18040 KB |
Output is correct |
99 |
Correct |
62 ms |
18040 KB |
Output is correct |
100 |
Correct |
72 ms |
17924 KB |
Output is correct |
101 |
Correct |
65 ms |
18064 KB |
Output is correct |
102 |
Correct |
69 ms |
18016 KB |
Output is correct |
103 |
Correct |
80 ms |
18072 KB |
Output is correct |
104 |
Correct |
72 ms |
18072 KB |
Output is correct |
105 |
Correct |
66 ms |
18040 KB |
Output is correct |