#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
#define INF 1e18
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define OK cout<<"Ok"<<endl;
#define MOD (ll)(1e9+7)
const int mxn=1e3+5;
int n,color[mxn],k,color2[mxn],sz,lst,arr[mxn][mxn];
vector<int>vt[mxn];
vector<int>vt2[mxn];
vector<vector<int>>p;
void dfs(int u){
color[u]=k;
sz++;
for(int v:vt[u]){
if(!color[v]){
arr[u][v]=1;
arr[v][u]=1;
dfs(v);
}
}
}
void dfs2(int u){
color2[u]=k;
sz++;
lst=u;
for(int vv:vt2[u]){
int v=color[vv];
if(!color2[v]){
arr[u][v]=1;
arr[v][u]=1;
dfs2(v);
}
}
}
int construct(vector<vector<int>> pp) {
p=pp;
n=p.size();
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(p[i][j]==1){
vt[i+1].pb(j+1);
vt[j+1].pb(i+1);
}
else if(p[i][j]==2){
vt2[i+1].pb(j+1);
vt2[j+1].pb(i+1);
}
else if(p[i][j]==3){
//cout<<"X1"<<endl;
return 0;
}
}
}
for(int i=1;i<=n;i++){
if(!color[i]){
k=i;
sz=0;
dfs(i);
}
}
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
if(color[i]==color[j]&&p[i-1][j-1]!=1){
//cout<<i<<' '<<j<<endl;
//cout<<"X2"<<endl;
return 0;
}
}
}
vector<pii>v;
for(int id=1;id<=n;id++){
int i=color[id];
if(color2[i]) continue;
k=i;
sz=0;
dfs2(i);
if(lst==i){
color2[i]=0;
}
else{
if(sz<=2) return 0;
arr[i][lst]=1;
arr[lst][i]=1;
v.pb({i,lst});
}
}
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
if(color2[i]>0&&color2[i]==color2[j]&&p[i-1][j-1]!=2){
//cout<<"X3"<<endl;
return 0;
}
}
}
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
if(p[i-1][j-1]==2&&color2[color[i]]!=color2[color[j]]){
int a=color[i];
int b=color2[j];
int c=color2[i];
int d=color[j];
if(a&&b){
arr[a][b]=1;
arr[b][a]=1;
}
else if(c&&d){
arr[c][d]=1;
arr[d][c]=1;
}
}
}
}
vector<vector<int>>ans;
for(int i=1;i<=n;i++){
vector<int>vt;
for(int j=1;j<=n;j++){
vt.pb(arr[i][j]);
}
ans.pb(vt);
}
for(int i=0;i<n;i++){
ans[i][i]=0;
}
build(ans);
return 1;
}
/*
4
1 1 2 2
1 1 2 2
2 2 1 2
2 2 2 1
7
1 2 2 2 0 0 0
2 1 2 2 0 0 0
2 2 1 2 0 0 0
2 2 2 1 0 0 0
0 0 0 0 1 2 2
0 0 0 0 2 1 2
0 0 0 0 2 2 1
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
7 ms |
2436 KB |
Output is correct |
7 |
Correct |
173 ms |
34080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
7 ms |
2436 KB |
Output is correct |
7 |
Correct |
173 ms |
34080 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
7 ms |
1364 KB |
Output is correct |
13 |
Correct |
156 ms |
26172 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
1748 KB |
Output is correct |
17 |
Correct |
69 ms |
18872 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
42 ms |
8628 KB |
Output is correct |
21 |
Correct |
174 ms |
30536 KB |
Output is correct |
22 |
Correct |
152 ms |
29652 KB |
Output is correct |
23 |
Correct |
164 ms |
32072 KB |
Output is correct |
24 |
Correct |
167 ms |
26308 KB |
Output is correct |
25 |
Correct |
61 ms |
16588 KB |
Output is correct |
26 |
Correct |
59 ms |
15676 KB |
Output is correct |
27 |
Correct |
165 ms |
33052 KB |
Output is correct |
28 |
Correct |
169 ms |
26208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 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 |
7 ms |
1364 KB |
Output is correct |
9 |
Correct |
153 ms |
26052 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
9 ms |
2388 KB |
Output is correct |
13 |
Correct |
160 ms |
34080 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
1748 KB |
Output is correct |
17 |
Correct |
71 ms |
19004 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
40 ms |
8636 KB |
Output is correct |
22 |
Correct |
156 ms |
30540 KB |
Output is correct |
23 |
Correct |
156 ms |
29980 KB |
Output is correct |
24 |
Correct |
161 ms |
33100 KB |
Output is correct |
25 |
Correct |
58 ms |
12348 KB |
Output is correct |
26 |
Correct |
68 ms |
16700 KB |
Output is correct |
27 |
Correct |
158 ms |
29896 KB |
Output is correct |
28 |
Correct |
162 ms |
32072 KB |
Output is correct |
29 |
Correct |
63 ms |
12344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
40 ms |
8656 KB |
Output is correct |
5 |
Correct |
156 ms |
30532 KB |
Output is correct |
6 |
Correct |
158 ms |
29644 KB |
Output is correct |
7 |
Correct |
160 ms |
32088 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
42 ms |
8652 KB |
Output is correct |
10 |
Correct |
160 ms |
30520 KB |
Output is correct |
11 |
Correct |
177 ms |
29988 KB |
Output is correct |
12 |
Correct |
164 ms |
33048 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
40 ms |
8628 KB |
Output is correct |
17 |
Correct |
166 ms |
32428 KB |
Output is correct |
18 |
Correct |
159 ms |
32428 KB |
Output is correct |
19 |
Correct |
155 ms |
32088 KB |
Output is correct |
20 |
Correct |
154 ms |
28228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
7 ms |
2436 KB |
Output is correct |
7 |
Correct |
173 ms |
34080 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
7 ms |
1364 KB |
Output is correct |
13 |
Correct |
156 ms |
26172 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
1748 KB |
Output is correct |
17 |
Correct |
69 ms |
18872 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
42 ms |
8628 KB |
Output is correct |
21 |
Correct |
174 ms |
30536 KB |
Output is correct |
22 |
Correct |
152 ms |
29652 KB |
Output is correct |
23 |
Correct |
164 ms |
32072 KB |
Output is correct |
24 |
Correct |
167 ms |
26308 KB |
Output is correct |
25 |
Correct |
61 ms |
16588 KB |
Output is correct |
26 |
Correct |
59 ms |
15676 KB |
Output is correct |
27 |
Correct |
165 ms |
33052 KB |
Output is correct |
28 |
Correct |
169 ms |
26208 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
0 ms |
340 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 |
7 ms |
1364 KB |
Output is correct |
37 |
Correct |
153 ms |
26052 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
0 ms |
340 KB |
Output is correct |
40 |
Correct |
9 ms |
2388 KB |
Output is correct |
41 |
Correct |
160 ms |
34080 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
4 ms |
1748 KB |
Output is correct |
45 |
Correct |
71 ms |
19004 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
0 ms |
340 KB |
Output is correct |
48 |
Correct |
0 ms |
340 KB |
Output is correct |
49 |
Correct |
40 ms |
8636 KB |
Output is correct |
50 |
Correct |
156 ms |
30540 KB |
Output is correct |
51 |
Correct |
156 ms |
29980 KB |
Output is correct |
52 |
Correct |
161 ms |
33100 KB |
Output is correct |
53 |
Correct |
58 ms |
12348 KB |
Output is correct |
54 |
Correct |
68 ms |
16700 KB |
Output is correct |
55 |
Correct |
158 ms |
29896 KB |
Output is correct |
56 |
Correct |
162 ms |
32072 KB |
Output is correct |
57 |
Correct |
63 ms |
12344 KB |
Output is correct |
58 |
Correct |
0 ms |
340 KB |
Output is correct |
59 |
Correct |
0 ms |
340 KB |
Output is correct |
60 |
Correct |
4 ms |
1748 KB |
Output is correct |
61 |
Correct |
73 ms |
20616 KB |
Output is correct |
62 |
Correct |
0 ms |
340 KB |
Output is correct |
63 |
Correct |
1 ms |
340 KB |
Output is correct |
64 |
Correct |
0 ms |
340 KB |
Output is correct |
65 |
Correct |
39 ms |
8664 KB |
Output is correct |
66 |
Correct |
63 ms |
18616 KB |
Output is correct |
67 |
Correct |
63 ms |
16924 KB |
Output is correct |
68 |
Correct |
62 ms |
18220 KB |
Output is correct |
69 |
Correct |
68 ms |
14308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
7 ms |
2436 KB |
Output is correct |
7 |
Correct |
173 ms |
34080 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
7 ms |
1364 KB |
Output is correct |
13 |
Correct |
156 ms |
26172 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
1748 KB |
Output is correct |
17 |
Correct |
69 ms |
18872 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
42 ms |
8628 KB |
Output is correct |
21 |
Correct |
174 ms |
30536 KB |
Output is correct |
22 |
Correct |
152 ms |
29652 KB |
Output is correct |
23 |
Correct |
164 ms |
32072 KB |
Output is correct |
24 |
Correct |
167 ms |
26308 KB |
Output is correct |
25 |
Correct |
61 ms |
16588 KB |
Output is correct |
26 |
Correct |
59 ms |
15676 KB |
Output is correct |
27 |
Correct |
165 ms |
33052 KB |
Output is correct |
28 |
Correct |
169 ms |
26208 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
0 ms |
340 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 |
7 ms |
1364 KB |
Output is correct |
37 |
Correct |
153 ms |
26052 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
0 ms |
340 KB |
Output is correct |
40 |
Correct |
9 ms |
2388 KB |
Output is correct |
41 |
Correct |
160 ms |
34080 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
4 ms |
1748 KB |
Output is correct |
45 |
Correct |
71 ms |
19004 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
0 ms |
340 KB |
Output is correct |
48 |
Correct |
0 ms |
340 KB |
Output is correct |
49 |
Correct |
40 ms |
8636 KB |
Output is correct |
50 |
Correct |
156 ms |
30540 KB |
Output is correct |
51 |
Correct |
156 ms |
29980 KB |
Output is correct |
52 |
Correct |
161 ms |
33100 KB |
Output is correct |
53 |
Correct |
58 ms |
12348 KB |
Output is correct |
54 |
Correct |
68 ms |
16700 KB |
Output is correct |
55 |
Correct |
158 ms |
29896 KB |
Output is correct |
56 |
Correct |
162 ms |
32072 KB |
Output is correct |
57 |
Correct |
63 ms |
12344 KB |
Output is correct |
58 |
Correct |
0 ms |
340 KB |
Output is correct |
59 |
Correct |
0 ms |
340 KB |
Output is correct |
60 |
Correct |
0 ms |
340 KB |
Output is correct |
61 |
Correct |
40 ms |
8656 KB |
Output is correct |
62 |
Correct |
156 ms |
30532 KB |
Output is correct |
63 |
Correct |
158 ms |
29644 KB |
Output is correct |
64 |
Correct |
160 ms |
32088 KB |
Output is correct |
65 |
Correct |
0 ms |
340 KB |
Output is correct |
66 |
Correct |
42 ms |
8652 KB |
Output is correct |
67 |
Correct |
160 ms |
30520 KB |
Output is correct |
68 |
Correct |
177 ms |
29988 KB |
Output is correct |
69 |
Correct |
164 ms |
33048 KB |
Output is correct |
70 |
Correct |
0 ms |
340 KB |
Output is correct |
71 |
Correct |
0 ms |
340 KB |
Output is correct |
72 |
Correct |
0 ms |
340 KB |
Output is correct |
73 |
Correct |
40 ms |
8628 KB |
Output is correct |
74 |
Correct |
166 ms |
32428 KB |
Output is correct |
75 |
Correct |
159 ms |
32428 KB |
Output is correct |
76 |
Correct |
155 ms |
32088 KB |
Output is correct |
77 |
Correct |
154 ms |
28228 KB |
Output is correct |
78 |
Correct |
0 ms |
340 KB |
Output is correct |
79 |
Correct |
0 ms |
340 KB |
Output is correct |
80 |
Correct |
4 ms |
1748 KB |
Output is correct |
81 |
Correct |
73 ms |
20616 KB |
Output is correct |
82 |
Correct |
0 ms |
340 KB |
Output is correct |
83 |
Correct |
1 ms |
340 KB |
Output is correct |
84 |
Correct |
0 ms |
340 KB |
Output is correct |
85 |
Correct |
39 ms |
8664 KB |
Output is correct |
86 |
Correct |
63 ms |
18616 KB |
Output is correct |
87 |
Correct |
63 ms |
16924 KB |
Output is correct |
88 |
Correct |
62 ms |
18220 KB |
Output is correct |
89 |
Correct |
68 ms |
14308 KB |
Output is correct |
90 |
Correct |
1 ms |
340 KB |
Output is correct |
91 |
Correct |
1 ms |
356 KB |
Output is correct |
92 |
Correct |
3 ms |
852 KB |
Output is correct |
93 |
Correct |
62 ms |
14052 KB |
Output is correct |
94 |
Correct |
1 ms |
340 KB |
Output is correct |
95 |
Correct |
0 ms |
360 KB |
Output is correct |
96 |
Correct |
1 ms |
340 KB |
Output is correct |
97 |
Correct |
21 ms |
3796 KB |
Output is correct |
98 |
Correct |
59 ms |
14120 KB |
Output is correct |
99 |
Correct |
60 ms |
14256 KB |
Output is correct |
100 |
Correct |
60 ms |
14124 KB |
Output is correct |
101 |
Correct |
58 ms |
14144 KB |
Output is correct |
102 |
Correct |
59 ms |
14124 KB |
Output is correct |
103 |
Correct |
58 ms |
14128 KB |
Output is correct |
104 |
Correct |
60 ms |
14120 KB |
Output is correct |
105 |
Correct |
62 ms |
14136 KB |
Output is correct |