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 <bits/stdc++.h>
using namespace std;
struct dsu{
int n;
vector<int> size, parent;
dsu(int _n) : n(_n), size(n, 1), parent(n){
iota(parent.begin(), parent.end(), 0);
}
int get(int a){
return a == parent[a] ? a : parent[a] = get(parent[a]);
}
bool unite(int b, int a){
a = get(a);
b = get(b);
if(a == b){
return 0;
}
size[b] += size[a];
parent[a] = b;
return 1;
}
};
int construct(vector<vector<int>> a){
int n = (int)a.size();
vector<vector<int>> ans(n, vector<int>(n, 0));
vector<vector<int>> v1(n), v2(n);
for(int i = 0; i < n; i++){
for(int j = i + 1; j < n; j++){
if(a[i][j] == 1){
v1[i].push_back(j);
v1[j].push_back(i);
} else if(a[i][j] == 2){
v2[i].push_back(j);
v2[j].push_back(i);
} else if(a[i][j] == 3){
return 0;
}
}
}
dsu s(n);
for(int i = 0; i < n; i++){
if(s.parent[i] == i){
for(int j = 0; j < n; j++){
for(int k : v1[i]){
if(a[k][j] != a[i][j]){
return 0;
}
ans[i][k] = 1;
ans[k][i] = 1;
s.unite(i, k);
}
}
}
}
vector<bool> b(n, false);
for(int i = 0; i < n; i++){
if(b[s.parent[i]]){
continue;
}
b[i] = true;
int cur = i;
for(int j : v2[i]){
if(s.parent[j] != j){
if(a[i][s.parent[j]] != 2){
return 0;
}
continue;
}
for(int k : v2[j]){
if(a[i][k] == 0 || (a[i][k] == 1 && s.parent[k] != i) || (a[i][k] == 2 && s.parent[k] == i)){
return 0;
}
}
for(int k : v2[i]){
if(a[j][k] == 0 || (a[j][k] == 1 && s.parent[k] != j) || (a[j][k] == 2 && s.parent[k] == j)){
return 0;
}
}
ans[cur][i] = 1;
ans[i][cur] = 1;
b[j] = true;
cur = j;
}
if(cur == i){
continue;
}
if(ans[i][cur]){
return 0;
}
ans[i][cur] = 1;
ans[cur][i] = 1;
}
build(ans);
return 1;
}
# | 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... |