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;
const int MAXN = 1111;
int par[MAXN][4], sz[MAXN][4];
int find_set(int x, int flag){
if (x == par[x][flag]) return x;
return par[x][flag] = find_set(par[x][flag], flag);
}
bool union_sets(int x, int y, int flag){
x = find_set(x, flag);
y = find_set(y, flag);
if (x == y) return false;
if (sz[x][flag] > sz[y][flag]) swap(x, y);
par[x][flag] = y;
sz[y][flag] += sz[x][flag];
return true;
}
bool same(int i, int j, int flag){
return find_set(i, flag) == find_set(j, flag);
}
vector<int> children[MAXN];
bool check(vector<vector<int>> &answer, vector<vector<int>> p){
int n = answer.size();
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
if (p[i][j] == 3) return false;
if (answer[i][j] == 1) union_sets(i, j, 3);
}
}
vector<int> cnt2(n);
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
if (p[i][j] == 0){
if (same(i, j, 3)) return false;
}
if (p[i][j] == 1){
if (!same(i, j, 3)) return false;
if (!same(i, j, 1)) return false;
}
if (p[i][j] == 2){
if (!same(i, j, 3)) return false;
if (same(i, j, 1)) return false;
cnt2[find_set(i, 3)]++;
}
}
}
for (int i = 0; i < n; i++){
if (cnt2[i] == 2) return false;
}
return true;
}
int construct(vector<vector<int>> p) {
int n = p.size();
vector<vector<int>> answer(n, vector<int>(n, 0));
for (int i = 0; i < n; i++){
par[i][1] = par[i][2] = par[i][3] = i;
sz[i][1] = sz[i][2] = sz[i][3] = 1;
}
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
if (p[i][j] == 1) union_sets(i, j, 1);
}
}
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
if (p[i][j] == 2){
int x = find_set(i, 1), y = find_set(j, 1);
union_sets(x, y, 2);
}
}
}
for (int i = 0; i < n; i++){
children[find_set(i, 2)].push_back(i);
}
for (int i = 0; i < n; i++){
if (children[i].size() < 2) continue;
children[i].push_back(children[i].front());
for (int j = 0; j < children[i].size() - 1; j++){
int x = children[i][j], y = children[i][j + 1];
answer[x][y] = answer[y][x] = 1;
}
}
for (int i = 0; i < n; i++){
int x = find_set(i, 1);
if (x != i) answer[i][x] = answer[x][i] = 1;
}
if (!check(answer, p)) return 0;
build(answer);
return 1;
}
/*
4
1 1 2 2
1 1 2 2
2 2 1 2
2 2 2 1
*/
Compilation message (stderr)
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:98:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
98 | for (int j = 0; j < children[i].size() - 1; j++){
| ~~^~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |