# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
315573 | tigicha | Connecting Supertrees (IOI20_supertrees) | C++14 | 0 ms | 0 KiB |
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 "supertrees.h"
#include<bits/stdc++.h>
using namespace std;
const int N=1e3+5;
int par[1005], ar[1005], a[1005][1005], ans[1005][1005], fix1[1005], n, fix[1005];
vector<long long>vec1[1005], vec[1005], v1;
vector<vector<long long> >answer;
long long parent(long long x){
if(par[x]==x) return x;
return par[x]=parent(par[x]);
}
void dsu(long long x, long long y){
x=parent(x);
y=parent(y);
if(x==y) return;
if(ar[x]<ar[y]) swap(x, y);
par[y]=x;
ar[x]+=ar[y];
}
void rec(long long z){
for(long long i=0; i<vec1[z].size(); i++)
for(long long j=i+1; j<vec1[z].size(); j++){
long long x=vec1[z][i];
long long y=vec1[z][j];
if(a[x][y]==1){
vec[x].push_back(y);
vec[y].push_back(x);
}
}
v1.clear();
for(long long i=0; i<vec1[z].size(); i++){
long long x=vec1[z][i];
if(fix[x]) continue;
v1.push_back(x);
fix1[x]=1;
if(!fix[x])
for(long long j=0; j<vec[x].size(); i++){
fix[vec[x][j]]=x;
ans[x][vec[x][j]]=1;
ans[vec[x][j]][x]=1;
}
}
if(v1.size()>1)
for(long long i=0; i<v1.size(); i++){
ans[v1[i]][v1[(i+1)%v1.size()]]=1;
ans[v1[(i+1)%v1.size()]][v1[i]]=1;
}
}
bool check(){
for(long long i=1; i<=n; i++){
std::vector<long long> v2;
for(long long j=1; j<=n; j++){
if(i==j) ans[i][i]=0;
else{
if(a[i][j]==0 && parent(i)==parent(j)) return 0;
if(!fix1[i] && !fix1[j] && a[i][j]==2 && fix[i]==fix[j]) return 0;
}
v2.push_back(ans[i][j]);
}
answer.push_back(v2);
}
return 1;
}
int construct(std::vector<std::vector<long long> > v){
n=v.size();
for(long long i=1; i<=n; i++){
par[i]=i;
ar[i]=1;
}
for(long long i=1; i<=n; i++)
for(long long j=1; j<=n; j++){
a[i][j]=v[i-1][j-1];
if(a[i][j]==3) return 0;
if(a[i][j]!=0) dsu(i, j);
}
for(long long i=1; i<=n; i++)
vec1[parent(i)].push_back(i);
for(long long i=1; i<=n; i++){
rec(i);
if(v1.size()==2 && a[v1[0]][v1[1]]==2) return 0;
}
if(!check()) return 0;
build(answer);
return 1;
}