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 n;
int N=1e3+9;
vector<int> rnk(N,0),par(N),rnk1(N,0),par1(N);
int ulp(int node){
if(par[node]==node) return node;
return par[node]=ulp(par[node]);
}
void spajaj(int a,int b){
a=ulp(a);b=ulp(b);
if(a==b) return;
if(rnk[b]>rnk[a])
swap(a,b);
par[b]=a;
if(rnk[a]==rnk[b])
rnk[a]++;
}
int ulp1(int node){
if(par1[node]==node) return node;
return par1[node]=ulp1(par1[node]);
}
void spajaj1(int a,int b){
a=ulp1(a);b=ulp1(b);
if(a==b) return;
if(rnk1[b]>rnk1[a])
swap(a,b);
par1[b]=a;
if(rnk1[a]==rnk1[b])
rnk1[a]++;
}
int construct(std::vector<std::vector<int>> p){
n=p.size();
std::vector<std::vector<int>> answer;
for (int i = 0; i < n; i++) {
std::vector<int> row;
row.resize(n);
answer.push_back(row);
}
vector<set<int>> lanci(n);
for(int i=0; i<n; i++) par[i]=i;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(p[i][j]==1)
spajaj(i,j);
if(p[i][j]==3) return 0;
}
}
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(p[i][j]==1){
lanci[ulp(i)].insert(j);
lanci[ulp(i)].insert(i);
}
if(ulp(i)==ulp(j)&&p[i][j]!=1)
return 0;
}
}
for(int i=0; i<n; i++){
if(lanci[i].size()<=1) continue;
for(auto it=next(lanci[i].begin()); it!=lanci[i].end(); it++){
answer[*prev(it)][*it]=1;
answer[*it][*prev(it)]=1;
}
}
for(int i=0; i<n; i++) par1[ulp(i)]=ulp(i);
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(p[i][j]==2)
spajaj1(ulp(i),ulp(j));
}
}
vector<set<int>> adj(n);
for(int i=0; i<n; i++)
adj[ulp1(ulp(i))].insert(ulp(i));
for(int i=0; i<n; i++){
if(adj[i].size()<=1) continue;
if(adj[i].size()==2) return 0;
for(auto it=next(adj[i].begin()); it!=adj[i].end(); it++){
answer[*prev(it)][*it]=1;
answer[*it][*prev(it)]=1;
}
answer[*prev(adj[i].end())][*adj[i].begin()]=1;
answer[*adj[i].begin()][*prev(adj[i].end())]=1;
}
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(p[i][j]==0){
if(ulp(i)==ulp(j)) return 0;
if(ulp1(ulp(i))==ulp1(ulp(j))) return 0;
}
if(p[i][j]==1)
if(ulp(i)!=ulp(j)) return 0;
if(p[i][j]==2){
if(ulp(i)==ulp(j)) return 0;
if(ulp1(ulp(i))!=ulp1(ulp(j))) return 0;
}
}
}
build(answer);
return 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... |