제출 #654425

#제출 시각아이디문제언어결과실행 시간메모리
654425jiahngSplit the Attractions (IOI19_split)C++14
0 / 100
119 ms27124 KiB
#include "split.h"
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> pi;
typedef vector <int> vi;
typedef vector <pi> vpi;
typedef pair<pi, ll> pii;
typedef set <ll> si;
typedef long double ld;
#define f first
#define s second
//#define mp make_pair
#define FOR(i,s,e) for(int i=s;i<=int(e);++i)
#define DEC(i,s,e) for(int i=s;i>=int(e);--i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i,x) for (auto i: x)
#define mem(x,i) memset(x,i,sizeof x)
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define maxn 200010
#define INF (ll)1e9
#define MOD 1000000007
typedef pair <vi, int> pvi;
typedef pair <int,pi> ipi;
typedef vector <pii> vpii;

int deg[maxn];
int par[maxn];
int depth[maxn], sz[maxn];
pi low[maxn];
bool vis[maxn];
vi adj[maxn],child[maxn];
int N;
void dfs(int x){
    vis[x] = 1;
    sz[x] = 1;
    low[x] = pi(x,x);
    aFOR(i, adj[x]){
        if (vis[i]){
            if (depth[i] < depth[low[x].f]) low[x] = pi(i,x);
        }else{
            par[i] = x;
            child[x].pb(i);
            depth[i] = depth[x] + 1; dfs(i);
            sz[x] += sz[i];
            if (depth[low[x].f] > depth[low[i].f]) low[x] = low[i]; 
        }
    }
}
vi ans;
void setst(int x, int v){
    ans[x] = v;
    aFOR(i, child[x]) setst(i,v);
}
int mp[4];
vector<int> find_split(int n, int a, int b, int c, vector<int> p, vector<int> q) {
    N = n;
    ans = vi(N,0);
    FOR(i,0,p.size()-1){
        adj[p[i]].pb(q[i]); adj[q[i]].pb(p[i]);
    }
    dfs(0);
    int tmpa = min({a,b,c}); int tmpc = max({a,b,c}); int tmpb = N - tmpa - tmpc;
    
    mp[1] = min({pi(a,1), pi(b,2), pi(c,3)}).s;
    mp[3] = max({pi(a,1), pi(b,2), pi(c,3)}).s;
    mp[2] = 6-mp[1]-mp[3];
    
    swap(a, tmpa); swap(c, tmpc); swap(b, tmpb);

    bool Adone = 0;
    FOR(i,0,N-1) if (sz[i] >= a && sz[i] <= a + c){
        Adone = 1; setst(i,1); break;
    }
    if (!Adone){
        FOR(i,0,N-1) if (sz[i] >= a + c){
            int sm = 0; bool no = 0;
            aFOR(j, child[i]){
                if (sz[j] > a + c){
                    no = 1; break;
                }
                if (depth[low[j].f] < depth[i]) sm += sz[j];
            }
            if (no) continue;
            if (sz[i] - sm <= a + c){
                int cur = sz[i];
                setst(i,1);            
                aFOR(j, child[i]) if (depth[low[j].f] < depth[i] && cur > a + c){
                    par[low[j].s] = low[j].f;
                    cur -= sz[j]; setst(j, 0);
                }
                Adone = 1; break;
            }
        }
    }
    if (!Adone) return ans;
    vi vA,vB;

    int nA = 0, nB = 0;
    FOR(i,0,N-1){
        if (ans[i] == 1) nA++;
        else nB++;
    }

    FOR(i,0,N-1) if (ans[i] == 0) ans[i] = 2;
    FOR(i,1,N-1) deg[par[i]]++;
    FOR(i,0,N-1) if (deg[i] == 0){
        if (ans[i] == 1) vA.pb(i);
        else vB.pb(i);
    }
    while (nA > a){
        int x = vA.back(); vA.pop_back(); nA--; ans[x] = 3;
        deg[par[x]]--; if (deg[par[x]] == 0) vA.pb(par[x]);
    }
    while (nB > b){
        int x = vB.back(); vB.pop_back(); nB--; ans[x] = 3;
        deg[par[x]]--; if (deg[par[x]] == 0) vB.pb(par[x]);
    }
    //cout << b << ' ' << nB << '\n';

    FOR(i,0,N-1) ans[i] = mp[ans[i]];
	return ans;
}
#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...