제출 #160487

#제출 시각아이디문제언어결과실행 시간메모리
160487tushar_2658Ball Machine (BOI13_ballmachine)C++14
77.44 / 100
1090 ms33912 KiB
#include "bits/stdc++.h"
using namespace std;

const int maxn = 100005;
int par[maxn], root;
vector<int> edges[maxn]; 
int depth[maxn], dp[maxn], n, m;

void dfs(int x, int p){
    depth[x] = depth[p] + 1;
    dp[x] = x;
    vector<pair<int, int>> all;
    for(auto i : edges[x]){
        if(i == p)continue; 
        dfs(i, x);
        all.push_back({dp[i], i});
        dp[x] = min(dp[x], dp[i]);
    }
    sort(all.begin(), all.end()); 
    edges[x].clear();
    for(auto i : all){
        edges[x].push_back(i.second);
    }
    edges[x].push_back(p);
}   

set<pair<int, int>> st;
int ed[maxn], cnt = 0;

void dfs1(int x, int p){
    for(auto i : edges[x]){
        if(i == p)continue;
        dfs1(i, x);
    }
    ed[x] = ++cnt;
    st.insert({cnt, x});
}

int sparse[maxn][22];

void build(){
    for(int i = 1; i <= n; i++){
        sparse[i][0] = par[i];
    }
    for(int j = 1; j < 20; j++){
        for(int i = 1; i <= n; i++){
            if(sparse[i][j - 1] != 0){
                sparse[i][j] = sparse[sparse[i][j - 1]][j - 1];
            }
        }
    }
}

bool status[maxn];

int first_empty(int x){
    for(int i = 20; i >= 0; i--){
        if(sparse[x][i] != 0 && status[sparse[x][i]] != 0){
            x = sparse[x][i];
        }
    }
    return x;
}

int main(int argc, char const *argv[])
{
//    freopen("in.txt", "r", stdin);
    scanf("%d %d", &n, &m);
    for(int i = 1; i <= n; i++){
        int x; 
        scanf("%d", &x);
        if(x != 0){
            edges[i].push_back(x); 
            edges[x].push_back(i);
            par[i] = x;
        }else {
            root = i;
        }
    }
    dfs(root, -1);
    dfs1(root, -1);
    build();
    while(m--){
        int t, x;
        scanf("%d %d", &t, &x); 
        if(t == 1){
            pair<int, int> last;
            for(int i = 0; i < x; i++){
                last = *st.begin();
                st.erase(st.begin());
                status[last.second] = 1;
            }
            printf("%d\n", last.second);
        }else {
            int ff = first_empty(x);
            status[ff] = 0;
            st.insert({ed[x], x});
            printf("%d\n", depth[x] - depth[ff]);
        }
    }

    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

ballmachine.cpp: In function 'int main(int, const char**)':
ballmachine.cpp:68:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &n, &m);
     ~~~~~^~~~~~~~~~~~~~~~~
ballmachine.cpp:71:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &x);
         ~~~~~^~~~~~~~~~
ballmachine.cpp:85:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", &t, &x); 
         ~~~~~^~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...