# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
303501 | Jarif_Rahman | 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 "mysupertrees.h"
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
vector<vector<int>> v;
vector <vector<int>> dsu1;
vector <int> dsu2;
void unite(int a, int b){
if(dsu2[a] == dsu2[b]) return;
int x = dsu2[a], y = dsu2[b];
if(dsu1[x].size() < dsu1.size()){
while(!dsu1[x].empty()){
int ss = dsu1[x].back();
dsu1[x].pop_back();
dsu2[ss] = y;
dsu1[y].pb(ss);
}
}
else{
while(!dsu1[y].empty()){
int ss = dsu1[y].back();
dsu1[y].pop_back();
dsu2[ss] = x;
dsu1[x].pb(ss);
}
}
}
int construct(vector<vector<int>> p){
v.clear();
dsu1.clear();
dsu2.clear();
int n = p.size();
v = vector<vector<int>>(n, vector<int>(n, 0));
dsu1 = vector<vector<int>>(n);
dsu2 = vector<int>(n);
for(int i = 0; i < n ; i++) dsu1[i].pb(i);
for(int i = 0; i < n; i++) dsu2[i] = i;
for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) if(p[i][j] != 0) unite(i, j);
for(auto sth: dsu1){
if(sth.size() < 2) continue;
vector <int> two, one;
for(int x: sth){
int o = 0,tw = 0;
for(int k = 0; k < n; k++){
if(k == x) continue;
if(p[x][k] == 1) o++;
}
if(o == 0) two.pb(x);
else one.pb(x);
}
for(int i = 0; i < (int)one.size() - 1; i++){
int x = one[i], y = one[i+1];
v[x][y] = 1;
v[y][x] = 1;
}
if(two.size() == 0) continue;
for(int i = 0; i < (int)two.size() - 1; i++){
int x = two[i], y = two[i+1];
v[x][y] = 1;
v[y][x] = 1;
}
if(one.size() == 0){
int x = two[0], y = two.back();
v[x][y] = 1;
v[y][x] = 1;
}
else{
int x = two[0], y = two.back(), z = one[0];
v[x][z] = 1;
v[z][x] = 1;
v[y][z] = 1;
v[z][y] = 1;
}
}
build(v);
return 1;
}