#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;
vector<int> par1(1001),sz1(1001,1),par2(1001),sz2(1001,1),par3(1001),sz3(1003,1);
int findPar1(int u){
if(par1[u]==u) return u;
return par1[u]=findPar1(par1[u]);
}
int findPar2(int u){
if(par2[u]==u) return u;
return par2[u]=findPar2(par2[u]);
}
int findPar3(int u){
if(par3[u]==u) return u;
return par3[u]=findPar3(par3[u]);
}
void unite1(int u,int v){
int ulp=findPar1(u);
int vlp=findPar1(v);
if(ulp==vlp) return;
if(sz1[ulp]>sz1[vlp]){
par1[vlp]=ulp;
sz1[ulp]+=sz1[vlp];
}else{
par1[ulp]=vlp;
sz1[vlp]+=sz1[ulp];
}
}
void unite2(int u,int v){
int ulp=findPar2(u);
int vlp=findPar2(v);
if(ulp==vlp) return;
if(sz2[ulp]>sz2[vlp]){
par2[vlp]=ulp;
sz2[ulp]+=sz2[vlp];
}else{
par2[ulp]=vlp;
sz2[vlp]+=sz2[ulp];
}
}
void unite3(int u,int v){
int ulp=findPar3(u);
int vlp=findPar3(v);
if(ulp==vlp) return;
if(sz3[ulp]>sz3[vlp]){
par3[vlp]=ulp;
sz3[ulp]+=sz3[vlp];
}else{
par3[ulp]=vlp;
sz3[vlp]+=sz3[ulp];
}
}
bool is_samee1(int u,int v){
return findPar1(u)==findPar1(v);
}
bool is_samee2(int u,int v){
return findPar2(u)==findPar2(v);
}
bool is_samee3(int u,int v){
return findPar3(u)==findPar3(v);
}
int construct(vector<vector<int>> p) {
int n = p.size();
vector<vector<int>> adj(n,vector<int>(n,0));
// subtask 4
for(int i=0;i<n;++i) par1[i]=par2[i]=par3[i]=i;
for(int i=0;i<n;++i){
for(int j=i+1;j<n;++j){
if(p[i][j]==2){
unite2(i,j);
unite3(i,j);
}
}
}
for(int i=0;i<n;++i){
for(int j=i+1;j<n;++j){
if(p[i][j]==1&&sz2[i]==1&&sz2[j]==1){
unite1(i,j);
unite3(i,j);
}
}
}
for(int i=0;i<n;++i){
if(sz2[findPar2(i)]==2) return 0;
for(int j=i+1;j<n;++j){
if(p[i][j]==0&&is_samee3(i,j)) return 0;
}
}
vector<int> last_2(n+1,-1),first(n+1,-1),last_1(n+1,-1);
for(int i=0;i<n;++i){
// connecting the 2 ways road
int ulp1=findPar2(i);
if(last_2[ulp1]!=-1&&sz2[ulp1]>2){
adj[i][last_2[ulp1]]=1;
adj[last_2[ulp1]][i]=1;
}
if(first[ulp1]==-1) first[ulp1]=i;
last_2[ulp1]=i;
ulp1=findPar1(i);
if(last_1[ulp1]!=-1&&sz1[ulp1]>1){
adj[i][last_1[ulp1]]=1;
adj[last_1[ulp1]][i]=1;
}
last_1[ulp1]=i;
}
for(int i=0;i<n;++i){
int ulp=findPar2(i);
if(sz2[ulp]>1) adj[last_2[ulp]][first[ulp]]=adj[first[ulp]][last_2[ulp]]=1;
}
for(int i=0;i<n;++i){
for(int j=i+1;j<n;++j){
if(p[i][j]==1&&!is_samee3(i,j)){
unite3(i,j);
int u=findPar3(i),v=findPar3(j);
adj[u][v]=adj[v][u]=1;
}
}
}
/*
for(int i=0;i<n;++i){
for(int j=i+1;j<n;++j){
if(adj[i][j]>0){
cout<<i<<" - "<<j<<'\n';
}
}
}*/
build(adj);
return 1;
}
# |
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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
6 ms |
1372 KB |
Output is correct |
7 |
Correct |
132 ms |
23956 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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
6 ms |
1372 KB |
Output is correct |
7 |
Correct |
132 ms |
23956 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 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 |
1208 KB |
Output is correct |
13 |
Correct |
121 ms |
24148 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 |
856 KB |
Output is correct |
17 |
Correct |
73 ms |
14020 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
43 ms |
6432 KB |
Output is correct |
21 |
Correct |
123 ms |
24148 KB |
Output is correct |
22 |
Correct |
118 ms |
24144 KB |
Output is correct |
23 |
Correct |
144 ms |
24148 KB |
Output is correct |
24 |
Correct |
120 ms |
24148 KB |
Output is correct |
25 |
Correct |
52 ms |
14112 KB |
Output is correct |
26 |
Correct |
63 ms |
14164 KB |
Output is correct |
27 |
Correct |
132 ms |
24148 KB |
Output is correct |
28 |
Correct |
119 ms |
24152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 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 |
6 ms |
1372 KB |
Output is correct |
9 |
Correct |
119 ms |
24052 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
6 ms |
1372 KB |
Output is correct |
13 |
Correct |
121 ms |
24080 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 |
928 KB |
Output is correct |
17 |
Correct |
62 ms |
14164 KB |
Output is correct |
18 |
Correct |
1 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 |
6292 KB |
Output is correct |
22 |
Correct |
121 ms |
24148 KB |
Output is correct |
23 |
Correct |
119 ms |
24144 KB |
Output is correct |
24 |
Correct |
125 ms |
24096 KB |
Output is correct |
25 |
Correct |
51 ms |
14164 KB |
Output is correct |
26 |
Correct |
59 ms |
14160 KB |
Output is correct |
27 |
Correct |
121 ms |
24160 KB |
Output is correct |
28 |
Correct |
128 ms |
24144 KB |
Output is correct |
29 |
Correct |
53 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 |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
43 ms |
6224 KB |
Output is correct |
5 |
Correct |
125 ms |
24144 KB |
Output is correct |
6 |
Correct |
114 ms |
24148 KB |
Output is correct |
7 |
Correct |
126 ms |
24148 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
30 ms |
6372 KB |
Output is correct |
10 |
Correct |
116 ms |
24144 KB |
Output is correct |
11 |
Correct |
142 ms |
24144 KB |
Output is correct |
12 |
Correct |
131 ms |
24144 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Incorrect |
1 ms |
348 KB |
Too many ways to get from 0 to 4, should be 1 found no less than 2 |
16 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
6 ms |
1372 KB |
Output is correct |
7 |
Correct |
132 ms |
23956 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 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 |
1208 KB |
Output is correct |
13 |
Correct |
121 ms |
24148 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 |
856 KB |
Output is correct |
17 |
Correct |
73 ms |
14020 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
43 ms |
6432 KB |
Output is correct |
21 |
Correct |
123 ms |
24148 KB |
Output is correct |
22 |
Correct |
118 ms |
24144 KB |
Output is correct |
23 |
Correct |
144 ms |
24148 KB |
Output is correct |
24 |
Correct |
120 ms |
24148 KB |
Output is correct |
25 |
Correct |
52 ms |
14112 KB |
Output is correct |
26 |
Correct |
63 ms |
14164 KB |
Output is correct |
27 |
Correct |
132 ms |
24148 KB |
Output is correct |
28 |
Correct |
119 ms |
24152 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 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 |
6 ms |
1372 KB |
Output is correct |
37 |
Correct |
119 ms |
24052 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
6 ms |
1372 KB |
Output is correct |
41 |
Correct |
121 ms |
24080 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 |
928 KB |
Output is correct |
45 |
Correct |
62 ms |
14164 KB |
Output is correct |
46 |
Correct |
1 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 |
6292 KB |
Output is correct |
50 |
Correct |
121 ms |
24148 KB |
Output is correct |
51 |
Correct |
119 ms |
24144 KB |
Output is correct |
52 |
Correct |
125 ms |
24096 KB |
Output is correct |
53 |
Correct |
51 ms |
14164 KB |
Output is correct |
54 |
Correct |
59 ms |
14160 KB |
Output is correct |
55 |
Correct |
121 ms |
24160 KB |
Output is correct |
56 |
Correct |
128 ms |
24144 KB |
Output is correct |
57 |
Correct |
53 ms |
14160 KB |
Output is correct |
58 |
Correct |
0 ms |
464 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
4 ms |
860 KB |
Output is correct |
61 |
Correct |
63 ms |
14220 KB |
Output is correct |
62 |
Correct |
0 ms |
344 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Incorrect |
30 ms |
6240 KB |
Too many ways to get from 1 to 269, should be 1 found no less than 2 |
66 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
6 ms |
1372 KB |
Output is correct |
7 |
Correct |
132 ms |
23956 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 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 |
1208 KB |
Output is correct |
13 |
Correct |
121 ms |
24148 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 |
856 KB |
Output is correct |
17 |
Correct |
73 ms |
14020 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
43 ms |
6432 KB |
Output is correct |
21 |
Correct |
123 ms |
24148 KB |
Output is correct |
22 |
Correct |
118 ms |
24144 KB |
Output is correct |
23 |
Correct |
144 ms |
24148 KB |
Output is correct |
24 |
Correct |
120 ms |
24148 KB |
Output is correct |
25 |
Correct |
52 ms |
14112 KB |
Output is correct |
26 |
Correct |
63 ms |
14164 KB |
Output is correct |
27 |
Correct |
132 ms |
24148 KB |
Output is correct |
28 |
Correct |
119 ms |
24152 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 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 |
6 ms |
1372 KB |
Output is correct |
37 |
Correct |
119 ms |
24052 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
6 ms |
1372 KB |
Output is correct |
41 |
Correct |
121 ms |
24080 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 |
928 KB |
Output is correct |
45 |
Correct |
62 ms |
14164 KB |
Output is correct |
46 |
Correct |
1 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 |
6292 KB |
Output is correct |
50 |
Correct |
121 ms |
24148 KB |
Output is correct |
51 |
Correct |
119 ms |
24144 KB |
Output is correct |
52 |
Correct |
125 ms |
24096 KB |
Output is correct |
53 |
Correct |
51 ms |
14164 KB |
Output is correct |
54 |
Correct |
59 ms |
14160 KB |
Output is correct |
55 |
Correct |
121 ms |
24160 KB |
Output is correct |
56 |
Correct |
128 ms |
24144 KB |
Output is correct |
57 |
Correct |
53 ms |
14160 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
43 ms |
6224 KB |
Output is correct |
62 |
Correct |
125 ms |
24144 KB |
Output is correct |
63 |
Correct |
114 ms |
24148 KB |
Output is correct |
64 |
Correct |
126 ms |
24148 KB |
Output is correct |
65 |
Correct |
0 ms |
348 KB |
Output is correct |
66 |
Correct |
30 ms |
6372 KB |
Output is correct |
67 |
Correct |
116 ms |
24144 KB |
Output is correct |
68 |
Correct |
142 ms |
24144 KB |
Output is correct |
69 |
Correct |
131 ms |
24144 KB |
Output is correct |
70 |
Correct |
1 ms |
348 KB |
Output is correct |
71 |
Correct |
1 ms |
384 KB |
Output is correct |
72 |
Incorrect |
1 ms |
348 KB |
Too many ways to get from 0 to 4, should be 1 found no less than 2 |
73 |
Halted |
0 ms |
0 KB |
- |