# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1108736 | 2024-11-04T21:33:20 Z | SteveBro | Connecting Supertrees (IOI20_supertrees) | C++17 | 0 ms | 0 KB |
#include <bits/stdc++.h> using namespace std; int n,b[1001][1001]; int sef[1001]; int ef(int x) { if(sef[x]==x) return x; else return sef[x]=ef(sef[x]); } vector <int> comp[1001],rez; bool ok[1001]; int construct(int p[][1001]) { int i,j,nr; for(i=0;i<n;i++) sef[i]=i; for(i=0;i<n;i++) { for(j=0,nr=0;j<n;j++) { if(p[i][j]==3) return 0; if(p[i][j]==0&&ef(i)==ef(j)) return 0; if(p[i][j]>0) sef[ef(i)]=ef(j); if(p[i][j]==2) nr++; if(p[i][j]==1&&i!=j) ok[i]=true; } if(nr==1) return 0; } for(i=0;i<n;i++) comp[ef(i)].push_back(i); for(i=0;i<n;i++) sef[i]=-1; for(i=0;i<n;i++) { if(!comp[i].empty()) { rez.clear(); for(auto x : comp[i]) { if(!ok[x])/// se alfa pe ciclu rez.push_back(x); else { if(sef[x]==-1)///nou lant { rez.push_back(x); sef[x]=x; for(j=0;j<n;j++) { if(p[x][j]==1&&x!=j) { if(sef[j]==-1) { sef[j]=x; b[x][j]=b[j][x]=1; } else return 0; } } } else { for(j=0;j<n;j++) { if(p[x][j]==2&&sef[x]==sef[j]) return 0; if(p[x][j]==1&&sef[x]!=sef[j]) return 0; } } } } if(rez.size()>2) { for(j=1;j<rez.size();j++) { b[rez[j-1]][rez[j]]=b[rez[j]][rez[j-1]]=1; } b[rez[0]][rez[j-1]]=b[rez[j-1]][rez[0]]=1; } } } build(b); return 1; }