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> v;
void init(int n){
v.assign(n, -1);
}
int get(int x){return v[x] < 0 ? x : v[x] = get(v[x]);}
void unite(int x, int y){
x = get(x); y = get(y);
if(x == y) return;
if(x > y) swap(x, y);
v[x] += v[y]; v[y] = x;
return;
}
bool check(vector<vector<int>>& p, vector<vector<int>>& ans){
for(int i = 0; i < p.size(); i++){
for(int j = 0; j < p.size(); j++){
if(p[i][j] == 1){
if(get(i) != get(j)) return 0;
}else{
if(get(i) == get(j)) return 0;
}
}
}
for(int i = 0; i < v.size(); i++){
if(v[i] < 0){
int lst = i;
for(int j = 0; j < v.size(); j++){
if(j == i) continue;
if(v[j] == i){
ans[j][lst] = ans[lst][j] = 1;
lst = j;
}
}
}
}
return 1;
}
};
DSU UF;
void DFS(int x, vector<vector<int>>& adj, int r, vector<bool>& vis){
UF.unite(r, x);
if(vis[x]) return;
vis[x] = 1;
for(int i = 0; i < adj.size(); i++){
if(i == x || adj[i][x] == 0) continue;
DFS(i, adj, r, vis);
}
}
int construct(vector<vector<int>> p) {
int n = p.size();
vector<bool> vis(n, 0);
bool pos = 1;
UF.init(n);
vector<vector<int>> ans(n, vector<int>(n, 0));
for(int i = 0; i < n; i++){
if(!vis[i]){
DFS(i, p, i, vis);
}
}
if(UF.check(p, ans)){
//build(ans);
for(auto v: ans){
for(int x : v) cout << x << " ";
cout << endl;
}
return 1;
}else return 0;
}
Compilation message (stderr)
supertrees.cpp: In member function 'bool DSU::check(std::vector<std::vector<int> >&, std::vector<std::vector<int> >&)':
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:24: 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:31:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for(int i = 0; i < v.size(); i++){
| ~~^~~~~~~~~~
supertrees.cpp:34:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for(int j = 0; j < v.size(); j++){
| ~~^~~~~~~~~~
supertrees.cpp: In function 'void DFS(int, std::vector<std::vector<int> >&, int, std::vector<bool>&)':
supertrees.cpp:53:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for(int i = 0; i < adj.size(); i++){
| ~~^~~~~~~~~~~~
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:62:7: warning: unused variable 'pos' [-Wunused-variable]
62 | bool pos = 1;
| ^~~
# | 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... |