#include <bits/stdc++.h>
#define CLOSE ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define endl "\n"
typedef long long LL;
const int N = 1e4 + 5, M = 2e6 + 100, mod = 1e9 + 7;
using namespace std;
int h[N], e[M], ne[M], idx, din[N], dp[N], a[N];
int n, m;
void add(int x, int y){
e[idx] = y, ne[idx] = h[x], h[x] = idx ++;
}
void topSort(){
queue<int> q;
for(int i = 1; i <= n; i ++){
if(din[i] == 0){
q.push(i);
dp[i] = a[i];
}
}
while(!q.empty()){
int t = q.front();
q.pop();
for(int i = h[t]; i != -1; i = ne[i]){
int j = e[i];
din[j] --;
dp[j] = max(dp[j], dp[t] + a[j]);
if(!din[j]){
q.push(j);
}
}
}
}
int main()
{
memset(h, -1, sizeof h);
cin >> n;
for(int i = 1; i <= n; i ++){
int x, w;
cin >> x >> w;
a[x] = w;
int y;
while(cin >> y && y){
add(x, y);
din[y] ++;
}
}
topSort();
int ans = 0;
for(int i = 1; i <= n; i ++){
ans = max(ans, dp[i]);
}
cout << ans;
return 0;
}
标签:idx,int,杂物,long,ne,add,topSort
From: https://www.cnblogs.com/acwhr/p/18006423