#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);
}
}
}
int construct(vector<vector<int>>p){
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);
return 1;
}
#endif // SUPERTREES_H_INCLUDED
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:71:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | for(int j = 1;j < components.size();++j){
| ~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Answer gives possible 0 while actual possible 1 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Answer gives possible 0 while actual possible 1 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
11 ms |
1236 KB |
Output is correct |
9 |
Correct |
223 ms |
25988 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
8 ms |
1356 KB |
Output is correct |
13 |
Correct |
221 ms |
25988 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
7 ms |
852 KB |
Output is correct |
17 |
Correct |
119 ms |
16004 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
55 ms |
6764 KB |
Output is correct |
22 |
Correct |
214 ms |
25984 KB |
Output is correct |
23 |
Correct |
211 ms |
25988 KB |
Output is correct |
24 |
Correct |
239 ms |
25916 KB |
Output is correct |
25 |
Correct |
69 ms |
16076 KB |
Output is correct |
26 |
Correct |
80 ms |
16004 KB |
Output is correct |
27 |
Correct |
221 ms |
25988 KB |
Output is correct |
28 |
Correct |
234 ms |
25956 KB |
Output is correct |
29 |
Correct |
75 ms |
16004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Incorrect |
0 ms |
212 KB |
Too many ways to get from 1 to 4, should be 1 found no less than 2 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Answer gives possible 0 while actual possible 1 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Answer gives possible 0 while actual possible 1 |
3 |
Halted |
0 ms |
0 KB |
- |