#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>
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=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]==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);
set<int>s;
//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);
memset(last,-1,sizeof last);
memset(first,-1,sizeof first);
//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()||v[last[i]].size()){
//connect to second node
if(v[last[i]].size())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);
}
/*
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:91:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | for(int j=1;j<v[i].size();j++){
| ~^~~~~~~~~~~~
supertrees.cpp:127:5: error: 'build' was not declared in this scope
127 | build(ans);
| ^~~~~
supertrees.cpp: In function 'void setIO(std::string)':
supertrees.cpp:30:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | freopen((name+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:65:44: warning: control reaches end of non-void function [-Wreturn-type]
65 | vector<vector<int>>ans(n,vector<int>(n));
| ^