1. 程式人生 > >(搜尋/拓撲排序)雜務

(搜尋/拓撲排序)雜務

(記憶化搜尋/拓撲排序)雜務

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));
}