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];
int Find(int x) {
if (x==parent[x])
return x;
return parent[x]=Find(parent[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;
}
int construct(vector<vector<int> > paths) {
int n=paths.size();
vector<vector<int> > a=paths;
for (int i=0;i<n;i++)
sz[i]=1, parent[i]=i;
for (int i=0;i<n;i++)
for (int j=0;j<n;j++)
a[i][j]=0;
bool vtor_subtask=true;
for (int i=0;i<n;i++) {
for (int j=i+1;j<n;j++) {
if (paths[i][j]) Union(i, j);
if (paths[i][j]==2) vtor_subtask=false;
}
}
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()) {
for (auto x:gr[i])
for (auto y:gr[i])
if (!paths[x][y])
return 0;
if (vtor_subtask) {
int x=gr[i][0];
for (auto y:gr[i])
if (x!=y) a[x][y]=1, a[y][x]=1;
}
else {
if (gr[i].size()==2) return 0;
if (gr[i].size()==1) continue;
int x=gr[i][0], y=gr[i][gr[i].size()-1];
for (int j=0;j<gr[i].size()-1;j++)
a[gr[i][j]][gr[i][j+1]]=1, a[gr[i][j+1]][gr[i][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:58:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for (int j=0;j<gr[i].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... |