# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
550138 | esomer | Connecting Supertrees (IOI20_supertrees) | C++17 | 253 ms | 23500 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 k = 0; k < v.size(); k++){
if(v[k] < 0){
vector<int> c;
c.push_back(k);
for(int j = 0; j < v.size(); j++){
if(v[j] == k) c.push_back(j);
}
bool global_pos = 0;
for(int i : c){
if(global_pos) break;
bool pos = 1;
vector<int> three;
vector<int> two;
vector<int> one;
for(int j : c){
if(j == i) continue;
if(p[i][j] == 3) three.push_back(j);
else if(p[i][j] == 2) two.push_back(j);
else if(p[i][j] == 1) one.push_back(j);
else pos = 0;
}
if((three.size() > 0 && three.size() < 3) || (two.size() > 0 && two.size() < 2) || (two.size() > 0 && three.size() > 0)){
pos = 0;
}
for(int q : three){
for(int j : three){
if(q == j) continue;
if(p[q][j] != 3){
pos = 0;
}
}
for(int j : one){
if(p[q][j] != 3){
pos = 0;
}
}
}
for(int q : two){
for(int j : two){
if(q == j) continue;
if(p[q][j] != 2){
pos = 0;
}
}
for(int j : one){
if(p[q][j] != 2){
pos = 0;
}
}
}
for(int q : one){
for(int j : one){
if(q == j) continue;
if(p[q][j] != 1){
pos = 0;
}
}
}
if(pos){
global_pos = 1;
if(three.size() > 0){
ans[i][three[three.size() - 1]] = ans[i][three[three.size() - 2]] = 1;
ans[three[three.size() - 1]][i] = ans[three[three.size() - 2]][i] = 1;
ans[three[0]][i] = ans[i][three[0]] = 1;
for(int j = 0; j < three.size() - 1; j++){
ans[three[j]][three[j+1]] = ans[three[j+1]][three[j]] = 1;
}
}
if(two.size() > 0){
ans[i][two[two.size() - 1]] = ans[two[two.size() - 1]][i] = 1;
ans[i][two[0]] = ans[two[0]][i] = 1;
for(int j = 0; j < two.size() - 1; j++){
ans[two[j]][two[j+1]] = ans[two[j+1]][two[j]] = 1;
}
}
if(one.size() > 0){
ans[one[0]][i] = ans[i][one[0]] = 1;
for(int j = 0; j < one.size() - 1; j++){
ans[one[j]][one[j+1]] = ans[one[j+1]][one[j]] = 1;
}
}
}
}
if(!global_pos) return 0;
}
}
return 1;
}
};
DSU UF2;
int construct(vector<vector<int>> p) { //
int n = p.size();
vector<bool> vis(n, 0);
UF2.init(n);
vector<vector<int>> ans(n, vector<int>(n, 0));
for(int i = 0; i < n; i++){
if(!vis[i]){
vis[i] = 1;
for(int j = 0; j < n; j++){
if(i == j || p[i][j] == 0) continue;
if(vis[j] == 1) return 0;
vis[j] = 1;
UF2.unite(i, j);
}
}else{
for(int j = 0; j < n; j++){
if((p[i][j] != 0 && vis[j] == false) || (p[i][j] != 0 && vis[j] == 1 && UF2.get(i) != UF2.get(j))){
return 0;
}
}
}
}
if(UF2.check(p, ans)){
build(ans);
return 1;
}else return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |