# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
578065 | 1ne | Connecting Supertrees (IOI20_supertrees) | C++14 | 204 ms | 22108 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<int>cyc(n,0);
for (int i = 0;i<n;++i){
vector<int>cycle;
cycle.push_back(st.findsets(i));
for (int j = 0;j<n;++j){
if (p[i][j] == 2){
if (st.findsets(i) == st.findsets(j))continue;
cycle.push_back(st.findsets(j));
st.unionset(i,j);
}
}
if (cycle.size()==2)return 0;
if (cycle.size()==1)continue;
cyc[st.findsets(i)]++;
for (int j = 0;j<cycle.size();++j){
answer[cycle[j]][cycle[(j + 1)%(int)cycle.size()]] = true;
answer[cycle[(j + 1)%(int)cycle.size()]][cycle[j]] = true;
}
}
for (int i = 0;i<n;++i){
for (int j = 0;j<n;++j){
if (p[i][j] && st.findsets(i)!=st.findsets(j))return 0;
if (!p[i][j] && st.findsets(i)==st.findsets(j))return 0;
}
}
for (int i = 0;i<n;++i){
if (cyc[st.findsets(i)] > 1)return 0;
}
build(answer);
return 1;
}
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... |