#P2597. [ZJOI2012] 灾难

    ID: 1610 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2012倍增各省省选浙江O2优化拓扑排序最近公共祖先,LCA

[ZJOI2012] 灾难

Description

Let’s look at this problem from a more professional perspective. We use a directed graph called a food web to describe the relationships between species:

  • A food web has nn vertices, representing nn species, numbered from 11 to nn.
  • If species xx can eat species yy, then draw a directed edge from yy to xx.
  • The graph has no cycles.
  • Some vertices have no outgoing edges; these are producers that can survive via photosynthesis.
  • Vertices with outgoing edges are consumers; they must survive by eating other species.
  • If all the foods of a consumer go extinct, it will go extinct as well.

We define the “disaster value” of a species in the food web as the number of species that would go extinct along with it if it suddenly went extinct.

For example: on a grassland, the relationships between species are as follows

If Xiaoqiang and Amiba scare all the sheep to death, then the wolves will go extinct due to lack of food, while Xiaoqiang and Amiba can survive by eating cows, and cows can survive by eating grass. Thus, the disaster value of sheep is 11. However, if the grass suddenly went extinct, then all 55 species on the grassland would be affected, so the disaster value of grass is 44.

Given a food web, you are required to compute the disaster value for each species.

Input Format

The first line contains an integer representing the number of vertices nn in the food web.

From the 22-nd to the (n+1)(n + 1)-st line, each line contains several distinct integers. The (i+1)(i + 1)-st line lists integers ai,ja_{i, j} indicating that species ii can eat species ai,ja_{i, j}. Each line ends with an integer 00 marking the end of that line.

Output Format

Output nn lines, one integer per line. On the ii-th line, output the disaster value of species ii.

5
0
1 0
1 0
2 3 0
2 0

4
1
0
0
0

Hint

Sample 1 Explanation

The sample input describes the example in the problem statement.

Constraints

  • For 50%50\% of the testdata, it is guaranteed that n104n \leq 10^4.
  • For 100%100\% of the testdata, it is guaranteed that 1n655341 \leq n \leq 65534, 1ai,jn1 \leq a_{i, j} \leq n, the input file size does not exceed 11 MB, and the graph has no cycles.

Translated by ChatGPT 5