---
id: 88
lang: en
translation_of: index.md
source_sha256: aeb2185da96da0680e645b40e9445fb44653ef8860869b8976cd4b3901f1475a
title: "P1137: Travel Plan — Solution"
description: "From the solutions, it is a DP over topological order: do the DP following the topological order, with dp[x] recording the places vertex x can reach."
tags: ["OI","DP","Tree DP","DAG","Topological Sort"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "旅行计划 (the Luogu problem name) rendered as “Travel Plan”."
---

## Main Idea

I haven't written a topological sort in ages. Today I looked at this problem for ages, and from the solutions it turns out to be a DP over topological order. The principle is to do the DP following the topological order. Since no vertex in a DAG can loop back, we only need to head straight down, using dp[x] to record the places vertex x can reach. Using a topological sort means that, during the DP, nothing ends up affecting other nodes that have already been computed.

## Code

```cpp
// P1137.cpp
#include <iostream>
#include <cstring>
#include <queue>

using namespace std;

const int maxn = 100010;
int points[maxn];
int to[2 * maxn];
int next[2 * maxn];
int indeg[maxn];
int res[maxn];
int dp[maxn];
int tot = 0;
int current = 0;
int n, m;

void add_edge(int a, int b)
{
    to[current] = b;
    next[current] = points[a];
    points[a] = current++;
}

void toposort()
{
    queue<int> waiting;
    for (int i = 1; i <= n; i++)
        if (indeg[i] == 0)
            waiting.push(i), res[tot++] = i;
    while (!waiting.empty())
    {
        int curt = waiting.front();
        waiting.pop();
        for (int edge = points[curt]; edge != -1; edge = next[edge])
        {
            indeg[to[edge]]--;
            if (indeg[to[edge]] == 0)
                waiting.push(to[edge]), res[tot++] = to[edge];
        }
    }
}

void DP()
{
    for (int i = 1; i <= n; i++)
        dp[i] = 1;
    for (int i = 0; i < n; i++)
        for (int edge = points[res[i]]; edge != -1; edge = next[edge])
            dp[to[edge]] = max(dp[to[edge]], dp[res[i]] + 1);
}

int main()
{
    memset(points, -1, sizeof(points));
    memset(to, -1, sizeof(to));
    memset(indeg, 0, sizeof(indeg));
    cin >> n >> m;
    int a, b;
    for (int i = 0; i < m; i++)
    {
        cin >> a >> b, add_edge(a, b);
        indeg[b] += 1;
    }
    toposort();
    DP();
    for (int i = 1; i <= n; i++)
        cout << dp[i] << endl;
    return 0;
}
```
