#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
int parent[1005], sz[1005], parent2[1005], sz2[1005];
int Find(int x) {
if (x==parent[x])
return x;
return parent[x]=Find(parent[x]);
}
int Find2(int x) {
if (x==parent2[x])
return x;
return parent2[x]=Find2(parent2[x]);
}
void Union(int a, int b) {
a=Find(a), b=Find(b);
if (a==b) return;
if (sz[a]<sz[b])
swap(a, b);
sz[a]+=sz[b];
parent[b]=a;
}
void Union2(int a, int b) {
a=Find2(a), b=Find2(b);
if (a==b) return;
if (sz2[a]<sz2[b])
swap(a, b);
sz2[a]+=sz2[b];
parent2[b]=a;
}
int construct(vector<vector<int> > paths) {
int n=paths.size();
vector<vector<int> > a(n, vector<int> (n, 0));
for (int i=0;i<n;i++)
sz[i]=1, parent[i]=i, sz2[i]=1, parent2[i]=i;
for (int i=0;i<n;i++)
for (int j=i+1;j<n;j++) {
if (paths[i][j]==3) return 0;
else if (paths[i][j]) Union(i, j);
}
vector<int> gr[n];
for (int i=0;i<n;i++) {
int lider=Find(i);
gr[lider].push_back(i);
}
for (int i=0;i<n;i++) {
if (!gr[i].size()) continue;
vector<int> roots;
for (int j=0;j<gr[i].size();j++) {
int x=gr[i][j];
for (int k=j+1;k<gr[i].size();k++) {
int y=gr[i][k];
if (paths[x][y]==1) Union2(x, y);
}
}
for (int j=0;j<gr[i].size();j++) {
int x=gr[i][j];
for (int k=j+1;k<gr[i].size();k++) {
int y=gr[i][k];
if (paths[x][y]==2&&Find2(x)==Find2(y)) return 0;
}
}
for (auto x:gr[i]) {
if (Find2(x)==x) roots.push_back(x);
else a[x][parent2[x]]=1, a[parent2[x]][x]=1;
}
if (roots.size()<=1) continue;
if (roots.size()==2) return 0;
int x=roots[0], y=roots[roots.size()-1];
for (int j=0;j<roots.size()-1;j++)
a[roots[j]][roots[j+1]]=1, a[roots[j+1]][roots[j]]=1;
a[x][y]=1, a[y][x]=1;
}
build(a);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:56:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for (int j=0;j<gr[i].size();j++) {
| ~^~~~~~~~~~~~~
supertrees.cpp:58:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for (int k=j+1;k<gr[i].size();k++) {
| ~^~~~~~~~~~~~~
supertrees.cpp:63:23: 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<gr[i].size();j++) {
| ~^~~~~~~~~~~~~
supertrees.cpp:65:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for (int k=j+1;k<gr[i].size();k++) {
| ~^~~~~~~~~~~~~
supertrees.cpp:77:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for (int j=0;j<roots.size()-1;j++)
| ~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
6 ms |
1192 KB |
Output is correct |
7 |
Correct |
122 ms |
24060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
6 ms |
1192 KB |
Output is correct |
7 |
Correct |
122 ms |
24060 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
440 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
5 ms |
1368 KB |
Output is correct |
13 |
Correct |
127 ms |
24156 KB |
Output is correct |
14 |
Incorrect |
0 ms |
348 KB |
Answer gives possible 1 while actual possible 0 |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
7 ms |
1372 KB |
Output is correct |
9 |
Correct |
113 ms |
23836 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
6 ms |
1368 KB |
Output is correct |
13 |
Correct |
122 ms |
24076 KB |
Output is correct |
14 |
Incorrect |
0 ms |
344 KB |
Answer gives possible 1 while actual possible 0 |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
30 ms |
6380 KB |
Output is correct |
5 |
Correct |
122 ms |
24144 KB |
Output is correct |
6 |
Correct |
115 ms |
24148 KB |
Output is correct |
7 |
Correct |
155 ms |
24148 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
31 ms |
6236 KB |
Output is correct |
10 |
Correct |
121 ms |
24400 KB |
Output is correct |
11 |
Correct |
115 ms |
24084 KB |
Output is correct |
12 |
Correct |
121 ms |
23968 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
436 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
31 ms |
6276 KB |
Output is correct |
17 |
Correct |
122 ms |
24148 KB |
Output is correct |
18 |
Correct |
121 ms |
24188 KB |
Output is correct |
19 |
Correct |
126 ms |
24148 KB |
Output is correct |
20 |
Correct |
113 ms |
24056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
6 ms |
1192 KB |
Output is correct |
7 |
Correct |
122 ms |
24060 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
440 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
5 ms |
1368 KB |
Output is correct |
13 |
Correct |
127 ms |
24156 KB |
Output is correct |
14 |
Incorrect |
0 ms |
348 KB |
Answer gives possible 1 while actual possible 0 |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
6 ms |
1192 KB |
Output is correct |
7 |
Correct |
122 ms |
24060 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
440 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
5 ms |
1368 KB |
Output is correct |
13 |
Correct |
127 ms |
24156 KB |
Output is correct |
14 |
Incorrect |
0 ms |
348 KB |
Answer gives possible 1 while actual possible 0 |
15 |
Halted |
0 ms |
0 KB |
- |