This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#include"supertrees.h"
#pragma GCC optimize("O2")
#define S second
#define F first
#define PB push_back
#define sz(x) (int) x.size()
#define all(x) x.begin(),x.end()
#define L(x) 2*x
#define R(x) 2*x+1
#define M(x,y) (x+y)/2
using namespace std;
typedef long long ll;
const int N=1e3+1;
int p[N],lvl[N];
int getp(int x){
return (p[x]==x)? x : p[x]=getp(p[x]);
}
void unite(int i,int j){
int x=getp(i),y=getp(j);
if(x==y)
return ;
if(lvl[x]>lvl[y])
swap(x,y);
p[x]=y;
if(lvl[x]==lvl[y])
lvl[y]++;
}
void build1(vector<vector<int>> t){
for(auto v:t){
for(auto x:v)
cout<<x<<" ";
cout<<endl;
}
}
int construct(vector<vector<int>> b){
int n=sz(b);
vector<vector<int>> ans(n,vector<int>(n,0));
vector<int> co[n];
bool ex2[n]={0};
for(int i=0;i<n;i++){
lvl[i]=0;
p[i]=i;
}
for(int i=0;i<n;i++)
for(int j=0;j<n;j++){
if(b[i][j]==2)
unite(i,j),ex2[i]=1;
}
for(int i=0;i<n;i++)
co[getp(i)].PB(i);
for(int i=0;i<n;i++){
if(sz(co[i])<=1)
continue;
if(sz(co[i])==2){
return 0;
}
for(int j=0;j<sz(co[i])-1;j++)
ans[co[i][j]][co[i][j+1]]=1,ans[co[i][j+1]][co[i][j]]=1;
ans[co[i][0]][co[i].back()]=1,ans[co[i].back()][co[i][0]]=1;
for(auto x:co[i])
for(auto y:co[i]){
if(x==y)
continue;
if(b[x][y]==0)
return 0;
}
}
//b[i][j]=1
for(int i=0;i<n;i++){
lvl[i]=0;
p[i]=i;
co[i].clear();
}
for(int i=0;i<n;i++)
for(int j=0;j<n;j++){
if(b[i][j]==1)
unite(i,j);
}
for(int i=0;i<n;i++)
co[getp(i)].PB(i);
for(int i=0;i<n;i++){
if(sz(co[i])<=1)
continue;
for(int j=0;j<sz(co[i]);j++){
if(ex2[co[i][j]]){
swap(co[i][j],co[i][0]);
break;
}
}
for(int j=0;j<sz(co[i])-1;j++)
ans[co[i][j]][co[i][j+1]]=1,ans[co[i][j+1]][co[i][j]]=1;
for(auto x:co[i])
for(auto y:co[i]){
if(x==y)
continue;
if(b[x][y]==0)
return 0;
}
}
build(ans);
return 1;
}
/*
int main(){
int n;
cin>>n;
vector<vector<int>> t(n,vector<int>(n,0));
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cin>>t[i][j];
construct(t);
}
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |