Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef struct
- {
- int parent,value,choice,no,outdegree;
- }vertex;
- void sol()
- {
- int N=0,tmp=0;
- scanf("%d",&N);
- vertex v[N+1];
- for(size_t i=0; i<=N; i++)
- v[i].outdegree = v[i].no = 0;
- scanf("%d",&v[1].value);
- v[1].choice = v[1].value; //the initial value of choice
- for(size_t i=2; i<=N; i++)
- {
- scanf("%d%d",&v[i].parent,&v[i].value);
- v[i].choice = v[i].value;
- v[v[i].parent].outdegree++;
- }
- queue<int> tree;
- for(size_t i=2; i<=N; i++)
- {
- if(v[i].outdegree == 0)
- tree.push(i);
- }
- while(!tree.empty())
- {
- tmp = tree.front();
- tree.pop();
- v[v[tmp].parent].choice += v[tmp].no;
- v[v[tmp].parent].no += max(v[tmp].choice,v[tmp].no);
- v[v[tmp].parent].outdegree--;
- if(v[v[tmp].parent].outdegree == 0 && v[tmp].parent != 1)
- tree.push(v[tmp].parent);
- }
- printf("%d\n",max(v[1].choice,v[1].no));
- }
- int main()
- {
- int T;
- scanf("%d",&T);
- while(T--)
- sol();
- return 0;
- }
Add Comment
Please, Sign In to add comment