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;
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 (stderr)
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++)
| ~^~~~~~~~~~~~~~~
# | 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... |