Team Them Up
Time limit1sMemory limit128 MB
Split N people into two teams where teammates must mutually know each other, minimizing the size difference, and report the two team sizes.
- Level
Medium7 of 10
- Topics
- Graph, DFS, Dynamic programming, Brute force
- Solved
- No attempts yet
Problem
Divide a group of people into exactly two teams so that:
- everyone belongs to exactly one team;
- each team has at least one member;
- inside a team, every person knows every other member of that team;
- the two teams are as close in size as possible.
Acquaintance is not necessarily mutual: person may know person while does not know . Two people may be placed on the same team only if they know each other in both directions.
If it is impossible to split everyone into two such teams, report that no valid division exists.
Input
The people are numbered with distinct integers from to .
The first line contains one integer () — the number of people. Each of the next lines describes one person in increasing order of their number. The -th of these lines lists the distinct numbers (, ) of the people that person knows, separated by spaces and terminated by a single .
Output
If no valid division exists, print a single line containing No solution.
Otherwise the closest-possible split has a unique pair of team sizes. Print these two sizes on one line separated by a space: first the size of the smaller team, then the size of the larger team (if the two teams are equal, print that size twice).