#include<bits/stdc++.h>
#include "supertrees.h"
using namespace std;
vector<vector<int>> components;
int color[1005], sz = 0, color_count[1005], color_link[1005];
/*void build(vector<vector<int>> ans)
{
int n = ans.size();
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++) cout<<ans[i][j]<<" ";
cout<<endl;
}
}*/
int construct(vector<vector<int>> p)
{
int n = p.size();
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++) if(p[i][j] == 3) return 0;
}
bool ok = 1;
for(int i = 0; i < n; i++) if(color[i] == 0){
color[i] = ++sz;
queue<int> w; w.push(i);
vector<int> cur;
while(w.size() > 0){
int u = w.front(); w.pop(); cur.push_back(u);
color_count[color[u]]++;
for(int v = 0; v < n; v++) if(p[u][v] > 0){
if(color[v] == 0){
color[v] = color[u];
w.push(v);
}
if(color[v] == color[u]) color_link[color[u]]++;
else ok = 0;
}
}
components.push_back(cur);
}
for(int i = 1; i <= sz; i++) if(color_count[i] * color_count[i] != color_link[i]) ok = 0;
if(ok == 0) return 0;
//Solve for each component
vector<vector<int>> ans(n);
for(int i = 0; i < n; i++) ans[i].resize(n);
for(vector<int> c : components){
memset(color, 0, sizeof(color)); sz = 0;
memset(color_count, 0, sizeof(color_count));
memset(color_link, 0, sizeof(color_link));
vector<vector<int>> small_component;
for(int i : c) if(color[i] == 0){
color[i] = ++sz;
queue<int> w; w.push(i);
vector<int> cur;
while(w.size() > 0){
int u = w.front(); w.pop(); cur.push_back(u);
color_count[color[u]]++;
for(int v : c) if(p[u][v] == 1){
if(color[v] == 0){
color[v] = color[u];
w.push(v);
}
if(color[v] == color[u]) color_link[color[u]]++;
else ok = 0;
}
}
if(color_count[sz] * color_count[sz] != color_link[sz]) return 0;
small_component.push_back(cur);
}
if(small_component.size() == 2) return 0;
int m = small_component.size();
for(vector<int> d : small_component){
for(int i = 0; i < d.size()-1; i++){
ans[d[i]][d[i+1]] = 1;
ans[d[i+1]][d[i]] = 1;
}
}
if(m > 1){
for(int i = 0; i < m-1; i++){
ans[small_component[i][0]][small_component[i+1][0]] = 1;
ans[small_component[i+1][0]][small_component[i][0]] = 1;
}
ans[small_component[m-1][0]][small_component[0][0]] = 1;
ans[small_component[0][0]][small_component[m-1][0]] = 1;
}
}
build(ans);
return 1;
}
/*int main()
{
int n;
cin>>n;
vector<vector<int>> p(n);
for(int i = 0; i < n; i++) p[i].resize(n);
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++) cin>>p[i][j];
}
cout<<construct(p);
}*/
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:71:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | for(int i = 0; i < d.size()-1; i++){
| ~~^~~~~~~~~~~~
# |
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 |
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 |
5 ms |
1372 KB |
Output is correct |
7 |
Correct |
126 ms |
23948 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 |
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 |
5 ms |
1372 KB |
Output is correct |
7 |
Correct |
126 ms |
23948 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 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 |
1192 KB |
Output is correct |
13 |
Correct |
131 ms |
24144 KB |
Output is correct |
14 |
Correct |
0 ms |
600 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
680 KB |
Output is correct |
17 |
Correct |
65 ms |
10252 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
32 ms |
6228 KB |
Output is correct |
21 |
Correct |
129 ms |
24072 KB |
Output is correct |
22 |
Correct |
124 ms |
24060 KB |
Output is correct |
23 |
Correct |
135 ms |
24148 KB |
Output is correct |
24 |
Correct |
118 ms |
24200 KB |
Output is correct |
25 |
Correct |
54 ms |
10068 KB |
Output is correct |
26 |
Correct |
51 ms |
10124 KB |
Output is correct |
27 |
Correct |
140 ms |
24144 KB |
Output is correct |
28 |
Correct |
128 ms |
24064 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 |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
6 ms |
1188 KB |
Output is correct |
9 |
Correct |
138 ms |
24156 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 |
1220 KB |
Output is correct |
13 |
Correct |
129 ms |
23952 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
604 KB |
Output is correct |
17 |
Correct |
68 ms |
10068 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 |
33 ms |
6648 KB |
Output is correct |
22 |
Correct |
141 ms |
24136 KB |
Output is correct |
23 |
Correct |
115 ms |
23984 KB |
Output is correct |
24 |
Correct |
133 ms |
23984 KB |
Output is correct |
25 |
Correct |
50 ms |
14164 KB |
Output is correct |
26 |
Correct |
48 ms |
10072 KB |
Output is correct |
27 |
Correct |
125 ms |
24144 KB |
Output is correct |
28 |
Correct |
125 ms |
24060 KB |
Output is correct |
29 |
Correct |
51 ms |
14160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
440 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
29 ms |
6160 KB |
Output is correct |
5 |
Correct |
129 ms |
24144 KB |
Output is correct |
6 |
Correct |
126 ms |
24148 KB |
Output is correct |
7 |
Correct |
127 ms |
24148 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
29 ms |
6224 KB |
Output is correct |
10 |
Correct |
125 ms |
24144 KB |
Output is correct |
11 |
Correct |
118 ms |
24144 KB |
Output is correct |
12 |
Correct |
125 ms |
23936 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
28 ms |
6196 KB |
Output is correct |
17 |
Correct |
120 ms |
24148 KB |
Output is correct |
18 |
Correct |
126 ms |
24152 KB |
Output is correct |
19 |
Correct |
118 ms |
23980 KB |
Output is correct |
20 |
Correct |
116 ms |
24144 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 |
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 |
5 ms |
1372 KB |
Output is correct |
7 |
Correct |
126 ms |
23948 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 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 |
1192 KB |
Output is correct |
13 |
Correct |
131 ms |
24144 KB |
Output is correct |
14 |
Correct |
0 ms |
600 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
680 KB |
Output is correct |
17 |
Correct |
65 ms |
10252 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
32 ms |
6228 KB |
Output is correct |
21 |
Correct |
129 ms |
24072 KB |
Output is correct |
22 |
Correct |
124 ms |
24060 KB |
Output is correct |
23 |
Correct |
135 ms |
24148 KB |
Output is correct |
24 |
Correct |
118 ms |
24200 KB |
Output is correct |
25 |
Correct |
54 ms |
10068 KB |
Output is correct |
26 |
Correct |
51 ms |
10124 KB |
Output is correct |
27 |
Correct |
140 ms |
24144 KB |
Output is correct |
28 |
Correct |
128 ms |
24064 KB |
Output is correct |
29 |
Correct |
0 ms |
348 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 |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
6 ms |
1188 KB |
Output is correct |
37 |
Correct |
138 ms |
24156 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 |
1220 KB |
Output is correct |
41 |
Correct |
129 ms |
23952 KB |
Output is correct |
42 |
Correct |
1 ms |
344 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
3 ms |
604 KB |
Output is correct |
45 |
Correct |
68 ms |
10068 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 |
33 ms |
6648 KB |
Output is correct |
50 |
Correct |
141 ms |
24136 KB |
Output is correct |
51 |
Correct |
115 ms |
23984 KB |
Output is correct |
52 |
Correct |
133 ms |
23984 KB |
Output is correct |
53 |
Correct |
50 ms |
14164 KB |
Output is correct |
54 |
Correct |
48 ms |
10072 KB |
Output is correct |
55 |
Correct |
125 ms |
24144 KB |
Output is correct |
56 |
Correct |
125 ms |
24060 KB |
Output is correct |
57 |
Correct |
51 ms |
14160 KB |
Output is correct |
58 |
Correct |
0 ms |
344 KB |
Output is correct |
59 |
Correct |
1 ms |
344 KB |
Output is correct |
60 |
Correct |
3 ms |
600 KB |
Output is correct |
61 |
Correct |
63 ms |
10128 KB |
Output is correct |
62 |
Correct |
0 ms |
344 KB |
Output is correct |
63 |
Correct |
1 ms |
348 KB |
Output is correct |
64 |
Correct |
1 ms |
348 KB |
Output is correct |
65 |
Correct |
34 ms |
6228 KB |
Output is correct |
66 |
Correct |
50 ms |
10064 KB |
Output is correct |
67 |
Correct |
52 ms |
10068 KB |
Output is correct |
68 |
Correct |
50 ms |
10252 KB |
Output is correct |
69 |
Correct |
51 ms |
14160 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 |
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 |
5 ms |
1372 KB |
Output is correct |
7 |
Correct |
126 ms |
23948 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 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 |
1192 KB |
Output is correct |
13 |
Correct |
131 ms |
24144 KB |
Output is correct |
14 |
Correct |
0 ms |
600 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
680 KB |
Output is correct |
17 |
Correct |
65 ms |
10252 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
32 ms |
6228 KB |
Output is correct |
21 |
Correct |
129 ms |
24072 KB |
Output is correct |
22 |
Correct |
124 ms |
24060 KB |
Output is correct |
23 |
Correct |
135 ms |
24148 KB |
Output is correct |
24 |
Correct |
118 ms |
24200 KB |
Output is correct |
25 |
Correct |
54 ms |
10068 KB |
Output is correct |
26 |
Correct |
51 ms |
10124 KB |
Output is correct |
27 |
Correct |
140 ms |
24144 KB |
Output is correct |
28 |
Correct |
128 ms |
24064 KB |
Output is correct |
29 |
Correct |
0 ms |
348 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 |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
6 ms |
1188 KB |
Output is correct |
37 |
Correct |
138 ms |
24156 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 |
1220 KB |
Output is correct |
41 |
Correct |
129 ms |
23952 KB |
Output is correct |
42 |
Correct |
1 ms |
344 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
3 ms |
604 KB |
Output is correct |
45 |
Correct |
68 ms |
10068 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 |
33 ms |
6648 KB |
Output is correct |
50 |
Correct |
141 ms |
24136 KB |
Output is correct |
51 |
Correct |
115 ms |
23984 KB |
Output is correct |
52 |
Correct |
133 ms |
23984 KB |
Output is correct |
53 |
Correct |
50 ms |
14164 KB |
Output is correct |
54 |
Correct |
48 ms |
10072 KB |
Output is correct |
55 |
Correct |
125 ms |
24144 KB |
Output is correct |
56 |
Correct |
125 ms |
24060 KB |
Output is correct |
57 |
Correct |
51 ms |
14160 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
0 ms |
440 KB |
Output is correct |
60 |
Correct |
1 ms |
348 KB |
Output is correct |
61 |
Correct |
29 ms |
6160 KB |
Output is correct |
62 |
Correct |
129 ms |
24144 KB |
Output is correct |
63 |
Correct |
126 ms |
24148 KB |
Output is correct |
64 |
Correct |
127 ms |
24148 KB |
Output is correct |
65 |
Correct |
1 ms |
344 KB |
Output is correct |
66 |
Correct |
29 ms |
6224 KB |
Output is correct |
67 |
Correct |
125 ms |
24144 KB |
Output is correct |
68 |
Correct |
118 ms |
24144 KB |
Output is correct |
69 |
Correct |
125 ms |
23936 KB |
Output is correct |
70 |
Correct |
0 ms |
348 KB |
Output is correct |
71 |
Correct |
1 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
28 ms |
6196 KB |
Output is correct |
74 |
Correct |
120 ms |
24148 KB |
Output is correct |
75 |
Correct |
126 ms |
24152 KB |
Output is correct |
76 |
Correct |
118 ms |
23980 KB |
Output is correct |
77 |
Correct |
116 ms |
24144 KB |
Output is correct |
78 |
Correct |
0 ms |
344 KB |
Output is correct |
79 |
Correct |
1 ms |
344 KB |
Output is correct |
80 |
Correct |
3 ms |
600 KB |
Output is correct |
81 |
Correct |
63 ms |
10128 KB |
Output is correct |
82 |
Correct |
0 ms |
344 KB |
Output is correct |
83 |
Correct |
1 ms |
348 KB |
Output is correct |
84 |
Correct |
1 ms |
348 KB |
Output is correct |
85 |
Correct |
34 ms |
6228 KB |
Output is correct |
86 |
Correct |
50 ms |
10064 KB |
Output is correct |
87 |
Correct |
52 ms |
10068 KB |
Output is correct |
88 |
Correct |
50 ms |
10252 KB |
Output is correct |
89 |
Correct |
51 ms |
14160 KB |
Output is correct |
90 |
Correct |
1 ms |
348 KB |
Output is correct |
91 |
Correct |
0 ms |
344 KB |
Output is correct |
92 |
Correct |
4 ms |
1112 KB |
Output is correct |
93 |
Correct |
51 ms |
10248 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 |
14 ms |
2904 KB |
Output is correct |
98 |
Correct |
49 ms |
10248 KB |
Output is correct |
99 |
Correct |
50 ms |
10064 KB |
Output is correct |
100 |
Correct |
48 ms |
10124 KB |
Output is correct |
101 |
Correct |
49 ms |
10064 KB |
Output is correct |
102 |
Correct |
47 ms |
10068 KB |
Output is correct |
103 |
Correct |
49 ms |
10248 KB |
Output is correct |
104 |
Correct |
50 ms |
10064 KB |
Output is correct |
105 |
Correct |
54 ms |
10104 KB |
Output is correct |