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;
#define vvi vector<vector<int> >
void pri_graph(vvi &v){
for(int i=0;i<v.size();i++)
for(int j=i;j<v.size();j++)
if(v[i][j]) printf("%d <-> %d\n",i,j);
printf("\n");
}
void init(int n, vector<int> &p){
for(int i=0;i<n;i++) p[i]=i;
}
int find_set(int n, vector<int> &p){
return (n==p[n]) ? n : p[n]=find_set(p[n],p);
}
bool same_set(int a, int b, vector<int> &p){
return find_set(a,p)==find_set(b,p);
}
void union_set(int a, int b, vector<int> &p){
if(!same_set(a,b,p)) p[find_set(a,p)]=find_set(b,p);
}
vector<int> to_vector(unordered_set<int> &s){
unordered_set<int> :: iterator it;
vector<int> v;
for(it=s.begin();it!=s.end();it++)
v.push_back(*it);
return v;
}
void join_1(vector<int> &g, vvi &res){
for(int i=1;i<g.size();i++)
res[g[i-1]][g[i]]=res[g[i]][g[i-1]]=1;
}
void join_2(vector<int> &g, vvi &res){
if(g.size()<3) return;
for(int i=0;i<g.size();i++){
int j=(i+1)%g.size();
res[g[i]][g[j]]=res[g[j]][g[i]]=1;
}
}
void make_group(int n, vector<int> &g, vvi &v, vvi &res){
vector<int> p(n);
init(n,p);
unordered_set<int> mid;
for(int i=0;i<g.size();i++){
int aux=0;
for(int j=0;j<g.size();j++)
if(v[g[i]][g[j]]==1) union_set(g[i],g[j],p);
else aux++;
if(aux==g.size()-1) mid.insert(g[i]);
}
vvi one(n);
for(int i=0;i<g.size();i++){
if(mid.find(g[i])==mid.end())
one[find_set(g[i],p)].push_back(g[i]);
if(g[i]==find_set(g[i],p)) mid.insert(g[i]);
}
vector<int> v_mid=to_vector(mid);
/*
printf("The middle: ");
for(int i=0;i<v_mid.size();i++) printf("%d ",v_mid[i]);
printf("\nGroup of one\n");
for(int i=0;i<n;i++){
if(!one[i].size()) continue;
for(int j=0;j<one[i].size();j++) printf("%d ",one[i][j]);
printf("\n");
}
*/
join_2(v_mid,res);
for(int i=0;i<n;i++)
if(one[i].size()) join_1(one[i],res);
}
int construct(vvi v){
int n=v.size();
vector<int> p(n);
init(n,p);
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(v[i][j]) union_set(i,j,p);
vvi g(n);
for(int i=0;i<n;i++)
g[find_set(i,p)].push_back(i);
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(same_set(i,j,p) && v[i][j]==0) return 0;
vvi res(n,vector<int> (n,0));
for(int i=0;i<n;i++)
if(g[i].size()>0)
make_group(n,g[i],v,res);
//pri_graph(res);
build(res);
return 1;
}
Compilation message (stderr)
supertrees.cpp: In function 'void pri_graph(std::vector<std::vector<int> >&)':
supertrees.cpp:8:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
8 | for(int i=0;i<v.size();i++)
| ~^~~~~~~~~
supertrees.cpp:9:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
9 | for(int j=i;j<v.size();j++)
| ~^~~~~~~~~
supertrees.cpp: In function 'void join_1(std::vector<int>&, std::vector<std::vector<int> >&)':
supertrees.cpp:39:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int i=1;i<g.size();i++)
| ~^~~~~~~~~
supertrees.cpp: In function 'void join_2(std::vector<int>&, std::vector<std::vector<int> >&)':
supertrees.cpp:45:15: 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<g.size();i++){
| ~^~~~~~~~~
supertrees.cpp: In function 'void make_group(int, std::vector<int>&, std::vector<std::vector<int> >&, std::vector<std::vector<int> >&)':
supertrees.cpp:56:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for(int i=0;i<g.size();i++){
| ~^~~~~~~~~
supertrees.cpp:58:16: 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<g.size();j++)
| ~^~~~~~~~~
supertrees.cpp:61:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | if(aux==g.size()-1) mid.insert(g[i]);
| ~~~^~~~~~~~~~~~
supertrees.cpp:64:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for(int i=0;i<g.size();i++){
| ~^~~~~~~~~
# | 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... |