Love among People
All submissions for this problem are available.
There are n people in a group. Some of them love each other. You are given m such relationships. Each relationship is represented by two integers u, v, denoting that person u and v love each other.
One day the people went to a magician asking about an issue bothering them for long time. The magician said "It is not a good omen if two persons love each other. There is no joy in the love if you get reciprocated for your love. So from now on, you guys have to make sure that whoever loves each other among you should decide the person who will be loved and who will love.".
Formally, he asked each pair of persons u, v which used to love each other to decide among themselves whether u will love v or v will love u, but both of them can't love each other.
Truly scared and saddened by the magician's suggestion, people decided that they will change their relationships according to the suggestion. Soon they realized that by following the suggestion blindly it might happen that a person loves a lot of other people and in turn very few people love him, which will make a person really sad :(. Sometimes it might also happen a lot of persons love a person, but that person loves too less persons, in that case also, he will feel sad too. In particular, sadness of a person will be equal to absolute difference of the number of persons that he loves and number of persons that love him.
Now they want people to be as less sad as possible. Formally, they want to minimize the maximum value of sadness over all the persons in the group. Can you please help them in this daunting task? Formally, you have to tell for each loving pair u, v, which way the love should be?
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
First line of the each test case contains two space separated integer n, m denoting the number of persons and number of their love relationships.
Each of the next m lines contains two space separated integers u, v denoting that persons u and v love each other.
For each test case, output m + 1 lines.
First line should contain a single integer the minimum value of maximum sadness over all persons that they can achieve.
In the next m lines, i-th line should contain two space separated integers u, v denoting that person u loves person v. If v loves person u, then print v, u.
Please note that there can be more than one possible ways of changing the relationships. In those situations, you are allowed to print any of them
- 1 ≤ T ≤ 106
- 1 ≤ N ≤ 105
- Sum of N's' over all the test cases in a single file won't exceed 106
Subtask #1 (30 points)
- You just have to tell the minimum value of maximum sadness factor. You don't require to print one such way of changing relationships. Note that if you want to solve this subtask only, the first line of your output should be "SMALL_SUBTASK" (without quotes). After that, for each test case, you should just print the first line corresponding the minimum of maximum sadness.
Subtask #2 (70 points)
- For solving this subtask, your should print first line as "BOTH_SUBTASKS" (without quotes). The output format for this case will be similar to that is written in output section.
Input 2 2 1 1 2 3 2 1 2 2 3 Output: For solving both the subtasks. BOTH_SUBTASKS 1 1 2 1 1 2 3 1 For solving only a single subtask SMALL_SUBTASK 1 1
Example case 1. There are two persons. Person 1 and 2 love each other. Now, according to magician's suggestion, if person 1 loves person 2, then sadness of both the persons will be equal to 1. Same is the case with person 2 loving person 1. Hence answer is 1.
Example case 2. There are three persons. Person 1 and 2 love each other. Person 2 and 3 love each other. Now, according to magician's suggestion, if person 1 loves person 2 and person 3 loves 1. Then, sadness of all three persons will be equal to 1. This is the best that they can do. For example, if the person 1 loves 2 and person 1 loves 3 too, then sadness of first person will be 2, which will be worse than previous case. Hence answer is 1.
|Time Limit:||5 sec|
|Source Limit:||50000 Bytes|
|Languages:||C, CPP14, JAVA, PYTH, PYTH 3.5, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, PERL6, TEXT, SCM chicken, CLOJ, FS|
Fetching successful submissions
If you are still having problems, see a sample solution here.