我觉得也没毛病呀
#include<bits/stdc++.h>
using namespace std;
int n,book[10005],val[10005],maxn[15000];
int first[100005],nextt[100005],u[100005],v[100005];
struct node{
int tep;
int cur;
}que[200002];
char ch;
int read(){
int s = 0,w = 1;
ch = getchar();
while(ch < '0' || ch > '9'){
if(ch == '-') w = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9'){
s = s * 10 + ch - '0';
ch = getchar();
}
return s * w;
}
int main()
{
int p = 0,tot = 0,head = 1,tail = 1;
memset(first,-1,sizeof first);
cin >> n;
for(int i = 1; i <= n; i++)
{
p = read();
val[p] = read();
p = read();
if(p == 0)
{
book[i] = 1;
que[tail].cur = i;
que[tail].tep = 1;
tail++;
}
while(p != 0)
{
tot++;
u[tot] = p;
v[tot] = i;
nextt[tot] = first[u[tot]];
first[u[tot]] = tot;
p = read();
}
}
for( ; head < tail; head++)
{
maxn[que[head].tep] = max(maxn[que[head].tep],val[que[head].cur]);
for(int k = first[que[head].cur]; k != -1; k = nextt[k])
{
if(book[v[k]]) continue;
book[v[k]] = 1;
que[tail].cur = v[k];
que[tail].tep = que[head].tep + 1;
tail++;
}
}
int sum = 0;
for(int i = 1; i <= n + 100 ; i++)
{
sum += maxn[i];
}
cout << sum << endl;
return 0;
}