Submission #774009

#TimeUsernameProblemLanguageResultExecution timeMemory
774009Jarif_RahmanSplit the Attractions (IOI19_split)C++17
0 / 100
71 ms21152 KiB
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;

int n, m;
vector<int> s, so;
vector<vector<int>> graph;
vector<vector<int>> dfs_tree;
vector<bool> visited;
vector<int> sz;

void dfs(int nd, int mom){
    visited[nd] = 1;
    for(int x: graph[nd]) if(x != mom && !visited[x]) dfs(x, nd), sz[nd]+=sz[x];
    if(mom != -1) dfs_tree[mom].pb(nd);
}

vector<int> ans;
int cnt;
void create_ans(int nd, int forbidden, int id){
    if(cnt == 0) return;
    ans[nd] = id;
    cnt--;
    for(int x: dfs_tree[nd]) if(x != forbidden) create_ans(x, forbidden, id);
}

bool found = 0;
void dfs2(int nd){
    if(found) return;

    if(s[0] <= sz[nd] && sz[1] <= n-sz[nd]){
        found = 1;
        ans.assign(n, so[2]);
        cnt = s[1];
        create_ans(0, nd, so[1]);
        cnt = s[0];
        create_ans(nd, -1, so[0]);
        return;
    }
    if(s[1] <= sz[nd] && sz[0] <= n-sz[nd]){
        found = 1;
        ans.assign(n, so[2]);
        cnt = s[0];
        create_ans(0, nd, so[0]);
        cnt = s[1];
        create_ans(nd, -1, so[1]);
        return;
    }    

    for(int x: dfs_tree[nd]) dfs2(x);
}

vector<int> find_split(int _n, int a, int b, int c, vector<int> U, vector<int> V){
    s = {a, b, c}, so = {1, 2, 3};
    sort(so.begin(), so.end(), [&](int i, int j){
        return s[i-1] < s[j-1];
    });
    sort(s.begin(), s.end());

    swap(n, _n);
    m = U.size();

    graph.resize(n);
    dfs_tree.resize(n);
    visited.assign(n, 0);
    sz.assign(n, 1);
    for(int i = 0; i < m; i++){
        graph[U[i]].pb(V[i]);
        graph[V[i]].pb(U[i]);
    }

    dfs(0, -1);
    dfs2(0);

    if(found) return ans;
    else return vector<int>(n, 0);
}
#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...