# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
312002 | Blerargh | 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 <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for (int i=(a); i<=(b); i++)
#define ROF(i,a,b) for (int i=(a); i>=(b); i--)
#define gay cout << "gay";
bool visited[1005];
int parent[1005], n;
vector<vector<int> > answer, pp;
int fp(int a){
if (a == parent[a]) return a;
else return parent[a] = fp(parent[a]);
}
bool join(int a, int b){
int pa = fp(a);
int pb = fp(b);
if (pa!=pb) {
parent[b] = a;
return 1;
} else return 0;
}
void dfs(int u, int start){
visited[fp(u)] = 1;
bool check=0;
FOR(i,0,n-1){
if (pp[u][i] != 2) continue;
else if (!visited[fp(i)]) {
answer[u][i] = answer[i][u] = 1;
dfs(i, start);
check = 1;
}
}
if (!check) answer[u][start] = answer[start][u] = 1;
return;
}
vector<vector<int> > chk;
bool visiting[1005];
void dfscheck(int u, int start){
//cout << u << '\n';
visiting[u] = 1;
chk[start][u]++;
FOR(i,0,n-1){
if (!answer[u][i]) continue;
if (!visiting[i]) dfscheck(i, start);
}
visiting[u] = 0;
return;
}
bool test(){
FOR(i,0,n-1){
visiting[i] = 0;
vector<int> row;
row.resize(n);
chk.push_back(row);
FOR(j,0,n-1) chk[i][j] = 0;
}
FOR(i,0,n-1){
dfscheck(i,i);
}
/*
FOR(i,0,n-1){
FOR(j,0,n-1){
cout << chk[i][j] << " ";
}
cout << '\n';
}
*/
if (chk == pp) return 1;
else return 0;
}
int construct(vector<vector<int> > p){
pp = p;
n = p.size();
FOR(i,0,n-1){
parent[i] = i;
visited[i] = 0;
}
answer.clear();
FOR(i,0,n-1){
vector<int> row;
row.resize(n);
answer.push_back(row);
}
bool imposs=0;
FOR(i,0,n-1) {
FOR(j,0,n-1){
if (i==j && p[i][j] != 1) imposs=1;
if (p[i][j] != p[j][i]) imposs=1;
if (p[i][j] == 3) imposs=1;
if (imposs) break;
if (p[i][j] == 1 && join(i,j)) answer[i][j] = answer[j][i] = 1;
}
if (imposs) break;
}
if (imposs) return 0;
FOR(i,0,n-1){
if (!visited[fp(i)]) dfs(i,i);
}
if (test()) {
build(answer);
return 1;
} else return 0;
}