DFS + Cycles + Cactus
Hockey in Ural
To promote hockey and to improve the skill of the Ural hockey teams, the Ural tournament was organised. n hockey teams from the cities of the Urals were invited to participate in the tournament.
After the first two rounds, where in each round each team played one match, it turned out that there were too many teams. The organisers of the tournament decided to allow only k teams to continue participation, no two of which have met in the first two rounds.
Write a program that finds a set of k teams that satisfies the conditions, or prints a message that this cannot be done. If there are several solutions, print any of them.
The first line contains number n (2 ≤ n ≤
105, n is even). Next n lines give the description of all passed matches. The description of each match consists of two positive integers not greater than n - the numbers of the teams that played in the match. The first n / 2 of them correspond to the matches of the first round, the rest correspond to the matches of the second round.
The last line contains one integer k (2 ≤ k ≤ n).
It is guaranteed that each team played exactly two matches: one in the first round and one in the second.
Print either a single number 0 if there is no solution, or k different numbers - the numbers of selected teams.
6 1 2 3 5 4 6 2 3 4 5 1 6 3
1 4 3
4 1 2 3 4 2 1 4 3 3