# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
949963 | irmuun | Connecting Supertrees (IOI20_supertrees) | C++17 | 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<bits/stdc++.h>
//#include "supertrees.h"
using namespace std;
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define all(s) s.begin(),s.end()
#define rall(s) s.rbegin(),s.rend()
struct dsu{
int n;
vector<int>sz,par;
dsu(int n):n(n){
sz.resize(n);
par.resize(n);
fill(all(sz),1);
iota(all(par),0);
}
int find(int x){
if(par[x]==x) return x;
return par[x]=find(par[x]);
}
void merge(int x,int y){
x=find(x);
y=find(y);
if(x!=y){
if(sz[x]<sz[y]){
swap(x,y);
}
sz[x]+=sz[y];
par[y]=x;
}
}
};
int construct(vector<vector<int>>p){
int n=p.size();
bool a[4];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
a[p[i][j]]=true;
}
}
vector<vector<int>>b(n,vector<int>(n,0));
if(!a[2]&&!a[3]){
vector<bool>used(n,0);
vector<int>ver;
for(int i=0;i<n;i++){
if(!used[i]){
queue<int>q;
q.push(i);
used[i]=true;
while(!q.empty()){
int x=q.front();
ver.pb(x);
q.pop();
for(int j=0;j<n;j++){
if(p[x][j]==1&&!used[j]){
used[j]=true;
q.push(j);
}
}
}
for(auto x:ver){
for(auto y:ver){
if(x!=y&&p[x][y]!=1){
return 0;
}
}
}
for(int j=1;j<ver.size();j++){
b[ver[j]][ver[j-1]]=1;
b[ver[j-1]][ver[j]]=1;
}
ver.clear();
}
}
build(b);
return 1;
}
if(a[0]&&!a[1]&&a[2]&&!a[3]){
vector<bool>used(n,0);
vector<int>ver;
for(int i=0;i<n;i++){
if(!used[i]){
queue<int>q;
q.push(i);
used[i]=true;
while(!q.empty()){
int x=q.front();
ver.pb(x);
q.pop();
for(int j=0;j<n;j++){
if(p[x][j]==2&&!used[j]){
used[j]=true;
q.push(j);
}
}
}
for(auto x:ver){
for(auto y:ver){
if(x!=y&&p[x][y]!=2){
return 0;
}
}
}
for(int j=1;j<ver.size();j++){
b[ver[j]][ver[j-1]]=1;
b[ver[j-1]][ver[j]]=1;
}
if(ver.size()>1){
b[ver[0]][ver.back()]=1;
b[ver.back()][ver[0]]=1;
}
ver.clear();
}
}
build(b);
return 1;
}
return 0;
}