Parliament Party Split
Time limit1sMemory limit64 MB
Simulate the greedy process that repeatedly moves the smallest-numbered member with at least three same-party quarrel partners to the other party.
- Level
Medium6 of 10
- Topics
- Simulation, Graph, Heap
- Solved
- No attempts yet
Problem
A parliament in a faraway country has members. From Monday to Friday of this week every member came to work and argued all day about the amendment to the new referendum law.
A reporter photographed the chamber once a day for those five days, each time in the middle of the argument. One photograph holds the pairs of members quarreling face to face. You analyze all five photographs.
Every member belongs to one of two parties, written A and B. Decide the party of each member so that no member quarreled with more than two members of their own party.
The same pair can appear in the photographs of several days. Such a pair still counts as one quarreling partner for each of the two members.
Input
The first line contains the number of members (). Members are numbered from 1 to .
The next five lines describe the photographs of Monday to Friday in that order. Each line lists the pairs of quarreling members in that day's photograph. First comes the number of pairs (), then pairs in the form " ", where and are the numbers of the two members quarreling with each other. Two spaces come before each pair. A member appears at most once per line.
Output
Print one line with a string of length made of the characters A and B. The th character is the party of member .
Many assignments satisfy the condition, so only the assignment this procedure produces is accepted.
- Put every member in party A.
- If some member quarreled with three or more members of their own party, take the smallest numbered such member and move that member to the other party.
- Repeat step 2 until no such member is left.
A member appears at most once per photograph, so a member has at most five quarreling partners. A member who moves therefore has at most two quarreling partners in the new party right after the move, and the number of pairs that quarreled inside one party drops with every move. The procedure ends.