#P2597. Food Web Cascade Disaster Value
Food Web Cascade Disaster Value
Food Web Cascade Disaster Value
In a directed acyclic food web representing biological relationships, each species is numbered from 1 to \(n\). An edge exists from species \(y\) to species \(x\) if and only if species \(x\) can eat species \(y\). A species that does not eat any other species (i.e. its food list is empty) is considered a producer and survives through photosynthesis, while the others are consumers who must eat at least one food source to survive.
If a species suddenly goes extinct, then any consumer that loses all of its food sources will also go extinct. This cascade continues until no additional extinctions occur. The disaster value of a species is defined as the number of species (excluding itself) that eventually become extinct due to its initial extinction.
Your task is, given the food web, to compute the disaster value for every species.
inputFormat
The first line contains two integers \(n\) and \(m\), representing the number of species and the number of relationships, respectively.
Each of the next \(m\) lines contains two integers \(x\) and \(y\), indicating that species \(x\) can eat species \(y\). This implies a directed edge from \(y\) to \(x\) in the food web.
outputFormat
Output \(n\) integers separated by a space. The \(i\)-th integer represents the disaster value of species \(i\), i.e. the number of other species that will become extinct when species \(i\) is removed.
sample
5 4
2 1
3 2
4 1
5 4
4 1 0 1 0