# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
550063 | esomer | 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 "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
struct DSU{
vector<int> v;
void init(int n){
v.assign(n, -1);
}
int get(int x){return v[x] < 0 ? x : v[x] = get(v[x]);}
void unite(int x, int y){
x = get(x); y = get(y);
if(x == y) return;
if(x > y) swap(x, y);
v[x] += v[y]; v[y] = x;
return;
}
bool check(vector<vector<int>>& p, vector<vector<int>>& ans){
for(int i = 0; i < p.size(); i++){
for(int j = 0; j < p.size(); j++){
if(p[i][j] == 1){
if(get(i) != get(j)) return 0;
}else{
if(get(i) == get(j)) return 0;
}
}
}
for(int i = 0; i < v.size(); i++){
if(v[i] < 0){
int lst = i;
for(int j = 0; j < v.size(); j++){
if(j == i) continue;
if(v[j] == i){
ans[j][lst] = ans[lst][j] = 1;
lst = j;
}
}
}
}
return 1;
}
};
DSU UF;
void DFS(int x, vector<vector<int>>& adj, int r, vector<bool>& vis){
UF.unite(r, x);
if(vis[x]) return;
vis[x] = 1;
for(int i = 0; i < adj.size(); i++){
if(i == x || adj[i][x] == 0) continue;
DFS(i, adj, r, vis);
}
}
int construct(vector<vector<int>> p) {
int n = p.size();
vector<bool> vis(n, 0);
bool pos = 1;
UF.init(n);
vector<vector<int>> ans(n, vector<int>(n, 0));
for(int i = 0; i < n; i++){
if(!vis[i]){
DFS(i, p, i, vis);
}
}
if(UF.check(p, ans)){
build(ans);
return 1;
}else return 0;
}