Submission #567649

#TimeUsernameProblemLanguageResultExecution timeMemory
567649pere_gilConnecting Supertrees (IOI20_supertrees)C++17
100 / 100
227 ms22524 KiB
#include "supertrees.h" #include "bits/stdc++.h" using namespace std; #define vvi vector<vector<int> > struct union_find{ vector<int> p; void init(int n){ p.resize(n); iota(p.begin(),p.end(),0); } int find_set(int n){ return (n==p[n]) ? n : p[n]=find_set(p[n]); } bool same_set(int a, int b){ return find_set(a)==find_set(b); } void union_set(int a, int b){ if(!same_set(a,b)) p[find_set(a)]=find_set(b); } }; /* 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); } */ 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; } bool join_2(vector<int> &g, vvi &res){ if(g.size()==1) return true; if(g.size()<3) return false; 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; } return true; } bool make_group(int n, vector<int> &g, vvi &v, vvi &res){ //vector<int> p(n); //init(n,p); union_find p; p.init(n); 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) p.union_set(g[i],g[j]); 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[p.find_set(g[i])].push_back(g[i]); if(g[i]==p.find_set(g[i])) mid.insert(g[i]); } vector<int> v_mid(mid.begin(),mid.end()); for(int i=0;i<n;i++) if(one[i].size()) join_1(one[i],res); return join_2(v_mid,res); } int construct(vvi v){ int n=v.size(); //vector<int> p(n); //init(n,p); union_find p; p.init(n); for(int i=0;i<n;i++) for(int j=0;j<n;j++) if(v[i][j]) p.union_set(i,j); vvi g(n); for(int i=0;i<n;i++) g[p.find_set(i)].push_back(i); for(int i=0;i<n;i++) for(int j=0;j<n;j++) if((p.same_set(i,j) && v[i][j]==0) || v[i][j]==3) return 0; vvi res(n,vector<int> (n,0)); for(int i=0;i<n;i++) if(g[i].size()>0) if(!make_group(n,g[i],v,res)) return 0; build(res); return 1; }

Compilation message (stderr)

supertrees.cpp: In function 'void join_1(std::vector<int>&, std::vector<std::vector<int> >&)':
supertrees.cpp:40:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |  for(int i=1;i<g.size();i++)
      |              ~^~~~~~~~~
supertrees.cpp: In function 'bool join_2(std::vector<int>&, std::vector<std::vector<int> >&)':
supertrees.cpp:46:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |  for(int i=0;i<g.size();i++){
      |              ~^~~~~~~~~
supertrees.cpp: In function 'bool make_group(int, std::vector<int>&, std::vector<std::vector<int> >&, std::vector<std::vector<int> >&)':
supertrees.cpp:59:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |  for(int i=0;i<g.size();i++){
      |              ~^~~~~~~~~
supertrees.cpp:61:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |   for(int j=0;j<g.size();j++)
      |               ~^~~~~~~~~
supertrees.cpp:64:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |   if(aux==g.size()-1) mid.insert(g[i]);
      |      ~~~^~~~~~~~~~~~
supertrees.cpp:67:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |  for(int i=0;i<g.size();i++){
      |              ~^~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...