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;
vector<int> lab;
int get_root(int u) {
return lab[u] == u ? u : lab[u] = get_root(lab[u]);
}
bool unite(int u, int v) {
u = get_root(u);
v = get_root(v);
if(u == v) return false;
lab[v] = u;
return true;
}
int construct(vector<vector<int>> p) {
int n = p.size();
lab.resize(n);
for(int i = 0; i < n; i ++) lab[i] = i;
vector<int> mark(n, 0);
vector<vector<int>> answer(n, vector<int> (n, 0));
for (int i = 0; i < n; i ++) {
for(int j = 0; j < n; j ++) {
if(get_root(i) == get_root(j) || p[i][j] != 1) continue ;
answer[i][j] = answer[j][i] = 1;
unite(i, j);
}
}
for(int i = 0; i < n; i ++) {
if(get_root(i) != i) continue ;
vector<int> row;
for(int j = 0; j < n; j ++) {
if(p[i][j] != 2) continue ;
if(mark[j]) return 0;
row.push_back(j);
}
if(row.empty()) continue ;
int root = i;
int k = i;
int cnt = 1;
for(int j = 0; j < row.size(); j ++) {
if(unite(k, row[j])) {
answer[row[j]][k] = 1;
answer[k][row[j]] = 1;
k = row[j];
cnt ++ ;
}
}
answer[root][k] = 1;
answer[k][root] = 1;
for(int j = 0; j < n; j ++) {
if(get_root(i) == get_root(j)) {
mark[j] = 1;
}
}
for(int x : row) {
for(int y : row) {
if(x == y) continue ;
if(p[x][y] != 2) return 0;
}
}
if(cnt <= 2) return 0;
}
for(int i = 0; i < n; i ++) {
if(get_root(i) != i) continue ;
vector<int> row;
for(int j = 0; j < n; j ++) {
if(p[i][j] != 3) continue ;
if(mark[j]) return 0;
row.push_back(j);
}
if(row.empty()) continue ;
int root = i;
int k = i, prev_k = 0;
int cnt = 1;
for(int j = 0; j < row.size(); j ++) {
if(unite(k, row[j])) {
answer[j][k] = 1;
answer[k][j] = 1;
prev_k = k;
k = j;
cnt ++ ;
}
}
answer[root][k] = 1;
answer[k][root] = 1;
answer[root][prev_k] = 1;
answer[prev_k][root] = 1;
for(int j = 0; j < n; j ++) {
if(get_root(i) == get_root(j)) {
mark[j] = 1;
}
}
for(int x : row) {
for(int y : row) {
if(x == y) continue ;
if(p[x][y] != 3) return 0;
}
}
if(cnt <= 3) return 0;
}
build(answer);
return 1;
}
Compilation message (stderr)
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:56:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for(int j = 0; j < row.size(); j ++) {
| ~~^~~~~~~~~~~~
supertrees.cpp:101:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
101 | for(int j = 0; j < row.size(); 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... |