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>
#define all(x) x.begin(),x.end()
#define sz(x) ll(x.size())
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;
const ll MAX=1e6;
int f[MAX];
vector<int> adj[MAX];
set<ll> s, c;
int fiend(int u){
if(f[u] == u)return u;
return f[u]=fiend(f[u]);
}
void unir(int u,int v){
int x=fiend(u);
int y=fiend(v);
if(x!=y){
f[x]=y;//x en y
}
return;
}
int construct(vector<vector<int>> p) {
int n = p.size();
for(int i=0; i<n; i++){
f[i]=i;
}
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(j==i){
if(p[i][j]!=0)return 0;
}
if(p[i][j]==1){
unir(i,j);
}
if(p[i][j]!=p[j][i])return 0;
}
}
vector<vector<int>> gfo(n,vector<int>(n));
for(int i=0; i<n; i++){
fiend(f[i]);
fiend(i);
if(f[i]!=i)adj[f[i]].pb(i);// aqui estan cada subarbol de 1 o mas
s.insert(f[i]);//representante de cada grupo
}
for(auto u:s){//armamos subarboles
for(auto v:adj[u]){
gfo[v][u]=1;
gfo[u][v]=1;
}
}
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if((!p[i][j] && i!=j) || p[i][j]==2){
if(f[i]==f[j]) return 0;
}
if(p[i][j]==1){
if(f[i]!=f[j]) return 0;
}
}
}
// para los ciclos
for(int i=0; i<n; i++){
adj[i].clear();
for(int j=0; j<n; j++){
if(p[i][j]==2){
unir(f[i],f[j]);
}
}
}
for(auto u:s){
fiend(f[u]);
fiend(u);
if(f[u]!=u)adj[f[u]].pb(u);
c.insert(f[u]);
}
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(!p[i][j] && i!=j){
if(f[i]==f[j]) return 0;
}
}
}
//cout<<sz(c)<<'\n';
for(auto x:c){// armamos ciclos
//cout<<x<<' ';
adj[x].pb(x);
for(int i=0; i<sz(adj[x]); i++){
int v=adj[x][i%sz(adj[x])];
int u=adj[x][(i+1)%sz(adj[x])];
if(v!=u){
gfo[v][u]=1;
gfo[u][v]=1;
}
}
}
build(gfo);
return 1;
}
/* con fe
9
0 1 1 1 2 2 2 2 2
1 0 1 1 2 2 2 2 2
1 1 0 1 2 2 2 2 2
1 1 1 0 2 2 2 2 2
2 2 2 2 0 2 2 2 2
2 2 2 2 2 0 2 1 2
2 2 2 2 2 2 0 2 1
2 2 2 2 2 1 2 0 2
2 2 2 2 2 2 1 2 0
*/
# | 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... |