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;
struct DSU{
vector<int> parent, sz;
void init(int n){
parent.resize(n), sz.resize(n);
for(int i = 0; i < n; i ++) parent[i] = i, sz[i] = 1;
}
int get(int a){
return parent[a] == a ? a : parent[a] = get(parent[a]);
}
void unite(int a, int b){
a = get(a); b = get(b);
if(a==b) return;
if(sz[a] < sz[b]) swap(a,b);
sz[a] += sz[b];
parent[b] = a;
}
};
int construct(vector<vector<int>> p) {
bool ok = 0;
bool yes1 = 0, yes2 = 0;
int n = p.size();DSU gr; gr.init(n);
vector<vector<int>> b(n,vector<int> (n));
DSU t; t.init(n);
for(int j = 0; j < n; j ++){
for(int i = 0; i < n; i ++){
if(p[i][j] == 1) t.unite(i,j),gr.unite(i,j);
if(p[i][j] == 3) ok = 1;
if(p[i][j] == 2) yes2 = 1;
}
}
if(ok) return 0;
for(int i = 0; i < n; i ++){
for(int j=0; j < n; j ++){
if(gr.get(i) == gr.get(j) && p[i][j] != 1) return 0;
}
}
vector<vector<int>> par(n);
for(int i = 0; i < n; i ++){
par[t.get(i)].push_back(i);
}
vector<int> comp;
for(int i = 0; i < n; i ++){
if(par[i].empty()) continue;
for(int j = 1; j < par[i].size(); j ++){
int u = par[i][j-1], v = par[i][j];
b[u][v] = b[v][u] = 1; gr.unite(v,u);
}
comp.push_back(par[i][0]);
}
t.init(n);
for(int i = 0; i < comp.size(); i ++){
for(int j = i + 1; j < comp.size(); j++){
int u = comp[i], v = comp[j];
if(p[u][v] == 2){
t.unite(u,v); //cout << u << ' ' << v << endl;
}
}
}
for(int i = 0; i < n; i ++) par[i].clear();
for(int i = 0; i < n; i ++){
par[t.get(i)].push_back(i);
}
for(int i = 0; i < n; i ++){
if(par[i].size() == 2) return 0;
for(int j = 1; j < par[i].size(); j ++){
int u = par[i][j-1], v = par[i][j];
b[u][v] = b[v][u] = 1; gr.unite(v,u);
}
if(par[i].size() > 1){
int u = par[i][0], v = par[i].back();
b[u][v] = b[v][u] = 1; gr.unite(u,v);
}
}
for(int i = 0; i < n; i ++){
for(int j=0; j < n; j ++){
if(gr.get(i) == gr.get(j) && p[i][j] == 0) return 0;
}
}
build(b);
return 1;
}
Compilation message (stderr)
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:60:26: 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 = 1; j < par[i].size(); j ++){
| ~~^~~~~~~~~~~~~~~
supertrees.cpp:67:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for(int i = 0; i < comp.size(); i ++){
| ~~^~~~~~~~~~~~~
supertrees.cpp:68:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for(int j = i + 1; j < comp.size(); j++){
| ~~^~~~~~~~~~~~~
supertrees.cpp:84:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for(int j = 1; j < par[i].size(); j ++){
| ~~^~~~~~~~~~~~~~~
supertrees.cpp:30:10: warning: unused variable 'yes1' [-Wunused-variable]
30 | bool yes1 = 0, yes2 = 0;
| ^~~~
supertrees.cpp:30:20: warning: variable 'yes2' set but not used [-Wunused-but-set-variable]
30 | bool yes1 = 0, yes2 = 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... |