#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include<cmath>
#include<set>
#include<algorithm>
#include<bitset>
#include "supertrees.h"
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define pb push_back
#define all(x) x.begin(),x.end()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
using namespace std;
const int mxn=2*1e3,mod=998244353,lg=42,root=80,inf=1e9;
void setIO(string name) {
ios_base::sync_with_stdio(0); cin.tie(0);
freopen((name+".in").c_str(),"r",stdin);
freopen((name+".out").c_str(),"w",stdout);
}
int last[mxn+10],first[mxn+10];
struct dsu{
int pa[mxn+10],sz[mxn+10];
void init(){for(int i=0;i<=mxn;i++)pa[i]=i,sz[i]=1;}
int find(int u){
if(pa[u]==u)return u;
return pa[u]=find(pa[u]);
}
void merg(int u,int v){
int a=find(u),b=find(v);
if(a==b)return;
if(sz[a]>=sz[b]){
pa[b]=a;
sz[a]+=sz[b];
return;
}
pa[a]=b;
sz[b]+=sz[a];
}
};
/*
void build(vector<vector<int>>v){
for(auto i:v){
for(auto j:i)cout<<j<<" ";
cout<<'\n';
}
}
*/
vector<int>v[mxn+10];
int construct(vector<vector<int>>p){
int n=p.size();
dsu one,two;
vector<vector<int>>ans(n,vector<int>(n));
one.init();
two.init();
for(int i=0;i<n;i++){
for(int j=0;j<i;j++){
if(p[i][j]==3)return 0;//not possible
if(p[i][j]==1)one.merg(i,j);
if(p[i][j]!=p[j][i])return 0;
}
}
for(int i=0;i<n;i++)v[one.pa[i]].pb(i),one.pa[i]=one.find(i);
//case one
for(int i=0;i<n;i++)if(one.pa[i]!=i)ans[i][one.pa[i]]=1,ans[one.pa[i]][i]=1;
for(int i=0;i<n;i++){
for(int j=0;j<i;j++){
if(p[i][j]==2)two.merg(one.pa[i],one.pa[j]);//grouping
}
}
for(int i=0;i<n;i++)two.pa[i]=two.find(i);
fill(last,last+mxn,-1);
fill(first,first+mxn,-1);
//cyclic connection in group
for(int i=0;i<n;i++){
if(v[i].size())continue;
vector<int>check(n,-1);
for(int j=0;j<n;j++)check[j]=p[i][j];
for(int j=1;j<v[i].size();j++){
for(int k=0;k<n;k++)if(check[j]!=p[v[i][j]][k])return 0;
}
}
for(int i=0;i<n;i++){
if(one.pa[i]!=i)continue;
if(last[two.pa[i]]==-1)last[two.pa[i]]=i,first[two.pa[i]]=i;
else{
ans[last[two.pa[i]]][i]=1;
ans[i][last[two.pa[i]]]=1;
last[two.pa[i]]=i;
}
}
//finish the last connect
for(int i=0;i<n;i++){
if(last[i]==-1)continue;
if(first[i]==last[i])continue;
if(ans[first[i]][last[i]]){//already connected
if(v[first[i]].size()>1||v[last[i]].size()>1){
//connect to second node
if(v[last[i]].size()>1)swap(first[i],last[i]);
ans[first[i]][v[last[i]][1]]=1;
ans[v[last[i]][1]][first[i]]=1;
}
else return 0;
continue;
}
ans[first[i]][last[i]]=1;
ans[last[i]][first[i]]=1;
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(p[i][j]!=0)continue;
if((two.pa[i]==two.pa[j])||(one.pa[i]==one.pa[j]))return 0;
}
}
build(ans);
return 1;
}
/*
int32_t main(){
fastio
int n;cin>>n;
vector<vector<int>>in(n,vector<int>(n));
for(int i=0;i<n;i++)for(int j=0;j<n;j++)cin>>in[i][j];
construct(in);
return 0;
}*/
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:92:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for(int j=1;j<v[i].size();j++){
| ~^~~~~~~~~~~~
supertrees.cpp: In function 'void setIO(std::string)':
supertrees.cpp:31:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | freopen((name+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
supertrees.cpp:32:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | freopen((name+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
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 |
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 |
8 ms |
1368 KB |
Output is correct |
7 |
Correct |
155 ms |
22280 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 |
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 |
8 ms |
1368 KB |
Output is correct |
7 |
Correct |
155 ms |
22280 KB |
Output is correct |
8 |
Correct |
0 ms |
348 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 |
7 ms |
1416 KB |
Output is correct |
13 |
Correct |
170 ms |
22088 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
5 ms |
860 KB |
Output is correct |
17 |
Correct |
98 ms |
12368 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
600 KB |
Output is correct |
20 |
Correct |
48 ms |
5944 KB |
Output is correct |
21 |
Correct |
155 ms |
22356 KB |
Output is correct |
22 |
Correct |
180 ms |
22096 KB |
Output is correct |
23 |
Correct |
166 ms |
22236 KB |
Output is correct |
24 |
Correct |
166 ms |
22092 KB |
Output is correct |
25 |
Correct |
61 ms |
12372 KB |
Output is correct |
26 |
Correct |
61 ms |
12372 KB |
Output is correct |
27 |
Correct |
161 ms |
22096 KB |
Output is correct |
28 |
Correct |
152 ms |
22328 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 |
1 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 |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
7 ms |
1372 KB |
Output is correct |
9 |
Correct |
167 ms |
22068 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
8 ms |
1416 KB |
Output is correct |
13 |
Correct |
150 ms |
22176 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
856 KB |
Output is correct |
17 |
Correct |
68 ms |
12364 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 |
38 ms |
5976 KB |
Output is correct |
22 |
Correct |
151 ms |
22096 KB |
Output is correct |
23 |
Correct |
150 ms |
22100 KB |
Output is correct |
24 |
Correct |
163 ms |
22108 KB |
Output is correct |
25 |
Correct |
63 ms |
12372 KB |
Output is correct |
26 |
Correct |
63 ms |
12364 KB |
Output is correct |
27 |
Correct |
156 ms |
22200 KB |
Output is correct |
28 |
Correct |
163 ms |
22092 KB |
Output is correct |
29 |
Correct |
75 ms |
12372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
41 ms |
5776 KB |
Output is correct |
5 |
Correct |
155 ms |
22124 KB |
Output is correct |
6 |
Correct |
152 ms |
22256 KB |
Output is correct |
7 |
Correct |
159 ms |
22096 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
38 ms |
5916 KB |
Output is correct |
10 |
Correct |
154 ms |
22272 KB |
Output is correct |
11 |
Correct |
146 ms |
22100 KB |
Output is correct |
12 |
Correct |
162 ms |
22100 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
37 ms |
5832 KB |
Output is correct |
17 |
Correct |
159 ms |
22284 KB |
Output is correct |
18 |
Correct |
151 ms |
22096 KB |
Output is correct |
19 |
Correct |
152 ms |
22352 KB |
Output is correct |
20 |
Correct |
157 ms |
22292 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 |
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 |
8 ms |
1368 KB |
Output is correct |
7 |
Correct |
155 ms |
22280 KB |
Output is correct |
8 |
Correct |
0 ms |
348 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 |
7 ms |
1416 KB |
Output is correct |
13 |
Correct |
170 ms |
22088 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
5 ms |
860 KB |
Output is correct |
17 |
Correct |
98 ms |
12368 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
600 KB |
Output is correct |
20 |
Correct |
48 ms |
5944 KB |
Output is correct |
21 |
Correct |
155 ms |
22356 KB |
Output is correct |
22 |
Correct |
180 ms |
22096 KB |
Output is correct |
23 |
Correct |
166 ms |
22236 KB |
Output is correct |
24 |
Correct |
166 ms |
22092 KB |
Output is correct |
25 |
Correct |
61 ms |
12372 KB |
Output is correct |
26 |
Correct |
61 ms |
12372 KB |
Output is correct |
27 |
Correct |
161 ms |
22096 KB |
Output is correct |
28 |
Correct |
152 ms |
22328 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 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 |
344 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
7 ms |
1372 KB |
Output is correct |
37 |
Correct |
167 ms |
22068 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
8 ms |
1416 KB |
Output is correct |
41 |
Correct |
150 ms |
22176 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
3 ms |
856 KB |
Output is correct |
45 |
Correct |
68 ms |
12364 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 |
38 ms |
5976 KB |
Output is correct |
50 |
Correct |
151 ms |
22096 KB |
Output is correct |
51 |
Correct |
150 ms |
22100 KB |
Output is correct |
52 |
Correct |
163 ms |
22108 KB |
Output is correct |
53 |
Correct |
63 ms |
12372 KB |
Output is correct |
54 |
Correct |
63 ms |
12364 KB |
Output is correct |
55 |
Correct |
156 ms |
22200 KB |
Output is correct |
56 |
Correct |
163 ms |
22092 KB |
Output is correct |
57 |
Correct |
75 ms |
12372 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 |
856 KB |
Output is correct |
61 |
Correct |
73 ms |
12344 KB |
Output is correct |
62 |
Correct |
0 ms |
344 KB |
Output is correct |
63 |
Correct |
1 ms |
348 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
39 ms |
5856 KB |
Output is correct |
66 |
Correct |
74 ms |
12348 KB |
Output is correct |
67 |
Correct |
62 ms |
12368 KB |
Output is correct |
68 |
Correct |
61 ms |
12360 KB |
Output is correct |
69 |
Correct |
59 ms |
12360 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 |
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 |
8 ms |
1368 KB |
Output is correct |
7 |
Correct |
155 ms |
22280 KB |
Output is correct |
8 |
Correct |
0 ms |
348 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 |
7 ms |
1416 KB |
Output is correct |
13 |
Correct |
170 ms |
22088 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
5 ms |
860 KB |
Output is correct |
17 |
Correct |
98 ms |
12368 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
600 KB |
Output is correct |
20 |
Correct |
48 ms |
5944 KB |
Output is correct |
21 |
Correct |
155 ms |
22356 KB |
Output is correct |
22 |
Correct |
180 ms |
22096 KB |
Output is correct |
23 |
Correct |
166 ms |
22236 KB |
Output is correct |
24 |
Correct |
166 ms |
22092 KB |
Output is correct |
25 |
Correct |
61 ms |
12372 KB |
Output is correct |
26 |
Correct |
61 ms |
12372 KB |
Output is correct |
27 |
Correct |
161 ms |
22096 KB |
Output is correct |
28 |
Correct |
152 ms |
22328 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 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 |
344 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
7 ms |
1372 KB |
Output is correct |
37 |
Correct |
167 ms |
22068 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
8 ms |
1416 KB |
Output is correct |
41 |
Correct |
150 ms |
22176 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
3 ms |
856 KB |
Output is correct |
45 |
Correct |
68 ms |
12364 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 |
38 ms |
5976 KB |
Output is correct |
50 |
Correct |
151 ms |
22096 KB |
Output is correct |
51 |
Correct |
150 ms |
22100 KB |
Output is correct |
52 |
Correct |
163 ms |
22108 KB |
Output is correct |
53 |
Correct |
63 ms |
12372 KB |
Output is correct |
54 |
Correct |
63 ms |
12364 KB |
Output is correct |
55 |
Correct |
156 ms |
22200 KB |
Output is correct |
56 |
Correct |
163 ms |
22092 KB |
Output is correct |
57 |
Correct |
75 ms |
12372 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
0 ms |
344 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
41 ms |
5776 KB |
Output is correct |
62 |
Correct |
155 ms |
22124 KB |
Output is correct |
63 |
Correct |
152 ms |
22256 KB |
Output is correct |
64 |
Correct |
159 ms |
22096 KB |
Output is correct |
65 |
Correct |
0 ms |
348 KB |
Output is correct |
66 |
Correct |
38 ms |
5916 KB |
Output is correct |
67 |
Correct |
154 ms |
22272 KB |
Output is correct |
68 |
Correct |
146 ms |
22100 KB |
Output is correct |
69 |
Correct |
162 ms |
22100 KB |
Output is correct |
70 |
Correct |
0 ms |
344 KB |
Output is correct |
71 |
Correct |
0 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
37 ms |
5832 KB |
Output is correct |
74 |
Correct |
159 ms |
22284 KB |
Output is correct |
75 |
Correct |
151 ms |
22096 KB |
Output is correct |
76 |
Correct |
152 ms |
22352 KB |
Output is correct |
77 |
Correct |
157 ms |
22292 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 |
856 KB |
Output is correct |
81 |
Correct |
73 ms |
12344 KB |
Output is correct |
82 |
Correct |
0 ms |
344 KB |
Output is correct |
83 |
Correct |
1 ms |
348 KB |
Output is correct |
84 |
Correct |
0 ms |
348 KB |
Output is correct |
85 |
Correct |
39 ms |
5856 KB |
Output is correct |
86 |
Correct |
74 ms |
12348 KB |
Output is correct |
87 |
Correct |
62 ms |
12368 KB |
Output is correct |
88 |
Correct |
61 ms |
12360 KB |
Output is correct |
89 |
Correct |
59 ms |
12360 KB |
Output is correct |
90 |
Correct |
0 ms |
344 KB |
Output is correct |
91 |
Correct |
0 ms |
348 KB |
Output is correct |
92 |
Correct |
3 ms |
860 KB |
Output is correct |
93 |
Correct |
64 ms |
12116 KB |
Output is correct |
94 |
Correct |
0 ms |
348 KB |
Output is correct |
95 |
Correct |
0 ms |
348 KB |
Output is correct |
96 |
Correct |
1 ms |
488 KB |
Output is correct |
97 |
Correct |
15 ms |
3824 KB |
Output is correct |
98 |
Correct |
61 ms |
13520 KB |
Output is correct |
99 |
Correct |
61 ms |
13432 KB |
Output is correct |
100 |
Correct |
60 ms |
13396 KB |
Output is correct |
101 |
Correct |
59 ms |
13396 KB |
Output is correct |
102 |
Correct |
60 ms |
13552 KB |
Output is correct |
103 |
Correct |
78 ms |
13372 KB |
Output is correct |
104 |
Correct |
61 ms |
13876 KB |
Output is correct |
105 |
Correct |
64 ms |
13612 KB |
Output is correct |