Rank
InterviewTime limit2sMemory limit1024 MB
Count the players that lie on a directed win cycle built from the game results.
Problem
A tournament has players and games. Each game is played by two different players. A player may play any number of games and does not have to meet every opponent. A player may play no game at all. There is no draw, so every game has one winner and one loser.
Once all the games are over, the players are ranked. Ranking can fail for several reasons, but this problem deals only with cycles of wins. For example, if A beats B, B beats C, and C in turn beats A, the relative ranking of these three players cannot be determined.
Stated precisely, suppose there are distinct players () such that beat , beat , and so on until beat . The ranking of those players cannot be determined because of the cycle. The case happens when two players met twice and each of them won once. One player may belong to several cycles at the same time, and such a player is counted once.
Only players caught in a cycle are counted. A player whose ranking is undetermined for a different reason, for example a player who played no game, is not counted.
Given the list of games and their results, write a program that finds how many players have an undetermined ranking because of a cycle.
Input
The first line contains the number of players and the number of games , separated by a space. (, ) The players are numbered through .
Each of the next lines contains the result of one game as four integers , , , . Here and are the numbers of the two players, and and are the scores of player and player . Every score is a non-negative integer smaller than , and the player with the larger score wins.
Output
Print the number of players whose ranking cannot be determined because of a cycle.