# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
309539 | algorithm16 | 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<iostream>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;
int parent[1005],d[1005];
vector <vector<int> > sol;
set <int> s1;
int find_par(int x) {
if(parent[x]==x) return x;
int par=find_par(parent[x]);
parent[x]=par;
return par;
}
void s(int x,int y) {
x=find_par(x);
y=find_par(y);
if(x==y) return;
if(d[x]==d[y]) d[x]+=1;
if(d[x]>d[y]) parent[y]=x;
else parent[x]=y;
sol[x][y]=1;
sol[y][x]=1;
s1.insert(x);
s1.insert(y);
}
int construct(vector <vector<int> > p) {
int n=p.size();
for(int i=0;i<n;i++) {
parent[i]=i;
d[i]=1;
sol.push_back({});
for(int j=0;j<n;j++) {
sol[i].push_back(0);
}
}
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
if(i==j) continue;
if(p[i][j]==1) s(i,j);
}
}
vector <int> v;
for(int i=0;i<n;i++) {
v.clear();
for(int j=0;j<n;j++) {
if(p[i][j]==2) v.push_back(j);
}
if(!v.empty()) s(i,v[0]);
for(int j=0;j<v.size()-1;j++) {
s(v[j],v[j+1]);
}
if(!v.empty()) s(v.back(),i);
}
build(sol);
return 1;
}