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 <vector>
const int MAXN = 1000;
int parent[MAXN], sz[MAXN];
int find(int p){
while (p != parent[p])
p = parent[p];
return p;
}
void merge(int p, int q) {
int rootP = find(p);
int rootQ = find(q);
if (rootP == rootQ) return;
if (sz[rootP] < sz[rootQ]) {
parent[rootP] = rootQ;
sz[rootQ] += sz[rootP];
}
else {
parent[rootQ] = rootP;
sz[rootP] += sz[rootQ];
}
}
int construct(std::vector<std::vector<int> > p) {
int n = p.size();
std::vector<std::vector<int> > answer;
for (int i = 0; i < n; i++) {
std::vector<int> row;
row.resize(n);
answer.push_back(row);
}
for(int i = 0; i < n; i++){
parent[i] = i;
}
bool isOne = false;
bool isTwo = false;
for(int i = 0; i < n; i++){
for(int j = i + 1; j < n; j++){
if(p[i][j])
merge(i, j);
if(p[i][j] == 1)
isOne = true;
if(p[i][j] == 2)
isTwo = true;
}
}
for(int i = 0; i < n; i++){
for(int j = i + 1; j < n; j++){
if(find(i) == find(j) && !p[i][j])
return 0;
}
}
if(isOne){
for(int i = 0; i < n; i++){
int r = find(i);
if(r != i){
answer[r][i] = 1;
answer[i][r] = 1;
}
}
}
if(isTwo){
for(int i = 0; i < n; i++){
int r = find(i);
if(r == i){
int prev = -1;
int count = 0;
int start = -1;
for(int j = 0; j < n; j++){
if(find(j) == r){
count++;
if(prev == -1){
prev = j;
start = j;
}
else{
answer[prev][j] = 1;
answer[j][prev] = 1;
prev = j;
}
}
}
if(count == 1){
continue;
}
if(count == 2){
return 0;
}
answer[prev][start] = 1;
answer[start][prev] = 1;
}
}
}
build(answer);
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... |