显然是一道遍历树再加上判断权值的题目,只要注意只能往上走即可(回溯不算往下走)
#include <bits/stdc++.h> using namespace std; const int MAXN=2e4; int n,q; int fa[MAXN],a[MAXN]; vector<int> adj[MAXN]; int Burn(int u,int k) { int res=0; for(int v:adj[u]) if(a[v]<=k) res+=a[v]+Burn(v,k); return res; } int main() { ios::sync_with_stdio(false); cin>>n>>q; for(int i=2;i<=n;i++) cin>>fa[i]>>a[i],adj[fa[i]].push_back(i); while(q--) { int u,k; cin>>u>>k; cout<<Burn(u,k)<<endl; } return 0; }
标签:cout,int,fa,火花,MAXN,adj From: https://www.cnblogs.com/wjk53233/p/17259787.html