#P1137. 旅行计划

    ID: 137 远端评测题 1000ms 125MiB 尝试: 1 已通过: 0 难度: 5 上传者: 标签>动态规划,dp图论递推排序拓扑排序

旅行计划

Description

Xiao Ming is going to travel to a country. This country has NN cities, numbered 11 to NN, and there are MM roads connecting them. Xiao Ming plans to start from some city and only move east until he stops at city ii.

Therefore, for each city ii, he needs to choose a starting city and plan a route ending at city ii, such that along the route, except for the first city, every city lies to the east of the previous city. Under this condition, he also wants to visit as many cities as possible.

You only know the relative east–west positions of the two cities connected by each road, but you do not know the exact positions of all cities. Now, for every ii, you need to plan a route for Xiao Ming and compute the maximum number of cities that can be visited on a route ending at city ii.

Input Format

The first line contains two positive integers N,MN, M.

The next MM lines each contain two positive integers x,yx, y, indicating there is a road connecting city xx and city yy, and it is guaranteed that city xx is to the west of city yy.

Output Format

Output NN lines. The ii-th line contains a positive integer, the maximum number of cities that can be visited on a route ending at city ii.

5 6
1 2
1 3
2 3
2 4
3 4
2 5

1
2
3
4
3

Hint

Choosing to always start from city 11 yields the answers.

Constraints:

  • For 20%20\% of the testdata, 1N1001 \le N \le 100.
  • For 60%60\% of the testdata, 1N10001 \le N \le 1000.
  • For 100%100\% of the testdata, 1N1000001 \le N \le 100000, 1M2000001 \le M \le 200000.

Translated by ChatGPT 5