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<bits/stdc++.h>
#include "supertrees.h"
using namespace std;
const int N = 1e3 + 10;
int f[N], a[N], P[N];
int find_set(int x) {
if(x == P[x]) return x;
return P[x] = find_set(P[x]);
}
void union_set(int x, int y) {
x = find_set(x);
y = find_set(y);
P[y] = x;
}
int construct(vector < vector < int > > p) {
for(int i = 0; i < p.size(); i++) P[i] = i;
for(int i = 0; i < p.size(); i++) {
for(int j = 0; j < p.size(); j++) {
if(p[i][j] != 0) union_set(i, j);
}
}
for(int i = 0; i < p.size(); i++) {
for(int j = 0; j < p.size(); j++) {
if(p[i][j] == 3 || (find_set(i) == find_set(j) && p[i][j] == 0)) return 0;
}
}
vector < vector < int > > vr(p.size(), vector < int > (0, 0));
for(int i = 0; i < p.size(); i++) {
int m = find_set(i);
vr[m].push_back(i);
}
int q = 0;
vector < vector < int > > ans(p.size(), vector < int > (p.size(), 0));
for(int i = 0; i < p.size(); i++) P[i] = i;
for(int i = 0; i < p.size(); i++) {
if(vr[i].size()) {
for(int u : vr[i]) {
for(int v : vr[i]) {
if(p[u][v] == 1) {
union_set(u, v);
}
}
}
for(int u : vr[i]) {
for(int v : vr[i]) {
if(p[u][v] == 2) {
if(find_set(u) == find_set(v)) return 0;
}
}
}
for(int j = 0; j < p.size(); j++) f[j] = -1e9;
for(int u : vr[i]) {
if(u == find_set(u)) {
a[++q] = u;
}
int m = find_set(u);
if(f[m] != -1e9) {
ans[f[m]][u] = 1;
ans[u][f[m]] = 1;
}
f[m] = u;
}
if(q == 2) return 0;
for(int i = 2; i <= q + 1; i++) {
if(q + 1 == i) {
if(a[i - 1] != a[i - q]) {
ans[a[i - 1]][a[i - q]] = 1;
ans[a[i - q]][a[i - 1]] = 1;
}
}
ans[a[i]][a[i - 1]] = 1;
ans[a[i - 1]][a[i]] = 1;
}
q = 0;
}
}
build(ans);
return 1;
}
// main() {
// cout << construct({{1, 1, 2 , 2}, {1, 1, 2, 2}, {2, 2, 1, 2}, {2, 2, 2, 1}}) << "\n";
// }
Compilation message (stderr)
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:16:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | for(int i = 0; i < p.size(); i++) P[i] = i;
| ~~^~~~~~~~~~
supertrees.cpp:17:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | for(int i = 0; i < p.size(); i++) {
| ~~^~~~~~~~~~
supertrees.cpp:18:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
18 | for(int j = 0; j < p.size(); j++) {
| ~~^~~~~~~~~~
supertrees.cpp:22:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for(int i = 0; i < p.size(); i++) {
| ~~^~~~~~~~~~
supertrees.cpp:23:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for(int j = 0; j < p.size(); j++) {
| ~~^~~~~~~~~~
supertrees.cpp:28:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | for(int i = 0; i < p.size(); i++) {
| ~~^~~~~~~~~~
supertrees.cpp:34:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for(int i = 0; i < p.size(); i++) P[i] = i;
| ~~^~~~~~~~~~
supertrees.cpp:35:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for(int i = 0; i < p.size(); i++) {
| ~~^~~~~~~~~~
supertrees.cpp:51:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for(int j = 0; j < p.size(); j++) f[j] = -1e9;
| ~~^~~~~~~~~~
# | 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... |