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 <vector>
#include <bits/stdc++.h>
using namespace std;
int get(int x, vector<int> &root)
{
if (x == root[x]) return x;
return root[x] = get(root[x], root);
}
void unit(int a, int b, vector<int> &root, vector<int> &sz){
a = get(a, root);
b = get(b, root);
if (a != b){
if (sz[a] < sz[b]){
swap(a, b);
}
root[b] = a;
sz[a] += sz[b];
}
}
int construct(std::vector<std::vector<int>> p) {
int n = p.size();
vector<int> root(n), sz(n);
for (int i = 0; i < n; i++) root[i] = i, sz[i] = 1;
for (int i = 0; i < n; i++){
root[i] = i; sz[i] = 1;
}
int check = 0;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
if (p[i][j] == 3){
return 0;
}
if (p[i][j]){
unit(i, j, root, sz);
// check = max(check, p[i][j]);
}
}
}
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
if (p[i][j] == 0 and get(i, root) == get(j, root)){ //impossible
return 0;
}
}
}
vector <vector<int>> answer(n, vector<int>(n));
// if(!check){
// build(answer);
// return 1;
// }
for (int i = 0; i < n; i++){
vector <int> branch;
for (int j = 0; j < n; j++){
if (get(j, root) == i){
branch.push_back(j);
}
}
vector <int> root2(branch.size()), sz2(branch.size(), 1);
for (int j = 0; j < branch.size(); j++){
root2[j] = j;
}
for (int j = 0; j < branch.size(); j++){
for (int k = 0; k < branch.size(); k++){
if (p[branch[j]][branch[k]] == 1){
unit(j, k, root2, sz2);
}
}
}
for (int j = 0; j < branch.size(); j++){
for (int k = 0; k < branch.size(); k++){
if (p[branch[j]][branch[k]] == 2 and get(j, root2) == get(k, root2)){
return 0;
}
}
}
vector <vector <int>> forest(branch.size());
for (int j = 0; j < branch.size(); j++){
forest[get(j, root2)].push_back(branch[j]);
}
vector <int> q;
for (int j = 0; j < forest.size(); j++){
if (forest[j].size()){
q.push_back(forest[j][0]);
// cout << "node: " << forest[j][0] << "\n";
// cout << forest[j].size() << "\n";
}
for (int k = 0; k + 1 < forest[j].size(); k++){
// cout << forest[j][k] << " " << forest[j][k + 1] << "\n";
answer[forest[j][k]][forest[j][k + 1]] = 1;
answer[forest[j][k + 1]][forest[j][k]] = 1;
}
}
if (q.size() == 2){
return 0;
}
if (q.size() > 2){
for (int j = 0; j < q.size(); j++){
answer[q[j]][q[(j + 1)%q.size()]] = 1;
answer[q[(j + 1)%q.size()]][q[j]] = 1;
}
}
// for (int j = 0; j + 1 < branch.size(); j++){
// answer[branch[j]][branch[j + 1]] = 1;
// answer[branch[j + 1]][branch[j]] = 1;
// }
// if (check == 2){
// if(branch.size() == 2){
// return 0;
// }
// if (branch.size() > 2){
// answer[branch[0]][branch.back()] = 1;
// answer[branch.back()][branch[0]] = 1;
// }
// }
}
build(answer);
return 1;
}
Compilation message (stderr)
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:60:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for (int j = 0; j < branch.size(); j++){
| ~~^~~~~~~~~~~~~~~
supertrees.cpp:63:21: 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 < branch.size(); j++){
| ~~^~~~~~~~~~~~~~~
supertrees.cpp:64:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for (int k = 0; k < branch.size(); k++){
| ~~^~~~~~~~~~~~~~~
supertrees.cpp:70:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for (int j = 0; j < branch.size(); j++){
| ~~^~~~~~~~~~~~~~~
supertrees.cpp:71:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | for (int k = 0; k < branch.size(); k++){
| ~~^~~~~~~~~~~~~~~
supertrees.cpp:78:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for (int j = 0; j < branch.size(); j++){
| ~~^~~~~~~~~~~~~~~
supertrees.cpp:82:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | for (int j = 0; j < forest.size(); j++){
| ~~^~~~~~~~~~~~~~~
supertrees.cpp:88:26: 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 = 0; k + 1 < forest[j].size(); k++){
| ~~~~~~^~~~~~~~~~~~~~~~~~
supertrees.cpp:98:22: 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 < q.size(); j++){
| ~~^~~~~~~~~~
supertrees.cpp:28:6: warning: unused variable 'check' [-Wunused-variable]
28 | int check = 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... |