#P2597. [ZJOI2012] 灾难
[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 vertices, representing species, numbered from to .
- If species can eat species , then draw a directed edge from to .
- 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 . However, if the grass suddenly went extinct, then all species on the grassland would be affected, so the disaster value of grass is .
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 in the food web.
From the -nd to the -st line, each line contains several distinct integers. The -st line lists integers indicating that species can eat species . Each line ends with an integer marking the end of that line.
Output Format
Output lines, one integer per line. On the -th line, output the disaster value of species .
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 of the testdata, it is guaranteed that .
- For of the testdata, it is guaranteed that , , the input file size does not exceed MB, and the graph has no cycles.
Translated by ChatGPT 5
京公网安备 11011102002149号