#include "supertrees.h"
#include <vector>
using namespace std;
vector<vector<int>> answer;
int group_ids[1000];
vector<int> groups[1000];
vector<vector<int>> p;
int num_groups;
void connect(int a, int b){
answer[a][b]=1;
answer[b][a]=1;
}
int connect_group(int group_id){
vector<int> group=groups[group_id];
vector<bool> in_cycle(group.size(),false);
vector<int> tree_ids(group.size(),-1);
vector<int> trees[1000];
int num_trees=0;
for(int i=0;i<group.size();i++){
if(tree_ids[i]!=-1)
continue;
tree_ids[i]=num_trees;
trees[num_trees].push_back(i);
for(int j=0;j<group.size();j++){
if(i!=j&&p[group[i]][group[j]]==1){
tree_ids[j]=num_trees;
trees[num_trees].push_back(j);
connect(group[i],group[j]);
}
}
num_trees++;
}
for(int i=0;i<group.size();i++)
for(int j=0;j<group.size();j++){
if(tree_ids[i]==tree_ids[j]&&p[group[i]][group[j]]==2)
return 0;
if(tree_ids[i]!=tree_ids[j]&&p[group[i]][group[j]]==1)
return 0;
}
if(num_trees==2)
return 0;
for(int i=0;i<num_trees-1;i++)
connect(group[trees[i][0]],group[trees[i+1][0]]);
if(num_trees!=1)
connect(group[trees[0][0]],group[trees[num_trees-1][0]]);
return 1;
}
int construct(vector<vector<int>> p_) {
p=p_;
int n = p.size();
answer=vector<vector<int>>(n,vector<int>(n,0));
for(vector<int>& i:p)
for(int j:i)
if(j==3)
return 0;
for(int i=0;i<n;i++)
group_ids[i]=-1;
for(int i=0;i<n;i++){
if(group_ids[i]!=-1)
continue;
group_ids[i]=num_groups;
groups[num_groups].push_back(i);
for(int j=i+1;j<n;j++){
if(p[i][j]!=0){
group_ids[j]=num_groups;
groups[num_groups].push_back(j);
}
}
num_groups++;
}
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++){
if(group_ids[i]!=group_ids[j]&&p[i][j]!=0)
return 0;
if(group_ids[i]==group_ids[j]&&p[i][j]==0)
return 0;
}
for(int i=0;i<num_groups;i++)
if(connect_group(i)==0)
return 0;
build(answer);
return 1;
}
Compilation message
supertrees.cpp: In function 'int connect_group(int)':
supertrees.cpp:23:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for(int i=0;i<group.size();i++){
| ~^~~~~~~~~~~~~
supertrees.cpp:28:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | for(int j=0;j<group.size();j++){
| ~^~~~~~~~~~~~~
supertrees.cpp:38:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for(int i=0;i<group.size();i++)
| ~^~~~~~~~~~~~~
supertrees.cpp:39:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int j=0;j<group.size();j++){
| ~^~~~~~~~~~~~~
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:90:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
90 | for(int i=0;i<num_groups;i++)
| ^~~
supertrees.cpp:94:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
94 | build(answer);
| ^~~~~
# |
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 |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1364 KB |
Output is correct |
7 |
Correct |
184 ms |
26036 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 |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1364 KB |
Output is correct |
7 |
Correct |
184 ms |
26036 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
8 ms |
1364 KB |
Output is correct |
13 |
Correct |
175 ms |
26016 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 |
75 ms |
16032 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
64 ms |
6784 KB |
Output is correct |
21 |
Correct |
176 ms |
26012 KB |
Output is correct |
22 |
Correct |
170 ms |
26016 KB |
Output is correct |
23 |
Correct |
176 ms |
26000 KB |
Output is correct |
24 |
Correct |
195 ms |
26020 KB |
Output is correct |
25 |
Correct |
69 ms |
16076 KB |
Output is correct |
26 |
Correct |
66 ms |
16028 KB |
Output is correct |
27 |
Correct |
181 ms |
26024 KB |
Output is correct |
28 |
Correct |
169 ms |
26004 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 |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
8 ms |
1364 KB |
Output is correct |
9 |
Correct |
171 ms |
27956 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
8 ms |
1364 KB |
Output is correct |
13 |
Correct |
174 ms |
28048 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
316 KB |
Output is correct |
16 |
Correct |
4 ms |
980 KB |
Output is correct |
17 |
Correct |
75 ms |
18064 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 |
46 ms |
7204 KB |
Output is correct |
22 |
Correct |
188 ms |
27916 KB |
Output is correct |
23 |
Correct |
169 ms |
28036 KB |
Output is correct |
24 |
Correct |
175 ms |
27884 KB |
Output is correct |
25 |
Correct |
74 ms |
18052 KB |
Output is correct |
26 |
Correct |
91 ms |
18112 KB |
Output is correct |
27 |
Correct |
172 ms |
28044 KB |
Output is correct |
28 |
Correct |
193 ms |
27916 KB |
Output is correct |
29 |
Correct |
73 ms |
18060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
45 ms |
6724 KB |
Output is correct |
5 |
Correct |
177 ms |
26044 KB |
Output is correct |
6 |
Correct |
217 ms |
26004 KB |
Output is correct |
7 |
Correct |
188 ms |
25996 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
58 ms |
6808 KB |
Output is correct |
10 |
Correct |
172 ms |
26004 KB |
Output is correct |
11 |
Correct |
168 ms |
25956 KB |
Output is correct |
12 |
Correct |
188 ms |
25960 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
43 ms |
6804 KB |
Output is correct |
17 |
Correct |
167 ms |
26060 KB |
Output is correct |
18 |
Correct |
177 ms |
25972 KB |
Output is correct |
19 |
Correct |
166 ms |
26032 KB |
Output is correct |
20 |
Correct |
172 ms |
26000 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 |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1364 KB |
Output is correct |
7 |
Correct |
184 ms |
26036 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
8 ms |
1364 KB |
Output is correct |
13 |
Correct |
175 ms |
26016 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 |
75 ms |
16032 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
64 ms |
6784 KB |
Output is correct |
21 |
Correct |
176 ms |
26012 KB |
Output is correct |
22 |
Correct |
170 ms |
26016 KB |
Output is correct |
23 |
Correct |
176 ms |
26000 KB |
Output is correct |
24 |
Correct |
195 ms |
26020 KB |
Output is correct |
25 |
Correct |
69 ms |
16076 KB |
Output is correct |
26 |
Correct |
66 ms |
16028 KB |
Output is correct |
27 |
Correct |
181 ms |
26024 KB |
Output is correct |
28 |
Correct |
169 ms |
26004 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
8 ms |
1364 KB |
Output is correct |
37 |
Correct |
171 ms |
27956 KB |
Output is correct |
38 |
Correct |
1 ms |
316 KB |
Output is correct |
39 |
Correct |
0 ms |
340 KB |
Output is correct |
40 |
Correct |
8 ms |
1364 KB |
Output is correct |
41 |
Correct |
174 ms |
28048 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
0 ms |
316 KB |
Output is correct |
44 |
Correct |
4 ms |
980 KB |
Output is correct |
45 |
Correct |
75 ms |
18064 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 |
46 ms |
7204 KB |
Output is correct |
50 |
Correct |
188 ms |
27916 KB |
Output is correct |
51 |
Correct |
169 ms |
28036 KB |
Output is correct |
52 |
Correct |
175 ms |
27884 KB |
Output is correct |
53 |
Correct |
74 ms |
18052 KB |
Output is correct |
54 |
Correct |
91 ms |
18112 KB |
Output is correct |
55 |
Correct |
172 ms |
28044 KB |
Output is correct |
56 |
Correct |
193 ms |
27916 KB |
Output is correct |
57 |
Correct |
73 ms |
18060 KB |
Output is correct |
58 |
Correct |
0 ms |
212 KB |
Output is correct |
59 |
Correct |
1 ms |
212 KB |
Output is correct |
60 |
Correct |
4 ms |
1036 KB |
Output is correct |
61 |
Correct |
75 ms |
18068 KB |
Output is correct |
62 |
Correct |
1 ms |
212 KB |
Output is correct |
63 |
Correct |
1 ms |
312 KB |
Output is correct |
64 |
Correct |
0 ms |
212 KB |
Output is correct |
65 |
Correct |
44 ms |
7296 KB |
Output is correct |
66 |
Correct |
96 ms |
18048 KB |
Output is correct |
67 |
Correct |
69 ms |
17936 KB |
Output is correct |
68 |
Correct |
70 ms |
18040 KB |
Output is correct |
69 |
Correct |
69 ms |
18052 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 |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1364 KB |
Output is correct |
7 |
Correct |
184 ms |
26036 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
8 ms |
1364 KB |
Output is correct |
13 |
Correct |
175 ms |
26016 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 |
75 ms |
16032 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
64 ms |
6784 KB |
Output is correct |
21 |
Correct |
176 ms |
26012 KB |
Output is correct |
22 |
Correct |
170 ms |
26016 KB |
Output is correct |
23 |
Correct |
176 ms |
26000 KB |
Output is correct |
24 |
Correct |
195 ms |
26020 KB |
Output is correct |
25 |
Correct |
69 ms |
16076 KB |
Output is correct |
26 |
Correct |
66 ms |
16028 KB |
Output is correct |
27 |
Correct |
181 ms |
26024 KB |
Output is correct |
28 |
Correct |
169 ms |
26004 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
8 ms |
1364 KB |
Output is correct |
37 |
Correct |
171 ms |
27956 KB |
Output is correct |
38 |
Correct |
1 ms |
316 KB |
Output is correct |
39 |
Correct |
0 ms |
340 KB |
Output is correct |
40 |
Correct |
8 ms |
1364 KB |
Output is correct |
41 |
Correct |
174 ms |
28048 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
0 ms |
316 KB |
Output is correct |
44 |
Correct |
4 ms |
980 KB |
Output is correct |
45 |
Correct |
75 ms |
18064 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 |
46 ms |
7204 KB |
Output is correct |
50 |
Correct |
188 ms |
27916 KB |
Output is correct |
51 |
Correct |
169 ms |
28036 KB |
Output is correct |
52 |
Correct |
175 ms |
27884 KB |
Output is correct |
53 |
Correct |
74 ms |
18052 KB |
Output is correct |
54 |
Correct |
91 ms |
18112 KB |
Output is correct |
55 |
Correct |
172 ms |
28044 KB |
Output is correct |
56 |
Correct |
193 ms |
27916 KB |
Output is correct |
57 |
Correct |
73 ms |
18060 KB |
Output is correct |
58 |
Correct |
0 ms |
212 KB |
Output is correct |
59 |
Correct |
0 ms |
340 KB |
Output is correct |
60 |
Correct |
0 ms |
212 KB |
Output is correct |
61 |
Correct |
45 ms |
6724 KB |
Output is correct |
62 |
Correct |
177 ms |
26044 KB |
Output is correct |
63 |
Correct |
217 ms |
26004 KB |
Output is correct |
64 |
Correct |
188 ms |
25996 KB |
Output is correct |
65 |
Correct |
0 ms |
212 KB |
Output is correct |
66 |
Correct |
58 ms |
6808 KB |
Output is correct |
67 |
Correct |
172 ms |
26004 KB |
Output is correct |
68 |
Correct |
168 ms |
25956 KB |
Output is correct |
69 |
Correct |
188 ms |
25960 KB |
Output is correct |
70 |
Correct |
1 ms |
212 KB |
Output is correct |
71 |
Correct |
1 ms |
212 KB |
Output is correct |
72 |
Correct |
0 ms |
340 KB |
Output is correct |
73 |
Correct |
43 ms |
6804 KB |
Output is correct |
74 |
Correct |
167 ms |
26060 KB |
Output is correct |
75 |
Correct |
177 ms |
25972 KB |
Output is correct |
76 |
Correct |
166 ms |
26032 KB |
Output is correct |
77 |
Correct |
172 ms |
26000 KB |
Output is correct |
78 |
Correct |
0 ms |
212 KB |
Output is correct |
79 |
Correct |
1 ms |
212 KB |
Output is correct |
80 |
Correct |
4 ms |
1036 KB |
Output is correct |
81 |
Correct |
75 ms |
18068 KB |
Output is correct |
82 |
Correct |
1 ms |
212 KB |
Output is correct |
83 |
Correct |
1 ms |
312 KB |
Output is correct |
84 |
Correct |
0 ms |
212 KB |
Output is correct |
85 |
Correct |
44 ms |
7296 KB |
Output is correct |
86 |
Correct |
96 ms |
18048 KB |
Output is correct |
87 |
Correct |
69 ms |
17936 KB |
Output is correct |
88 |
Correct |
70 ms |
18040 KB |
Output is correct |
89 |
Correct |
69 ms |
18052 KB |
Output is correct |
90 |
Correct |
1 ms |
316 KB |
Output is correct |
91 |
Correct |
1 ms |
340 KB |
Output is correct |
92 |
Correct |
4 ms |
980 KB |
Output is correct |
93 |
Correct |
74 ms |
17976 KB |
Output is correct |
94 |
Correct |
0 ms |
212 KB |
Output is correct |
95 |
Correct |
1 ms |
212 KB |
Output is correct |
96 |
Correct |
1 ms |
212 KB |
Output is correct |
97 |
Correct |
18 ms |
4736 KB |
Output is correct |
98 |
Correct |
66 ms |
18060 KB |
Output is correct |
99 |
Correct |
66 ms |
18052 KB |
Output is correct |
100 |
Correct |
69 ms |
18052 KB |
Output is correct |
101 |
Correct |
66 ms |
18048 KB |
Output is correct |
102 |
Correct |
74 ms |
18048 KB |
Output is correct |
103 |
Correct |
100 ms |
18056 KB |
Output is correct |
104 |
Correct |
67 ms |
18056 KB |
Output is correct |
105 |
Correct |
72 ms |
17976 KB |
Output is correct |