Splitting workers between two work areas
Statement
A factory has two work areas. There are V workers, numbered from 1 to V. Every worker has to be assigned to one of the two work areas. How many workers go into a work area does not matter, and one of them may be left empty.
Some of the workers do not get along, so there are pairs that cannot be put in the same work area. M such pairs are given. Each pair holds for both sides, so if worker A and worker B form a pair, they cannot be in the same work area from A's side or from B's side. The pair is kept when the two are in different work areas.
Write a program that decides whether all V workers can be assigned to the two work areas without breaking a single one of the M pairs given. Information about several factories is given in order, so the decision has to be made for each factory.
For example, suppose there are V=3 workers and M=2 pairs that cannot be put together, as in the table below.
| Pair | 1 | 2 |
|---|---|---|
| The two who cannot be put together | 2 and 3 | 2 and 1 |
In this case the assignment below keeps both pairs.
| Work area | First | Second |
|---|---|---|
| Workers assigned | 2 | 1, 3 |
Worker 1 and worker 3 are not a pair that cannot be put together, so they may be in the same work area. The assignment is therefore possible in this case.
Now suppose instead that there are V=4 workers and M=4 pairs that cannot be put together, as in the table below.
| Pair | 1 | 2 | 3 | 4 |
|---|---|---|---|---|
| The two who cannot be put together | 2 and 4 | 2 and 3 | 4 and 3 | 1 and 4 |
Here workers 2, 3, and 4 form a pair that cannot be put together with each of the other two. There are only two work areas, so however the three are split up at least two of them land in the same work area, and those two break a pair. The assignment is therefore impossible in this case.
Input and Output Examples
Example 1
Input
1 3 2 2 3 2 1
Output
YES
Example 2
Input
1 4 4 2 4 2 3 4 3 1 4
Output
NO
Example 3
Input
2 3 2 1 3 2 3 4 4 1 2 2 3 3 4 4 2
Output
YES NO
Input
First the number of factories T is given as a natural number. T is a natural number from 1 to 5.
Then information about the T factories is given in order. The information for each factory starts with one line holding the number of workers V and the number of pairs that cannot be put together M, separated by a space. V is a natural number from 2 to 20,000, and M is a natural number from 1 to 200,000.
Then M lines follow, each holding the numbers of the two workers in a pair, separated by a space. Both numbers are natural numbers from 1 to V and differ from each other. The same pair is never given twice.
Output
For each factory print "YES" if every worker can be assigned to the two work areas and "NO" if they cannot, one per line, in order. Print both answers in capital letters.
Requirements
- Every worker has to be assigned to one of the two work areas
- One work area may be left empty, and the two work areas may hold different numbers of workers
- The two workers in a pair that cannot be put together have to be in different work areas
- Print "YES" or "NO" for each factory, one per line, in order
- You must write a program that reads from standard input and writes to standard output
- Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
- The languages you can use are Python and JavaScript
- The memory limit is 256MB. The browser does not measure it, it only states it
- The time limit is set by measuring the setter's reference solution on the same runtime
- On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
- The output has to match the expected value down to the character. A space inside a line counts the same as any other character.
- There are 15 cases, and 3 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
- You have to pass all 15 cases to solve this.
Examples
3 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.
1 3 2 2 3 2 1
YES
1 4 4 2 4 2 3 4 3 1 4
NO
2 3 2 1 3 2 3 4 4 1 2 2 3 3 4 4 2
YES NO
Solution
Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.
Submitting requires you to log in. Running against the public cases works without one.
You can test with 3 public cases. Submitting runs the hidden cases too.
People who solved this problem
The time shown is the time spent solving. Input reading time is outside the solver's control, so it is excluded. And since each device reports this value itself, we record which device measured it.
Log in and you can see the code once you solve this problem.