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;
const int N = 1000 + 5;
struct DSU{
int par[N], type[N];
set<int> nodes[N];
void reset(int n){
for(int i = 0; i < n; i++) par[i] = i, type[i] = 0, nodes[i].insert(i);
}
int setfind(int a){
if(par[a] == a) return a;
else return par[a] = setfind(par[a]);
}
void setunion(int a, int b){
a = setfind(a), b = setfind(b);
if(a != b){
if(nodes[b].size() > nodes[a].size()) swap(a, b);
par[b] = par[a];
type[b] = -1;
for(auto x : nodes[b]) nodes[a].insert(x);
nodes[b].clear();
}
}
};
DSU dsu;
int construct(vector<vector<int>> graph) {
int n = graph.size();
vector ans(n, vector(n, 0));
dsu.reset(n);
// finding 2 cycles
set<int> curnodes;
curnodes.clear();
for(int i = 0; i < n; i++) curnodes.insert(i);
for(int v = 0; v < n; v++){
if(curnodes.count(v) && dsu.type[dsu.setfind(v)] == 0){
curnodes.erase(v);
for(auto u : curnodes){
if(graph[v][u] == 2){
bool flag = true;
for(auto w : dsu.nodes[dsu.setfind(v)]){
if(graph[u][w] != 2) flag = false;
}
if(flag){
dsu.type[dsu.setfind(v)] = dsu.type[dsu.setfind(u)] = 2;
dsu.setunion(v, u);
}
}
}
for(auto u : dsu.nodes[dsu.setfind(v)]) curnodes.erase(u);
}
else curnodes.erase(v);
}
for(int i = 0; i < n; i++){
if(dsu.type[i] == 2){
if(dsu.nodes[i].size() <= 2) return 0;
vector<int> tmp;
for(auto v : dsu.nodes[i]) tmp.push_back(v);
for(int i = 0; i + 1 < tmp.size(); i++) ans[tmp[i]][tmp[i + 1]] = ans[tmp[i + 1]][tmp[i]] = 1;
ans[tmp.back()][tmp.front()] = ans[tmp.front()][tmp.back()] = 1;
}
}
for(int v = 0; v < n; v++){
for(int u = 0; u < n; u++){
if(dsu.setfind(v) != dsu.setfind(u) && graph[v][u] != 0) return 0;
}
}
build(ans);
return 1;
}
Compilation message (stderr)
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:82:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | for(int i = 0; i + 1 < tmp.size(); i++) ans[tmp[i]][tmp[i + 1]] = ans[tmp[i + 1]][tmp[i]] = 1;
| ~~~~~~^~~~~~~~~~~~
# | 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... |