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;
struct DSU{
int n; vector<int> fa, siz;
vector<vector<int>> comps;
void init(int _n){
n = _n;
fa.resize(n); siz.assign(n, 1);
iota(fa.begin(), fa.end(), 0);
comps.resize(n);
for(int i = 0; i<n; ++i)
comps[i].push_back(i);
}
int find(int v){return v==fa[v]?v:fa[v]=find(fa[v]);}
void unite(int u, int v){
u = find(u), v =find(v);
if(u==v) return;
if(siz[u]>siz[v]) swap(u, v);
for(auto x : comps[u])
comps[v].push_back(x);
comps[u].clear(); fa[u] = v;
}
};
int construct(vector<vector<int>> paths){
int n = paths.size();
DSU d1; d1.init(n);
for(int i = 0; i<n; ++i)
for(int j = i+1; j<n; ++j)
if(paths[i][j]) d1.unite(i, j);
vector<vector<int>> res(n, vector<int>(n, 0));
for(auto c : d1.comps){
if(c.empty()) continue;
int m = c.size();
for(int i = 0; i<m; ++i)
for(int j = i+1; j<m; ++j)
if(!paths[c[i]][c[j]]) return 0;
DSU d2; d2.init(m);
for(int i = 0; i<m; ++i)
for(int j = i+1; j<m; ++j)
if(paths[c[i]][c[j]]==1) d2.unite(i, j);
for(auto x : d2.comps)
for(int i = 0; i<x.size(); ++i)
for(int j = i+1; j<x.size(); ++j)
if(paths[c[x[i]]][c[x[j]]]!=1) return 0;
vector<int> good;
for(auto x : d2.comps){
if(!x.size()) continue;
good.push_back(c[x[0]]);
for(int i = 0; i<(int)x.size()-1; ++i)
res[c[x[i]]][c[x[i+1]]] = res[c[x[i+1]]][c[x[i]]] = 1;
}
if((int)good.size()==1) continue;
if((int)good.size()==2) return 0;
good.push_back(good[0]);
for(int i = 0; i<(int)good.size()-1; ++i)
res[good[i]][good[i+1]] = res[good[i+1]][good[i]] = 1;
}
build(res); return 1;
}
Compilation message (stderr)
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:45:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int i = 0; i<x.size(); ++i)
| ~^~~~~~~~~
supertrees.cpp:46:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | for(int j = i+1; j<x.size(); ++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... |