#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++){
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
6 |
Correct |
5 ms |
1116 KB |
Output is correct |
7 |
Correct |
115 ms |
22048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
6 |
Correct |
5 ms |
1116 KB |
Output is correct |
7 |
Correct |
115 ms |
22048 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 |
1116 KB |
Output is correct |
13 |
Correct |
111 ms |
22036 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
604 KB |
Output is correct |
17 |
Correct |
62 ms |
8220 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
29 ms |
5724 KB |
Output is correct |
21 |
Correct |
120 ms |
22100 KB |
Output is correct |
22 |
Correct |
113 ms |
22096 KB |
Output is correct |
23 |
Correct |
124 ms |
22096 KB |
Output is correct |
24 |
Correct |
113 ms |
22096 KB |
Output is correct |
25 |
Correct |
49 ms |
8220 KB |
Output is correct |
26 |
Correct |
51 ms |
8220 KB |
Output is correct |
27 |
Correct |
121 ms |
22016 KB |
Output is correct |
28 |
Correct |
115 ms |
22096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
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 |
5 ms |
1296 KB |
Output is correct |
9 |
Correct |
118 ms |
22100 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
5 ms |
1112 KB |
Output is correct |
13 |
Correct |
118 ms |
22040 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
604 KB |
Output is correct |
17 |
Correct |
60 ms |
8280 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 |
5716 KB |
Output is correct |
22 |
Correct |
127 ms |
22100 KB |
Output is correct |
23 |
Correct |
121 ms |
22100 KB |
Output is correct |
24 |
Correct |
124 ms |
22100 KB |
Output is correct |
25 |
Correct |
49 ms |
12304 KB |
Output is correct |
26 |
Correct |
50 ms |
8204 KB |
Output is correct |
27 |
Correct |
114 ms |
22008 KB |
Output is correct |
28 |
Correct |
120 ms |
22044 KB |
Output is correct |
29 |
Correct |
50 ms |
12112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
28 ms |
5860 KB |
Output is correct |
5 |
Correct |
116 ms |
22024 KB |
Output is correct |
6 |
Correct |
132 ms |
22048 KB |
Output is correct |
7 |
Correct |
123 ms |
22096 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
29 ms |
5856 KB |
Output is correct |
10 |
Correct |
117 ms |
22100 KB |
Output is correct |
11 |
Correct |
114 ms |
22040 KB |
Output is correct |
12 |
Correct |
122 ms |
22028 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
29 ms |
5724 KB |
Output is correct |
17 |
Correct |
126 ms |
22028 KB |
Output is correct |
18 |
Correct |
115 ms |
22096 KB |
Output is correct |
19 |
Correct |
113 ms |
22044 KB |
Output is correct |
20 |
Correct |
116 ms |
22032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
6 |
Correct |
5 ms |
1116 KB |
Output is correct |
7 |
Correct |
115 ms |
22048 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 |
1116 KB |
Output is correct |
13 |
Correct |
111 ms |
22036 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
604 KB |
Output is correct |
17 |
Correct |
62 ms |
8220 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
29 ms |
5724 KB |
Output is correct |
21 |
Correct |
120 ms |
22100 KB |
Output is correct |
22 |
Correct |
113 ms |
22096 KB |
Output is correct |
23 |
Correct |
124 ms |
22096 KB |
Output is correct |
24 |
Correct |
113 ms |
22096 KB |
Output is correct |
25 |
Correct |
49 ms |
8220 KB |
Output is correct |
26 |
Correct |
51 ms |
8220 KB |
Output is correct |
27 |
Correct |
121 ms |
22016 KB |
Output is correct |
28 |
Correct |
115 ms |
22096 KB |
Output is correct |
29 |
Correct |
0 ms |
344 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 |
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 |
5 ms |
1296 KB |
Output is correct |
37 |
Correct |
118 ms |
22100 KB |
Output is correct |
38 |
Correct |
1 ms |
344 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
5 ms |
1112 KB |
Output is correct |
41 |
Correct |
118 ms |
22040 KB |
Output is correct |
42 |
Correct |
1 ms |
344 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
3 ms |
604 KB |
Output is correct |
45 |
Correct |
60 ms |
8280 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 |
5716 KB |
Output is correct |
50 |
Correct |
127 ms |
22100 KB |
Output is correct |
51 |
Correct |
121 ms |
22100 KB |
Output is correct |
52 |
Correct |
124 ms |
22100 KB |
Output is correct |
53 |
Correct |
49 ms |
12304 KB |
Output is correct |
54 |
Correct |
50 ms |
8204 KB |
Output is correct |
55 |
Correct |
114 ms |
22008 KB |
Output is correct |
56 |
Correct |
120 ms |
22044 KB |
Output is correct |
57 |
Correct |
50 ms |
12112 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
3 ms |
604 KB |
Output is correct |
61 |
Correct |
57 ms |
8216 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
0 ms |
424 KB |
Output is correct |
64 |
Correct |
0 ms |
344 KB |
Output is correct |
65 |
Correct |
29 ms |
5900 KB |
Output is correct |
66 |
Correct |
49 ms |
8272 KB |
Output is correct |
67 |
Correct |
48 ms |
8280 KB |
Output is correct |
68 |
Correct |
49 ms |
8272 KB |
Output is correct |
69 |
Correct |
51 ms |
12112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
6 |
Correct |
5 ms |
1116 KB |
Output is correct |
7 |
Correct |
115 ms |
22048 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 |
1116 KB |
Output is correct |
13 |
Correct |
111 ms |
22036 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
604 KB |
Output is correct |
17 |
Correct |
62 ms |
8220 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
29 ms |
5724 KB |
Output is correct |
21 |
Correct |
120 ms |
22100 KB |
Output is correct |
22 |
Correct |
113 ms |
22096 KB |
Output is correct |
23 |
Correct |
124 ms |
22096 KB |
Output is correct |
24 |
Correct |
113 ms |
22096 KB |
Output is correct |
25 |
Correct |
49 ms |
8220 KB |
Output is correct |
26 |
Correct |
51 ms |
8220 KB |
Output is correct |
27 |
Correct |
121 ms |
22016 KB |
Output is correct |
28 |
Correct |
115 ms |
22096 KB |
Output is correct |
29 |
Correct |
0 ms |
344 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 |
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 |
5 ms |
1296 KB |
Output is correct |
37 |
Correct |
118 ms |
22100 KB |
Output is correct |
38 |
Correct |
1 ms |
344 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
5 ms |
1112 KB |
Output is correct |
41 |
Correct |
118 ms |
22040 KB |
Output is correct |
42 |
Correct |
1 ms |
344 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
3 ms |
604 KB |
Output is correct |
45 |
Correct |
60 ms |
8280 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 |
5716 KB |
Output is correct |
50 |
Correct |
127 ms |
22100 KB |
Output is correct |
51 |
Correct |
121 ms |
22100 KB |
Output is correct |
52 |
Correct |
124 ms |
22100 KB |
Output is correct |
53 |
Correct |
49 ms |
12304 KB |
Output is correct |
54 |
Correct |
50 ms |
8204 KB |
Output is correct |
55 |
Correct |
114 ms |
22008 KB |
Output is correct |
56 |
Correct |
120 ms |
22044 KB |
Output is correct |
57 |
Correct |
50 ms |
12112 KB |
Output is correct |
58 |
Correct |
0 ms |
344 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
1 ms |
344 KB |
Output is correct |
61 |
Correct |
28 ms |
5860 KB |
Output is correct |
62 |
Correct |
116 ms |
22024 KB |
Output is correct |
63 |
Correct |
132 ms |
22048 KB |
Output is correct |
64 |
Correct |
123 ms |
22096 KB |
Output is correct |
65 |
Correct |
0 ms |
344 KB |
Output is correct |
66 |
Correct |
29 ms |
5856 KB |
Output is correct |
67 |
Correct |
117 ms |
22100 KB |
Output is correct |
68 |
Correct |
114 ms |
22040 KB |
Output is correct |
69 |
Correct |
122 ms |
22028 KB |
Output is correct |
70 |
Correct |
0 ms |
348 KB |
Output is correct |
71 |
Correct |
0 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
29 ms |
5724 KB |
Output is correct |
74 |
Correct |
126 ms |
22028 KB |
Output is correct |
75 |
Correct |
115 ms |
22096 KB |
Output is correct |
76 |
Correct |
113 ms |
22044 KB |
Output is correct |
77 |
Correct |
116 ms |
22032 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Correct |
0 ms |
348 KB |
Output is correct |
80 |
Correct |
3 ms |
604 KB |
Output is correct |
81 |
Correct |
57 ms |
8216 KB |
Output is correct |
82 |
Correct |
0 ms |
348 KB |
Output is correct |
83 |
Correct |
0 ms |
424 KB |
Output is correct |
84 |
Correct |
0 ms |
344 KB |
Output is correct |
85 |
Correct |
29 ms |
5900 KB |
Output is correct |
86 |
Correct |
49 ms |
8272 KB |
Output is correct |
87 |
Correct |
48 ms |
8280 KB |
Output is correct |
88 |
Correct |
49 ms |
8272 KB |
Output is correct |
89 |
Correct |
51 ms |
12112 KB |
Output is correct |
90 |
Correct |
1 ms |
348 KB |
Output is correct |
91 |
Correct |
0 ms |
348 KB |
Output is correct |
92 |
Correct |
2 ms |
604 KB |
Output is correct |
93 |
Correct |
58 ms |
8204 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 |
412 KB |
Output is correct |
97 |
Correct |
15 ms |
2396 KB |
Output is correct |
98 |
Correct |
48 ms |
8216 KB |
Output is correct |
99 |
Correct |
50 ms |
8272 KB |
Output is correct |
100 |
Correct |
48 ms |
8276 KB |
Output is correct |
101 |
Correct |
45 ms |
8276 KB |
Output is correct |
102 |
Correct |
67 ms |
8204 KB |
Output is correct |
103 |
Correct |
47 ms |
8272 KB |
Output is correct |
104 |
Correct |
48 ms |
8276 KB |
Output is correct |
105 |
Correct |
51 ms |
8220 KB |
Output is correct |