Submission #546871

#TimeUsernameProblemLanguageResultExecution timeMemory
546871uroskConnecting Supertrees (IOI20_supertrees)C++14
0 / 100
1 ms340 KiB
#include "supertrees.h"
#include "bits/stdc++.h"
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define ld double
#define ll long long
#define ull unsigned long long
#define llinf 100000000000000000LL // 10^17
#define iinf 2000000000 // 2*10^9
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) ((ll)(a.size()))
#define all(a) a.begin(),a.end()
#define rall(a) a.begin(),a.end(),greater<int>()
#define getunique(v) {sort(all(v)); v.erase(unique(all(v)), v.end());}
#define pi 3.14159265358979323846
#define here cerr<<"=============================================================================\n"
#define ceri(a,l,r) {for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
#define ceri2(a,l,r,n,m) {for(ll i = l;i<=r;i++){for(ll j = n;j<=m;j++) cerr<<a[i][j]<< " ";cerr<<endl;}}
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define daj_mi_malo_vremena ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
using namespace std;
#define maxn 1005
ll dsu[maxn];
ll root(ll x){
    while(x!=dsu[x]){
        dsu[x] = dsu[dsu[x]];
        x = dsu[x];
    }
    return x;
}
void upd(ll x,ll y){dsu[root(x)] = root(y);}
bool get(ll x,ll y){return root(x)==root(y);}
vector<ll> v[maxn];
vector<std::vector<int>> answer;
bool cyc[maxn];
void upd2(ll x,ll y){
    answer[x][y] = 1;
    answer[y][x] = 1;
    cyc[y] = 1;
    cyc[x] = 1;
}
bool bio[maxn];
bool vis[maxn];
vector<ll> g[maxn];
ll d[maxn][maxn];
void dfs(ll poc,ll u,ll dis,bool c){
    vis[u] = 1;
    for(ll s : g[u]){
        if(vis[s]) continue;
        if(cyc[u]==0&&cyc[s]==1) d[poc][s] =1 ;
        else if(c||dis==0) d[poc][s] = 2;
        else d[poc][s] = 1;
        dfs(poc,s,dis+1,c|cyc[s]);
    }
}
int construct(std::vector<std::vector<int>> p) {

	int n = p.size();
	for(ll i = 0;i<n;i++){
        vector<int> w(n);
        answer.pb(w);
	}
	iota(dsu,dsu+n,0);
	for(ll i = 0;i<n;i++){
        for(ll j = 0;j<n;j++){
            if(p[i][j]==3) return 0;
        }
        for(ll j = 0;j<n;j++){
            if(p[i][j]==1) upd(i,j);
        }
	}
	vector<vector<ll> >w;
    for(ll i = 0;i<n;i++) v[root(i)].pb(i);
    for(ll i = 0;i<n;i++) if(sz(v[i])) w.pb(v[i]);
    for(ll i = 0;i<sz(w);i++){
        for(ll j = 0;j<sz(w[i])-1;j++){
            answer[w[i][j]][w[i][j+1]] = 1;
            answer[w[i][j+1]][w[i][j]] = 1;
        }
    }
    /*here;
    for(vector<ll> v : w){
        for(ll x : v) cerr<<x<< " ";
        cerr<<endl;
    }
    here;
    for(vector<int> v : answer){
        for(ll x : v) cerr<<x<< " ";
        cerr<<endl;
    }
    here;*/
    for(ll i = 0;i<sz(w);i++){
        ll last = i;
        for(ll j = i+1;j<sz(w);j++){
            if(bio[j]) continue;
            if(p[root(w[last].back())][root(w[j].back())]>0){
                upd2(w[last].back(),w[j].back());
                last = j;
                bio[j] = 1;
            }
        }
        if(last!=i) upd2(w[last].back(),w[i].back());
    }
    for(ll i = 0;i<n;i++){
        for(ll j = i+1;j<n;j++){
            if(answer[i][j]){
                //cerr<<i<< " "<<j<<endl;
                g[i].pb(j);
                g[j].pb(i);
            }
        }
    }
    fill(vis,vis+n,0);
    for(ll i = 0;i<n;i++){
        d[i][i] = 1;
        fill(vis,vis+n+1,0);
        dfs(i,i,0,cyc[i]);
    }
    for(ll i = 0;i<n;i++){
        for(ll j = 0;j<n;j++){
            d[i][j] = min(d[i][j],d[j][i]);
            //cerr<<d[i][j]<< " ";
        }
        //cerr<<endl;
    }
    for(ll i = 0;i<n;i++){
        for(ll j = 0;j<n;j++){
            if(d[i][j]!=p[i][j]) return 0;
        }
    }
    build(answer);
	return 1;
}
/*
4
1 1 2 2
1 1 2 2
2 2 1 2
2 2 2 1
*/
#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...