[模拟赛]删树游戏(game)
前言:
T1咋这么难啊?
题面描述:

解题思路:
注意到一个点的权值,能作为答案,当且仅当他的祖先都被取走了,所以我们在一个点取走后,将他的所有儿子节点加入优先队列,每次取最小值即可。
代码实现:
#include<bits/stdc++.h>
#define endl '\n'
#define pii pair<int, int>
using namespace std;
const int N = 3e5 + 10;
int n, a[N];
vector<int> v[N];
int main(){
// freopen("example/game6.in", "r", stdin);
freopen("game.in", "r", stdin);
freopen("game.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 2, fa; i <= n; i++) cin >> fa, v[fa].push_back(i);
for(int i = 1; i <= n; i++) cin >> a[i];
priority_queue<pii, vector<pii>, greater<pii> > q;
q.push({a[1], 1});
while(!q.empty()){
int val = q.top().first, x = q.top().second;
q.pop();
cout << val << endl;
for(int y : v[x]) q.push({a[y], y});
}
return 0;
}

浙公网安备 33010602011771号