---
id: 88
title: "P1137:旅行计划题解"
slug: p1137
lang: zh
date: 2018-10-26T15:43:24
date_gmt: 2018-10-26T07:43:24
modified: 2023-02-04T03:29:09
status: publish
password_protected: false
category: { name: Olympiad in Informatics, slug: oi }
tags: [OI, DP, 树形DP, DAG, 拓扑排序]
original_url: https://kalorona.com/oi/p1137/
archived: 2026-11-27
editor: gutenberg
media: []
archive_notes:
  - "Body headings were <h1> in the original; demoted to level 2 so the page title is the only h1."
  - "The code block's Enlighter language was null; the content is C++, so it was tagged cpp."
comments:
  - id: 12
    parent: 0
    type: comment
    author: lornd
    url: ""
    owner: false
    date: 2018-11-01T18:32:28
    content: |-
      %%%,太优秀了
---

## 思路

好久没有写拓扑排序了。今天看到这题看了半天，由题解可知是拓扑序DP。原理根据拓扑序来进行DP。由于每个点在DAG是不会回路的，所以我们只需一头往下走就好，用dp[x]记录点x可到达的地方。使用拓扑排序就会让DP时不会出现影响其他已计算过的节点。

## 代码

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