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;
class DSU {
vector<int> par;
vector<int> siz;
public:
DSU(int n) {
par.resize(n);
iota(par.begin(),par.end(),0);
siz.assign(n,1);
}
int find_set(int u) {
if (par[u]==u) return u;
return par[u]=find_set(par[u]);
}
bool is_same(int u, int v) {
return find_set(u)==find_set(v);
}
bool unite_set(int u, int v) {
u=find_set(u);
v=find_set(v);
if (u==v) return false;
if (siz[u]<siz[v]) swap(u,v);
par[v]=u;
siz[u]+=siz[v];
return true;
}
};
int construct(vector<vector<int>> p) {
int n = p.size();
vector<vector<int>> ans(n,vector<int>(n,0));
DSU dsu=DSU(n);
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
if (i==j) continue;
if (p[i][j]==2) {
dsu.unite_set(i,j);
}
}
}
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
if (p[i][j]==0&&dsu.is_same(i,j)) {
return 0;
}
}
}
unordered_map<int,vector<int>> m;
for (int i=0; i<n; i++) {
m[dsu.find_set(i)].push_back(i);
}
for (auto i: m) {
vector<int> v=i.second;
if (v.size()==1||v.size()==0) continue;
if (v.size()==2) return 0;
ans[v.front()][v.back()]=1;
ans[v.back()][v.front()]=1;
for (int j=0; j<v.size()-1; j++) {
ans[v[j]][v[j+1]]=1;
ans[v[j+1]][v[j]]=1;
}
}
build(ans);
return 1;
}
Compilation message (stderr)
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:65:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for (int j=0; j<v.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... |