# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
301155 | andrew | 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>
#include "supertrees.h"
#define fi first
#define se second
#define pii pair <int, int>
#define pll pair <ll, ll>
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define p_b push_back
using namespace std;
typedef long long ll;
const ll N = 2e3;
int p1[N], p2[N];
int get(int x){
if(p1[x] != x)p1[x] = get(p1[x]);
return p1[x];
}
int get1(int x){
if(p2[x] != x)p2[x] = get1(p2[x]);
return p2[x];
}
int construct(vector <vector <int> > p){
int n = sz(p);
vector <vector <int> > answer(n, vector <int> (n, 0));
vector <vector <int> > mb(n, vector <int> (n, 0));
iota(p1, p1 + n, 0);
iota(p2, p2 + n, 0);
int msk = 1;
for(int i = 0; i < n; i++)
for(int j = i + 1; j < n; j++){
if(p[i][j] == 1 && get(i) != get(j))p1[get(i)] = get(j);
msk |= 1 << p[i][j];
}
for(int i = 0; i < n; i++)if(get(i) == i){
vector <int> vc;
for(int j = 0; j < n; j++){
if(get(j) == i){
vc.p_b(j);
for(int i1 = 0; i1 < n; i1++)if(p[j][i1] == 2){
if(get(i1) == get(j))return 0;
mb[i][i1] = 1;
}
}
}
for(int j = 1; j < sz(vc); j++){
answer[vc[j - 1]][vc[j]] = answer[vc[j]][vc[j - 1]] = 1;
}
for(int i = 0; i < n; i++)if(mb[i][j]){
if(get1(get(j)) != get1(i)){
p2[get1(get(j))] = get1(i);
}
}
}
for(int i = 0; i < n; i++)if(get1(i) == i){
vector <int> vc;
for(int j = 0; j < n; j++)if(get(j) == j){
if(get1(j) == i)vc.p_b(j);
}
if(!sz(vc))continue;
if(sz(vc) == 2)return 0;
for(int j = 1; j < sz(vc); j++){
answer[vc[j - 1]][vc[j]] = answer[vc[j]][vc[j - 1]] = 1;
}
if(sz(vc) != 1){
answer[vc[0]][vc.back()] = answer[vc.back()][vc[0]] = 1;
}
}
for(int i = 0; i < n; i++){
for(int j = i + 1; j < n; j++){
int m = 0;
if(get(i) == get(j))m = 1;
if(get(i) == get(j))m = 2;
if(m != p[i][j] && msk != 7)return 0;
}
}
iota(p1, p1 + n, 0);
for(int i = 0; i < n; i++){
for(int j = i + 1; j < n; j++){
if(answer[i][j] && get(i) != get(j)){
p1[get(i)] = get(j);
}
}
}
for(int i = 0; i < n; i++)
for(int j = i + 1; j < n; j++){
if(p[i][j] && get(i) != get(j))return 0;
if(!p[i][j] && get(i) == get(j))return 0;
}
build(answer);
return 1;
}