Bosses
Time limit1sMemory limit1024 MB
Given a graph of projects where the lower-numbered endpoint is the boss, find the max number of edges so every vertex has at most one boss, minimizing cancellations.
- Level
Medium4 of 10
- Topics
- Graph, Greedy, Implementation, Sorting
- Solved
- No attempts yet
Problem
Company “Ūbr” employs programmers. Each is assigned a code — an integer from to . All employees' codes are distinct.
The company runs projects. Each project is the joint responsibility of two programmers, and the one with the smaller code is the other's boss. It is guaranteed that no pair of employees works together on more than one project.

Figure 1. In this example and five projects are running. The second, third, and sixth programmers each have one boss, while the fourth programmer has two bosses.
“Ūbr” has found that programmers with more than one boss enjoy their work less. The company wants to reorganize so that every employee has at most one boss. It will do this by terminating some existing projects and starting new ones. In the example above, one valid reorganization is to cancel project 3–4 and create the new project 3–5, but that is not the only option.
Find a way for “Ūbr” to reorganize so that no programmer has two or more bosses. Among all such reorganizations, the company wants to run as many projects as possible after the change; and among those, it wants to cancel as few of the original projects as possible.
Input
The first line contains two integers — the number of programmers and the number of projects run before the reorganization. Each of the next lines describes one project with two distinct integers between and : the codes of the two programmers responsible for that project.
Output
Output three integers , , on a single line, separated by spaces:
- — the number of projects the company will run after the reorganization
- — the number of projects that will be terminated
- — the number of new projects that will be started
They must satisfy . Choose the reorganization that first maximizes , and then, among those, minimizes . Under these two conditions the values , , and are uniquely determined, so output exactly those three numbers.