#ifndef SUPERTREES_H_INCLUDED
#define SUPERTREES_H_INCLUDED
#include "supertrees.h"
#include<bits/stdc++.h>
using namespace std;
struct DSU{
vector<int>par;
DSU(int n){
par.assign(n, -1);
}
int FindRep(int u){
if(par[u] < 0)return u;
return par[u] = FindRep(par[u]);
}
void Union(int u, int v){
u = FindRep(u);
v = FindRep(v);
if(u == v)return;
if(par[u] > par[v])swap(u, v);
par[u] += par[v];
par[v] = u;
}
bool IsConnected(int u, int v){
u = FindRep(u);
v = FindRep(v);
return (u == v);
}
};
int N;
void build(vector<vector<int>>b);
vector<bool>vis;
vector<vector<int>>B;
vector<int>components;
void dfs(int u){
components.push_back(u);
vis[u] = 1;
for(int i = 0;i < N;++i){
if(B[u][i] && !vis[i]){
dfs(i);
}
}
}
bool Check(vector<vector<int>>p){
for(int i = 0;i < N;++i){
for(int j = 0;j < N;++j){
if(p[i][j] == 2)return 1;
}
}
return 0;
}
int construct(vector<vector<int>>p){
bool ch = Check(p);
if(ch){
N = p.size();
DSU dsu1(N);
vis.resize(N);
B.assign(N, vector<int>(N));
vector<vector<int>>ans(N, vector<int>(N));
for(int i = 0;i < N;++i){
for(int j = 0;j < N;++j){
if(p[i][j] && i != j){
if(dsu1.IsConnected(i, j))continue;
dsu1.Union(i, j);
B[i][j] = B[j][i] = 1;
}
}
}
for(int i = 0;i < N;++i){
for(int j = 0;j < N;++j){
if(i != j && !p[i][j] && dsu1.IsConnected(i, j))return 0;
}
}
for(int i = 0;i < N;++i){
int p = dsu1.FindRep(i);
if(dsu1.par[p] == -2)return 0;
}
for(int i = 0;i < N;++i){
if(!vis[i]){
dfs(i);
if(components.size() > 2){
for(int j = 1;j < components.size();++j){
ans[components[j]][components[j - 1]] = ans[components[j - 1]][components[j]] = 1;
}
ans[components.back()][components[0]] = ans[components[0]][components.back()] = 1;
}
components.clear();
}
}
build(ans);
}
else{
int n = N;
DSU dsu1(n);
vector<vector<int>>b(n, vector<int>(n));
for(int i = 0;i < n;++i){
for(int j = 0;j < n;++j){
if(p[i][j] && i != j){
if(dsu1.IsConnected(i, j))continue;
dsu1.Union(i, j);
b[i][j] = b[j][i] = 1;
}
}
}
for(int i = 0;i < n;++i){
for(int j = 0;j < n;++j){
if(i != j && !p[i][j] && dsu1.IsConnected(i, j))return 0;
}
}
build(b);
}
return 1;
}
#endif // SUPERTREES_H_INCLUDED
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:81:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | for(int j = 1;j < components.size();++j){
| ~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
WA in grader: Invalid number of rows in b |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
WA in grader: Invalid number of rows in b |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
296 KB |
WA in grader: Invalid number of rows in b |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
WA in grader: Invalid number of rows in b |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
WA in grader: Invalid number of rows in b |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
WA in grader: Invalid number of rows in b |
2 |
Halted |
0 ms |
0 KB |
- |