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 <iostream>
#include <vector>
#include "supertrees.h"
using namespace std;
vector<int> v[1005], vv[1005];
int p[1005];
vector<vector<int>> res;
int Find(int x) {
if (x == p[x]) return x;
return p[x] = Find(p[x]);
}
void Merge(int x, int y, int fl) {
x = Find(x);
y = Find(y);
if (x == y) return;
if (v[x].size() < v[y].size()) {
swap(x, y);
}
for (int w : v[y]) v[x].push_back(w);
v[y].clear();
if (fl == 0) res[x][y] = res[y][x] = 1;
p[y] = x;
if (fl == 1) {
for (int w : vv[y]) vv[x].push_back(w);
}
}
void build(vector<vector<int>> b);
int construct(vector<vector<int>> a) {
int n = a.size();
res = vector<vector<int>>(n, vector<int>(n, 0));
for (int i = 0; i < n; i++) {
p[i] = i;
v[i].clear(); vv[i].clear();
v[i].push_back(i);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (a[i][j] == 1) {
Merge(i, j, 0);
}
if (a[i][j] == 3) return 0;
}
}
for (int i = 0; i < n; i++) {
if (Find(i) == i) {
for (int w : v[i]) {
for (int ww : v[i]) {
if (a[w][ww] != 1) return 0;
}
}
vv[i].push_back(i);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (a[i][j] == 2) {
Merge(i, j, 1);
}
}
}
for (int i = 0; i < n; i++) {
if (Find(i) == i) {
for (int j = 0; j < vv[i].size() - 1; j++) {
res[vv[i][j]][vv[i][j + 1]] = res[vv[i][j + 1]][vv[i][j]] = 1;
}
if (vv[i].size() != 1) res[vv[i][0]][vv[i].back()] = res[vv[i].back()][vv[i][0]] = 1;
if (vv[i].size() == 2) return 0;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (a[i][j] && Find(i) != Find(j)) return 0;
if (a[i][j] == 0 && Find(i) == Find(j)) return 0;
}
}
build(res);
return 1;
}
Compilation message (stderr)
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:63:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for (int j = 0; j < vv[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... |