#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;
#define pb push_back
const int nax=1005;
vector<int> adj[nax];
vector<int> comp[nax];
int parent[nax];
int find(int x){
if(x==parent[x]) return x;
return parent[x]=find(parent[x]);
}
bool sameset(int x,int y){
return find(x)==find(y);
}
void joinset(int x,int y){
x=find(x);
y=find(y);
if(comp[y].size()<comp[x].size()) swap(comp[x],comp[y]);
for(auto u:comp[x]) comp[y].pb(u);
parent[x]=y;
}
int construct(std::vector<std::vector<int>> p) {
int n = p.size();
std::vector<std::vector<int>> ans(n,vector<int> (n,0));
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
if(i==j) continue;
if(p[i][j]>0) adj[i].pb(j);
}
}
for (int i = 0; i < n; ++i)
{
parent[i]=i;
}
for (int i = 0; i < n; ++i)
{
for(auto u:adj[i])
if(!sameset(u,i)) joinset(u,i);
}
bool test=true;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
if(i==j) continue;
if(p[i][j]==3) test=false;
if(p[i][j]==0&&sameset(i,j)) test=false;
}
}
if(!test) return test;
for (int i = 0; i < n; ++i)
{
parent[i]=i;
adj[i].clear();
}
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
if(i==j) continue;
if(p[i][j]==1&&!sameset(j,i)){
adj[i].pb(j);
adj[j].pb(i);
joinset(j,i);
}
}
}
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
if(i==j) continue;
if(p[i][j]==2&&sameset(i,j)) test=false;
}
}
if(!test) return test;
for (int i = 0; i < n; ++i)
{
if(comp[find(i)].empty()) comp[find(i)].pb(find(i));
}
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
if(i==j) continue;
if(p[i][j]==2&&!sameset(i,j)){
joinset(i,j);
}
}
}
bool vis[n];
memset(vis,0,sizeof vis);
for (int i = 0; i < n; ++i)
{
if(!vis[find(i)]){
int x=find(i);
vis[x]=true;
if(comp[x].size()==1) continue;
if(comp[x].size()==2){
test=false;
break;
}
for (int j = 0; j < comp[x].size(); ++j)
{
adj[comp[x][j]].pb(comp[x][(j+1)%(int)comp[x].size()]);
adj[comp[x][(j+1)%(int)comp[x].size()]].pb(comp[x][j]);
}
}
}
if(!test) return 0;
for (int i = 0; i < n; ++i)
{
for(auto u:adj[i]) ans[i][u]=1;
}
build(ans);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:106:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
106 | for (int j = 0; j < comp[x].size(); ++j)
| ~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
4 ms |
1372 KB |
Output is correct |
7 |
Correct |
107 ms |
26200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
4 ms |
1372 KB |
Output is correct |
7 |
Correct |
107 ms |
26200 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
4 ms |
1372 KB |
Output is correct |
13 |
Correct |
100 ms |
22096 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
860 KB |
Output is correct |
17 |
Correct |
61 ms |
14668 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
30 ms |
5968 KB |
Output is correct |
21 |
Correct |
105 ms |
22612 KB |
Output is correct |
22 |
Correct |
102 ms |
22100 KB |
Output is correct |
23 |
Correct |
110 ms |
24144 KB |
Output is correct |
24 |
Correct |
102 ms |
22096 KB |
Output is correct |
25 |
Correct |
47 ms |
12628 KB |
Output is correct |
26 |
Correct |
58 ms |
12104 KB |
Output is correct |
27 |
Correct |
128 ms |
25172 KB |
Output is correct |
28 |
Correct |
99 ms |
22100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
4 ms |
1372 KB |
Output is correct |
9 |
Correct |
120 ms |
22092 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 |
1372 KB |
Output is correct |
13 |
Correct |
113 ms |
26184 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
60 ms |
14652 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
27 ms |
5848 KB |
Output is correct |
22 |
Correct |
103 ms |
22616 KB |
Output is correct |
23 |
Correct |
108 ms |
22096 KB |
Output is correct |
24 |
Correct |
112 ms |
25168 KB |
Output is correct |
25 |
Correct |
45 ms |
12108 KB |
Output is correct |
26 |
Correct |
47 ms |
12604 KB |
Output is correct |
27 |
Correct |
105 ms |
22096 KB |
Output is correct |
28 |
Correct |
109 ms |
24148 KB |
Output is correct |
29 |
Correct |
46 ms |
12116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
25 ms |
5836 KB |
Output is correct |
5 |
Correct |
106 ms |
22560 KB |
Output is correct |
6 |
Correct |
102 ms |
22096 KB |
Output is correct |
7 |
Correct |
110 ms |
24124 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
25 ms |
5840 KB |
Output is correct |
10 |
Correct |
101 ms |
22612 KB |
Output is correct |
11 |
Correct |
99 ms |
22096 KB |
Output is correct |
12 |
Correct |
111 ms |
25172 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 |
27 ms |
5836 KB |
Output is correct |
17 |
Correct |
102 ms |
22560 KB |
Output is correct |
18 |
Correct |
111 ms |
22608 KB |
Output is correct |
19 |
Correct |
101 ms |
22244 KB |
Output is correct |
20 |
Correct |
98 ms |
22092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
4 ms |
1372 KB |
Output is correct |
7 |
Correct |
107 ms |
26200 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
4 ms |
1372 KB |
Output is correct |
13 |
Correct |
100 ms |
22096 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
860 KB |
Output is correct |
17 |
Correct |
61 ms |
14668 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
30 ms |
5968 KB |
Output is correct |
21 |
Correct |
105 ms |
22612 KB |
Output is correct |
22 |
Correct |
102 ms |
22100 KB |
Output is correct |
23 |
Correct |
110 ms |
24144 KB |
Output is correct |
24 |
Correct |
102 ms |
22096 KB |
Output is correct |
25 |
Correct |
47 ms |
12628 KB |
Output is correct |
26 |
Correct |
58 ms |
12104 KB |
Output is correct |
27 |
Correct |
128 ms |
25172 KB |
Output is correct |
28 |
Correct |
99 ms |
22100 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 |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
4 ms |
1372 KB |
Output is correct |
37 |
Correct |
120 ms |
22092 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 |
1372 KB |
Output is correct |
41 |
Correct |
113 ms |
26184 KB |
Output is correct |
42 |
Correct |
1 ms |
344 KB |
Output is correct |
43 |
Correct |
0 ms |
344 KB |
Output is correct |
44 |
Correct |
3 ms |
860 KB |
Output is correct |
45 |
Correct |
60 ms |
14652 KB |
Output is correct |
46 |
Correct |
0 ms |
344 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
27 ms |
5848 KB |
Output is correct |
50 |
Correct |
103 ms |
22616 KB |
Output is correct |
51 |
Correct |
108 ms |
22096 KB |
Output is correct |
52 |
Correct |
112 ms |
25168 KB |
Output is correct |
53 |
Correct |
45 ms |
12108 KB |
Output is correct |
54 |
Correct |
47 ms |
12604 KB |
Output is correct |
55 |
Correct |
105 ms |
22096 KB |
Output is correct |
56 |
Correct |
109 ms |
24148 KB |
Output is correct |
57 |
Correct |
46 ms |
12116 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
1 ms |
344 KB |
Output is correct |
60 |
Correct |
3 ms |
1116 KB |
Output is correct |
61 |
Correct |
57 ms |
16204 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
25 ms |
5968 KB |
Output is correct |
66 |
Correct |
51 ms |
12880 KB |
Output is correct |
67 |
Correct |
48 ms |
12624 KB |
Output is correct |
68 |
Correct |
45 ms |
12368 KB |
Output is correct |
69 |
Correct |
46 ms |
12112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
4 ms |
1372 KB |
Output is correct |
7 |
Correct |
107 ms |
26200 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
4 ms |
1372 KB |
Output is correct |
13 |
Correct |
100 ms |
22096 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
860 KB |
Output is correct |
17 |
Correct |
61 ms |
14668 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
30 ms |
5968 KB |
Output is correct |
21 |
Correct |
105 ms |
22612 KB |
Output is correct |
22 |
Correct |
102 ms |
22100 KB |
Output is correct |
23 |
Correct |
110 ms |
24144 KB |
Output is correct |
24 |
Correct |
102 ms |
22096 KB |
Output is correct |
25 |
Correct |
47 ms |
12628 KB |
Output is correct |
26 |
Correct |
58 ms |
12104 KB |
Output is correct |
27 |
Correct |
128 ms |
25172 KB |
Output is correct |
28 |
Correct |
99 ms |
22100 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 |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
4 ms |
1372 KB |
Output is correct |
37 |
Correct |
120 ms |
22092 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 |
1372 KB |
Output is correct |
41 |
Correct |
113 ms |
26184 KB |
Output is correct |
42 |
Correct |
1 ms |
344 KB |
Output is correct |
43 |
Correct |
0 ms |
344 KB |
Output is correct |
44 |
Correct |
3 ms |
860 KB |
Output is correct |
45 |
Correct |
60 ms |
14652 KB |
Output is correct |
46 |
Correct |
0 ms |
344 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
27 ms |
5848 KB |
Output is correct |
50 |
Correct |
103 ms |
22616 KB |
Output is correct |
51 |
Correct |
108 ms |
22096 KB |
Output is correct |
52 |
Correct |
112 ms |
25168 KB |
Output is correct |
53 |
Correct |
45 ms |
12108 KB |
Output is correct |
54 |
Correct |
47 ms |
12604 KB |
Output is correct |
55 |
Correct |
105 ms |
22096 KB |
Output is correct |
56 |
Correct |
109 ms |
24148 KB |
Output is correct |
57 |
Correct |
46 ms |
12116 KB |
Output is correct |
58 |
Correct |
0 ms |
344 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
25 ms |
5836 KB |
Output is correct |
62 |
Correct |
106 ms |
22560 KB |
Output is correct |
63 |
Correct |
102 ms |
22096 KB |
Output is correct |
64 |
Correct |
110 ms |
24124 KB |
Output is correct |
65 |
Correct |
1 ms |
344 KB |
Output is correct |
66 |
Correct |
25 ms |
5840 KB |
Output is correct |
67 |
Correct |
101 ms |
22612 KB |
Output is correct |
68 |
Correct |
99 ms |
22096 KB |
Output is correct |
69 |
Correct |
111 ms |
25172 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 |
27 ms |
5836 KB |
Output is correct |
74 |
Correct |
102 ms |
22560 KB |
Output is correct |
75 |
Correct |
111 ms |
22608 KB |
Output is correct |
76 |
Correct |
101 ms |
22244 KB |
Output is correct |
77 |
Correct |
98 ms |
22092 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Correct |
1 ms |
344 KB |
Output is correct |
80 |
Correct |
3 ms |
1116 KB |
Output is correct |
81 |
Correct |
57 ms |
16204 KB |
Output is correct |
82 |
Correct |
0 ms |
348 KB |
Output is correct |
83 |
Correct |
0 ms |
348 KB |
Output is correct |
84 |
Correct |
0 ms |
348 KB |
Output is correct |
85 |
Correct |
25 ms |
5968 KB |
Output is correct |
86 |
Correct |
51 ms |
12880 KB |
Output is correct |
87 |
Correct |
48 ms |
12624 KB |
Output is correct |
88 |
Correct |
45 ms |
12368 KB |
Output is correct |
89 |
Correct |
46 ms |
12112 KB |
Output is correct |
90 |
Correct |
0 ms |
348 KB |
Output is correct |
91 |
Correct |
1 ms |
348 KB |
Output is correct |
92 |
Correct |
3 ms |
1116 KB |
Output is correct |
93 |
Correct |
58 ms |
16348 KB |
Output is correct |
94 |
Correct |
0 ms |
344 KB |
Output is correct |
95 |
Correct |
0 ms |
348 KB |
Output is correct |
96 |
Correct |
0 ms |
348 KB |
Output is correct |
97 |
Correct |
11 ms |
3896 KB |
Output is correct |
98 |
Correct |
47 ms |
14780 KB |
Output is correct |
99 |
Correct |
47 ms |
14672 KB |
Output is correct |
100 |
Correct |
47 ms |
14400 KB |
Output is correct |
101 |
Correct |
51 ms |
14140 KB |
Output is correct |
102 |
Correct |
51 ms |
18260 KB |
Output is correct |
103 |
Correct |
56 ms |
18300 KB |
Output is correct |
104 |
Correct |
54 ms |
18256 KB |
Output is correct |
105 |
Correct |
63 ms |
16720 KB |
Output is correct |