# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
536373 | ddy888 | 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.
#undef _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define fi first
#define si second
#define ar array
typedef pair<int,int> pi;
typedef tuple<int,int,int> ti;
void debug_out() {cerr<<endl;}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {cerr<<" "<<to_string(H);debug_out(T...);}
#define debug(...) cerr<<"["<<#__VA_ARGS__<<"]:",debug_out(__VA_ARGS__)
// #include "supertrees.h"
int N;
int A[1010][1010];
vector<vector<int> > ans;
set<int> rings;
vector<int> adj[1010];
struct dsu {
int par[1010];
void init(int x) {for (int i = 0; i <= x; ++i) par[i] = i;}
int root(int x) { return (par[x]==x)? x:par[x]=root(par[x]); }
bool same(int a, int b) { return root(a) == root(b); }
void merge(int a, int b) { // b absorbs a
par[root(a)] = root(b);
}
};
void join(int x, int y) {
ans[x - 1][y - 1] = 1;
ans[y - 1][x - 1] = 1;
}
int construct(std::vector<std::vector<int>> p) {
N = p.size();
ans.resize(N, vector<int>(N));
dsu one, two;
one.init(N), two.init(N);
for (int i = 1; i <= N; ++i) {
for (int j = 1; j <= N; ++j) {
A[i][j] = p[i - 1][j - 1];
if (A[i][j] == 3) return 0;
if (A[i][j] == 2) two.merge(one.root(i), one.root(j));
if (A[i][j] == 1) {
one.merge(i, j);
join(i, j);
}
}
}
for (int i = 1; i <= N; ++i) rings.insert(two.root(i));
for (int i = 1; i <= N; ++i) adj[two.root(i)].pb(i);
for (int i = 1; i <= N; ++i) {
if (adj[i].size() == 2) return 0;
int p = -1;
for (auto j: adj[i]) {
if (p != -1) join(p, j);
p = j;
}
for (auto j: adj[i]) {
for (auto k: adj[i]) {
if (j == k) continue;
if (A[j][k] != 2) return 0;
}
}
}
for (int i= 1; i <= N; ++i) {
for (int j = 1; j <= N; ++j) {
if (A[i][j] == 0 && (one.same(i, j) || two.same(i, j))) return 0;
}
}
build(ans);
return 1;
}