답안 #545520

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
545520 2022-04-04T18:07:46 Z shark25361 Ball Machine (BOI13_ballmachine) C++14
47.6984 / 100
1000 ms 12808 KB
#include<bits/stdc++.h>
#include <iomanip>
using namespace std;

#define FIO ios_base::sync_with_stdio(false);cin.tie(0);
#define ll long long
#define for_t ll T;cin>>T;while(T--)
#define endl "\n"
#define mod 1000000007
#define inf 1000000000000000001
#define all(c) c.begin(),c.end()
#define pb push_back
#define lc(curr) (curr * 2)
#define rc(curr) ((curr * 2) + 1)

/*
    subtask 2: only elements whose parent does not have a ball can be removed
    update status of node with ball to false and ans = 0
*/

const int maxn = 100005;

vector<ll> adj[maxn];
ll parent[maxn];
vector<bool> hasball(maxn,false);
ll cnt;
ll root;
ll last;
ll minnode[maxn];//min node in the subtree of i

void roll(ll i)
{
    if(adj[i].size() == 0)
    {
        hasball[i] = true;
        cnt--;
        last = i;
        return;
    }
    for(auto it:adj[i])
    {
        if(cnt && !hasball[it])
        {
            roll(it);
        }
    }
    if(cnt && !hasball[i])
    {
        hasball[i] = true;
        cnt--;
        last = i;
    }
}

//find the contiguous nodes in ancestors of i that have a ball
void remnode(ll i)
{
    ll cnt = 0;
    ll curr = i;
    while(curr != root && hasball[parent[curr]])
    {
        curr = parent[curr];
        cnt++;
    }
    hasball[curr] = false;
    cout << cnt << endl;
}

bool comp(ll i,ll j)
{
    return minnode[i] < minnode[j];
}

void dfs(ll i)
{
    minnode[i] = i;
    for(auto it:adj[i])
    {
        dfs(it);
        minnode[i] = min(minnode[i],minnode[it]);
    }
}

void sol()
{
    ll n,q;
    cin >> n >> q;
    for(int i = 1;i <= n;i++)
    {
        cin >> parent[i];
        if(parent[i] == 0)
        {
            root = i;
        }
        else
        {
            adj[parent[i]].pb(i);
        }
    }
    dfs(root);
    for(int i = 1;i <= n;i++)
    {
        sort(all(adj[i]),comp);
    }
    /*
    cnt = 5;
    roll(1);
    for(int i = 1;i <= n;i++)
    {
        if(hasball[i])
        {
            cout << i << " has a ball." << endl;
        }
    }
    */
    for(int i = 0;i < q;i++)
    {
        ll x,y;
        cin >> x >> y;
        if(x == 1)
        {
            cnt = y;
            roll(root);
            cout << last << endl;
        }
        else
        {
            remnode(y);
        }
    }
}

int main()
{
    FIO
    sol();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 60 ms 5984 KB Output is correct
3 Correct 37 ms 6088 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 4 ms 2832 KB Output is correct
10 Correct 10 ms 3440 KB Output is correct
11 Correct 38 ms 6044 KB Output is correct
12 Correct 43 ms 6088 KB Output is correct
13 Correct 43 ms 6000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1086 ms 4756 KB Time limit exceeded
2 Correct 57 ms 10112 KB Output is correct
3 Execution timed out 1074 ms 7028 KB Time limit exceeded
4 Correct 30 ms 5452 KB Output is correct
5 Correct 29 ms 5380 KB Output is correct
6 Correct 48 ms 5312 KB Output is correct
7 Correct 26 ms 4844 KB Output is correct
8 Execution timed out 1093 ms 4772 KB Time limit exceeded
9 Correct 87 ms 10608 KB Output is correct
10 Correct 76 ms 10156 KB Output is correct
11 Correct 507 ms 10200 KB Output is correct
12 Correct 84 ms 8936 KB Output is correct
13 Execution timed out 1079 ms 11688 KB Time limit exceeded
14 Execution timed out 1080 ms 6956 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1088 ms 6604 KB Time limit exceeded
2 Execution timed out 1088 ms 8532 KB Time limit exceeded
3 Execution timed out 1080 ms 10952 KB Time limit exceeded
4 Execution timed out 1070 ms 8680 KB Time limit exceeded
5 Execution timed out 1083 ms 8380 KB Time limit exceeded
6 Execution timed out 1073 ms 8376 KB Time limit exceeded
7 Execution timed out 1042 ms 7616 KB Time limit exceeded
8 Execution timed out 1078 ms 10944 KB Time limit exceeded
9 Execution timed out 1088 ms 10092 KB Time limit exceeded
10 Execution timed out 1066 ms 9552 KB Time limit exceeded
11 Execution timed out 1085 ms 9548 KB Time limit exceeded
12 Execution timed out 1090 ms 8332 KB Time limit exceeded
13 Execution timed out 1092 ms 12808 KB Time limit exceeded
14 Correct 48 ms 7324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1089 ms 10028 KB Time limit exceeded
2 Execution timed out 1083 ms 8384 KB Time limit exceeded
3 Execution timed out 1083 ms 12748 KB Time limit exceeded
4 Execution timed out 1076 ms 9996 KB Time limit exceeded
5 Execution timed out 1093 ms 10188 KB Time limit exceeded
6 Correct 834 ms 10308 KB Output is correct
7 Execution timed out 1092 ms 8500 KB Time limit exceeded
8 Execution timed out 1092 ms 12700 KB Time limit exceeded
9 Execution timed out 1077 ms 7016 KB Time limit exceeded