#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]) 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 (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:54:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for (int j=0;j<gr[i].size();j++) {
| ~^~~~~~~~~~~~~
supertrees.cpp:56:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for (int k=j+1;k<gr[i].size();k++) {
| ~^~~~~~~~~~~~~
supertrees.cpp:68:23: 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=0;j<roots.size()-1;j++)
| ~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
436 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
8 ms |
1372 KB |
Output is correct |
7 |
Correct |
123 ms |
23948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
436 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
8 ms |
1372 KB |
Output is correct |
7 |
Correct |
123 ms |
23948 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 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 |
1372 KB |
Output is correct |
13 |
Correct |
113 ms |
23960 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 |
344 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 |
5 ms |
1200 KB |
Output is correct |
9 |
Correct |
114 ms |
24060 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
7 ms |
1372 KB |
Output is correct |
13 |
Correct |
117 ms |
24144 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 |
29 ms |
6176 KB |
Output is correct |
5 |
Correct |
117 ms |
24104 KB |
Output is correct |
6 |
Correct |
118 ms |
24148 KB |
Output is correct |
7 |
Correct |
121 ms |
24144 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
41 ms |
6220 KB |
Output is correct |
10 |
Correct |
122 ms |
24148 KB |
Output is correct |
11 |
Correct |
114 ms |
24056 KB |
Output is correct |
12 |
Correct |
121 ms |
24148 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
30 ms |
6380 KB |
Output is correct |
17 |
Correct |
114 ms |
24144 KB |
Output is correct |
18 |
Correct |
118 ms |
24148 KB |
Output is correct |
19 |
Correct |
116 ms |
23952 KB |
Output is correct |
20 |
Correct |
119 ms |
23952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
436 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
8 ms |
1372 KB |
Output is correct |
7 |
Correct |
123 ms |
23948 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 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 |
1372 KB |
Output is correct |
13 |
Correct |
113 ms |
23960 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 |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
436 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
8 ms |
1372 KB |
Output is correct |
7 |
Correct |
123 ms |
23948 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 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 |
1372 KB |
Output is correct |
13 |
Correct |
113 ms |
23960 KB |
Output is correct |
14 |
Incorrect |
0 ms |
344 KB |
Answer gives possible 1 while actual possible 0 |
15 |
Halted |
0 ms |
0 KB |
- |