이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
vector<int>parent;
int UF(int u) {
if(parent[u] == u)
return u;
return parent[u] = UF(parent[u]);
}
int construct(vector<vector<int>> p) {
const int n = (int)p.size();
parent.resize(n);
vector<vector<int>>ans(n,vector<int>(n,0));
for(int i = 0 ; i < n ; i++)
parent[i] = i;
for(int i = 0 ; i < n ; i++) {
for(int j = i+1 ; j < n ; j++) {
if(p[i][j] == 3) {
return 0; // invalid
}
assert(p[i][j] == p[j][i]);
if(p[i][j] == 1) {
int u = UF(i), v = UF(j);
if(u != v) {
ans[i][j] = ans[j][i] = 1;
parent[u] = v;
}
}
}
}
for(int i = 0 ; i < n ; i++) {
for(int j = i+1 ; j < n ; j++) {
if((((p[i][j] == 0 || p[i][j] == 2) && UF(i) == UF(j))) || (p[i][j] == 1 && UF(i) != UF(j))) {
return 0;
}
}
}
vector<int>remember(n,-1);
vector<int>from(n);
vector<int>sz(n,1);
for(int i = 0 ; i < n ; i++)
from[i]=i;
for(int i = 0 ; i < n ; i++) {
for(int j = i+1 ; j < n ; j++) {
if(p[i][j] == 2) {
int root_a = UF(i), root_b = UF(j);
if(root_a == root_b)
continue;
vector<int>a,b;
for(int k = 0 ; k < n ; k++) {
if(UF(k) == root_a)
a.push_back(k);
else if(UF(k) == root_b)
b.push_back(k);
}
for(auto aa : a)
for(auto bb : b)
if(p[aa][bb] != 2)
return 0;
parent[root_a] = root_b;
ans[root_a][from[root_b]] = 1;
ans[from[root_b]][root_a] = 1;
remember[root_a] = -1;
remember[root_b] = from[root_a];
from[root_b] = remember[root_b];
sz[root_b]+=sz[root_a];
sz[root_a] = 0;
}
}
}
for(int i = 0 ; i < n ; i++) {
if(remember[i] != -1) {
ans[i][remember[i]] = 1;
ans[remember[i]][i] = 1;
}
if(sz[i] == 2) {
return 0;
}
}
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... |