Dominos
InterviewTime limit1sMemory limit256 MB
Given a directed graph of domino toppling relations, find the minimum number of blocks to push by hand so that all blocks fall.
- Level
Medium4 of 10
- Topics
- Graph, DFS, Implementation
- Solved
- No attempts yet
Problem
Dominos are fun. If you stand domino blocks in a long line and topple one of them, that block topples the next, which topples the one after it, and so on, so that the whole line falls in a chain reaction. Sometimes, however, the blocks are arranged so that one block does not topple another; in that case the next block must be toppled by hand.
Given the arrangement of the domino blocks (which blocks topple which when they fall), find the minimum number of blocks you must topple by hand so that every block falls.
Input
The first line contains the number of test cases.
The first line of each test case contains two integers and , both at most . is the number of domino blocks and is the number of relations. The blocks are numbered with integers from to . Each of the next lines contains two integers and , meaning that if block falls, block also falls.
Output
For each test case, output a single integer on its own line: the minimum number of domino blocks that must be toppled by hand.