| # | Time | Username | Problem | Language | Result | Execution time | Memory |
|---|---|---|---|---|---|---|---|
| 578021 | 1ne | 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 "supertrees.h"
#include <vector>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
struct DSU{
vector<int>parent;
vector<int>sz;
vector<vector<int>>comp;
void build(int n){
parent.resize(n);
sz.resize(n);
comp.resize(n);
for (int i = 0;i<n;++i){
parent[i] = i;
sz[i] = 1;
comp[i].push_back(i);
}
}
int findsets(int v){
if (v == parent[v])return v;
parent[v] = findsets(parent[v]);
return parent[v];
}
bool unionset(int a,int b){
int u = findsets(a);
int v = findsets(b);
if (u == v)return false;
if (sz[u] < sz[v])swap(u,v);
parent[v] = u;
sz[u]+=sz[v];
sz[v] = 0;
while(!comp[v].empty()){
comp[u].push_back(comp[v].back());
comp[v].pop_back();
}
return true;
};
};
int construct(std::vector<std::vector<int>> p) {
int n = p.size();
std::vector<std::vector<int>> answer(n,vector<int>(n,0));
for (int i = 0;i<n;++i){
for (int j = 0;j<n;++j){
if (p[i][j] == 3)return 0;
}
}
DSU st;
st.build(n);
for (int i = 0;i<n;++i){
for (int j = i + 1;j<n;++j){
int u = st.findsets(i);
int v = st.findsets(j);
if (p[i][j]==1 && st.unionset(i,j)){
answer[u][v] = true;
answer[v][u] = true;
}
}
}
vector<vector<bool>>pre(n,vector<bool>(n,false));
for (int i = 0;i<n;++i){
for (int j = i + 1;j<n;++j){
if (st.findsets(i) == st.findsets(j)){
p[i][j]--;
p[j][i]--;
pre[i][j] = true;
pre[j][i] = true;
}
}
}
for (int i = 0;i<n;++i){
for (int j = i + 1;j<n;++j){
if (p[i][j] > 0){
answer[i][j] = true;
answer[j][i] = true;
int y = st.findsets(i);
int q = st.findsets(j);
if (y == q){
for (auto qqq:st.comp[y]){
for (auto qq:st.comp[y]){
if (qqq == qq)continue;
if (pre[qqq][qq])continue;
p[qqq][qq]--;
}
}
continue;
}
vector<vector<bool>>visited(n,vector<bool>(n,false));
for (auto x:st.comp[y]){
visited[x][j] = true;
visited[j][x] = true;
}
for (auto x:st.comp[q]){
visited[i][x]=true;
visited[x][i]=true;
}
for (int qqq = 0;qqq<n;++qqq){
for (int qq = 0;qq<n;++qq){
p[qqq][qq]-=visited[qqq][qq];
}
}
st.unionset(i,j);
}
}
}
for (int i = 0;i<n;++i){
for (int j = 0;j < n;++j){
if (i == j)continue;
if (p[i][j] != 0){
//cout<<i<<" "<<j<<" "<<p[i][j]<<'\n';
return 0;
}
}
}
build(answer);
return 1;
}int construct(std::vector<std::vector<int>> p) {
int n = p.size();
std::vector<std::vector<int>> answer(n,vector<int>(n,0));
for (int i = 0;i<n;++i){
for (int j = 0;j<n;++j){
if (p[i][j] == 3)return 0;
}
}
DSU st;
st.build(n);
for (int i = 0;i<n;++i){
for (int j = i + 1;j<n;++j){
int u = st.findsets(i);
int v = st.findsets(j);
if (p[i][j]==1 && st.unionset(i,j)){
answer[u][v] = true;
answer[v][u] = true;
}
}
}
vector<vector<bool>>pre(n,vector<bool>(n,false));
for (int i = 0;i<n;++i){
for (int j = i + 1;j<n;++j){
if (st.findsets(i) == st.findsets(j)){
p[i][j]--;
p[j][i]--;
pre[i][j] = true;
pre[j][i] = true;
}
}
}
for (int i = 0;i<n;++i){
for (int j = i + 1;j<n;++j){
if (p[i][j] > 0){
answer[i][j] = true;
answer[j][i] = true;
int y = st.findsets(i);
int q = st.findsets(j);
if (y == q){
for (auto qqq:st.comp[y]){
for (auto qq:st.comp[y]){
if (qqq == qq)continue;
if (pre[qqq][qq])continue;
p[qqq][qq]--;
}
}
continue;
}
vector<vector<bool>>visited(n,vector<bool>(n,false));
for (auto x:st.comp[y]){
visited[x][j] = true;
visited[j][x] = true;
}
for (auto x:st.comp[q]){
visited[i][x]=true;
visited[x][i]=true;
}
for (int qqq = 0;qqq<n;++qqq){
for (int qq = 0;qq<n;++qq){
p[qqq][qq]-=visited[qqq][qq];
}
}
st.unionset(i,j);
}
}
}
for (int i = 0;i<n;++i){
for (int j = 0;j < n;++j){
if (i == j)continue;
if (p[i][j] != 0){
//cout<<i<<" "<<j<<" "<<p[i][j]<<'\n';
return 0;
}
}
}
build(answer);
return 1;
}
