Company
Time limit1sMemory limit128 MB
Keep the fewest boss relations from a DAG so that reachability between all pairs of employees is unchanged, and output them sorted.
- Level
Medium7 of 10
- Topics
- Graph, Topological sort, Greedy
- Solved
- No attempts yet
Problem
At the Plumsoft company there is a hierarchy among employees: some employees are the boss of others. Person is in charge of person if there is a chain of employees such that is 's boss, is 's boss, , and is 's boss. Plumsoft is a very well-run company, so you may assume that no two employees are each in charge of the other (the hierarchy has no cycles).
Management wants to cut the cost of meetings, so they plan to keep only some of the existing direct " is the boss of " relations while preserving every " is in charge of " relation. Help them keep as few direct "boss" relations as possible without changing who is in charge of whom.
Input
The first line contains two integers and separated by a space (, ), where is the number of employees and is the number of direct "boss" relations. Employees are labeled through . Each of the next lines contains two labels and separated by a space, meaning is the boss of .
Output
On the first line, print a single integer — the minimum number of direct "boss" relations that must be kept so that every "in charge of" relation is preserved. Then print those relations, one per line, as two labels and separated by a space (meaning is still the boss of ). Print the relations in ascending order, sorted first by and then by .