(记忆化搜索/拓扑排序)杂务
https://www.luogu.org/problemnew/show/P1113
每一次去找子节点的最耗时间的任务,记录最大耗时即可。
const int maxn = 1e5 + 10;
int ti[maxn];
struct edge {
int p, nxt;
} E[maxn];
int head[maxn], tot, vis[maxn], mem[maxn];
void addEdge(int u, int v) {
E[++tot] = (edge){v, head[u]};
head[u] = tot;
}
inline int T(int u) {
if (vis[u]) return mem[u];
if (head[u] == 0) return ti[u];
int maxtime = 0;
for (register int x = head[u]; x; x = E[x].nxt)
maxtime = max(maxtime, T(E[x].p));
vis[u] = 1;
return mem[u] = ti[u] + maxtime;
}
int main()
{
int n = read();
for (register int i = 0; i < n; i++) {
int id = read();
ti[id] = read();
int pre = read();
while(pre != 0) {
addEdge(pre, id);
pre = read();
}
}
printf("%d\n", T(1));
}