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 {
vector<int> dsu, sz;
DSU (int n) {
dsu.resize(n);
iota(dsu.begin(), dsu.end(), 0);
sz.resize(n, 1);
}
int find (int u) {
return u == dsu[u] ? u : dsu[u] = find(dsu[u]);
}
bool unite (int u, int v) {
if ((u = find(u)) != (v = find(v))) {
if (sz[u] < sz[v]) {
swap(u, v);
}
dsu[v] = u;
sz[u] += sz[v];
return 1;
}
return 0;
}
};
int construct(vector<vector<int>> p) {
int n = p.size();
vector<vector<int>> ans(n, vector<int>(n));
DSU nodes(n);
bool two = 0;
auto sit = [&] (int i, int j, int t) {
ans[i][j] = ans[j][i] = t;
};
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (p[i][j] == 1) {
sit(i, j, nodes.unite(i, j));
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (p[i][j] == 0 && nodes.find(i) == nodes.find(j)) {
return 0;
}
}
}
vector<vector<int>> c(n);
for (int i = 0; i < n; ++i) {
c[nodes.find(i)].push_back(i);
}
auto Do = [&] (int e) {
DSU cmp(n);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (p[i][j] == e) {
cmp.unite(nodes.find(i), nodes.find(j));
}
}
}
vector<vector<int>> C(n);
for (int i = 0; i < n; ++i) {
C[cmp.find(i)].push_back(i);
}
for (int i = 0; i < n; ++i) {
if (i != cmp.find(i)) {
continue;
}
for (int j = 0; j < n; ++j) {
if (nodes.find(i) != nodes.find(j) && cmp.find(i) == cmp.find(j) && p[i][j] != e) {
return 0;
}
}
for (int j : c[i]) {
for (int k = 0; k < n; ++k) {
if ((p[k][i] == e) ^ (p[j][k] == e)) {
return 0;
}
}
}
for (int j = 0; j < C[i].size(); ++j) {
for (int k = j + 1; k < C[i].size(); ++k) {
if (p[C[i][j]][C[i][k]] ^ e) {
return 0;
}
}
}
if (C[i].size() > 1 && C[i].size() <= e) {
return 0;
}
if (C[i].size() > e) {
sit(C[i][0], C[i][1], 1);
sit(C[i][1], C[i][2], 1);
for (int j = 3; j < C[i].size(); ++j) {
sit(C[i][j], C[i][j - 1], 1);
}
sit(C[i].back(), C[i][0], 1);
if (e == 3) {
sit(C[i][1], C[i][3], 1);
}
}
}
return 1;
};
if (!Do(2) || !Do(3)) {
return 0;
}
build(ans);
return 1;
}
Compilation message (stderr)
supertrees.cpp: In lambda function:
supertrees.cpp:87:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | for (int j = 0; j < C[i].size(); ++j) {
| ~~^~~~~~~~~~~~~
supertrees.cpp:88:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | for (int k = j + 1; k < C[i].size(); ++k) {
| ~~^~~~~~~~~~~~~
supertrees.cpp:94:42: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
94 | if (C[i].size() > 1 && C[i].size() <= e) {
| ~~~~~~~~~~~~^~~~
supertrees.cpp:97:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
97 | if (C[i].size() > e) {
| ~~~~~~~~~~~~^~~
supertrees.cpp:100:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
100 | for (int j = 3; j < C[i].size(); ++j) {
| ~~^~~~~~~~~~~~~
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:36:8: warning: unused variable 'two' [-Wunused-variable]
36 | bool two = 0;
| ^~~
# | 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... |