All submissions for this problem are available.
Read problems statements in Mandarin Chinese and Russian.
A galaxy is a set of planets with some connections between pairs of planets. Connections between planets are mutual and no planet can be connected with itself. The distance between a pair of planets is the least number of connections that you have to pass through while travelling from the first planet to the second.
Now let's consider how good galaxies are formed.
Initially there is a galaxy that consists of exactly one planet. At any moment, the galaxy grows by first making a copy of itself; then connecting each planet in the first copy to the corresponding planet in the second copy. Thus a galaxy which has P planets and M connections would grow into a new galaxy with 2P planets and 2M+P connections.
This diagram shows how the galaxy grows over the first two iterations. Dark grey shows the copy of the galaxy; light grey shows the additional connections.
However, galaxies do not continue growing forever. Why? The answer is simple: comets. At any point after the galaxy has started growing, a comet may pass through the galaxy. When it does, it changes the connections between planets by the following rule:
Two planets are connected after the comet arrives if and only if the distance between the planets was equal to 2 before the comet arrives.
It can be shown that a comet will split a growing galaxy into two disconnected galaxies with identical structures.
After a comet has arrived, the galaxy stops growing. Researchers have discovered that each of the two identical galaxies formed by the comet are 'good galaxies' - fit for living. In fact, galaxies formed in this way are the only good galaxies that exist.
This diagram shows the result when a comet arrives after each of the first three iterations. In each one the red and blue colours show the two equivalent good galaxies.
Unfortunately, nothing lasts forever. For many reasons, such as a second comet appearing, new planets or connections between them may be created or destroyed, resulting in galaxies with many other structures and causing the galaxy to lose its 'good' status.
You are given a galaxy with N planets and M connections. Your task is to determine whether this galaxy is a good galaxy - ie can be created by a comet passing through a growing galaxy.
The first line of the input contains one integer T denoting the number of test cases.
The first line of each test case contains two integers N and M - the number of planets in the galaxy and the number of connections. All planets are enumerated from 1 to N.
The following M lines each contain a pair of different integers - two planets which are connected in the galaxy. It's guaranteed that there won't be multiple connections between any pair of planets.
For each test case output one word per line: "YES" if the galaxy is a good galaxy, or "NO" if it is not.
- 1 ≤ T ≤ 5
- 2 ≤ N ≤ 1024
- 0 ≤ M ≤ 40 000
Subtask #1 [22 points]: N ≤ 32
Subtask #2 [78 points]: maximum constraints
Input: 5 2 1 1 2 2 0 3 3 1 2 2 3 1 3 4 6 1 2 1 3 1 4 2 3 2 4 3 4 4 5 1 2 1 3 1 4 2 3 2 4 Output: YES NO NO YES NO
Test case 1. This galaxy is equivalent to the red (or blue) galaxy in step 2 in the above image.
Test case 2. This galaxy is similar to test case 1, but the connection has been destroyed, so the galaxy is no longer good.
Test case 3. There are no good galaxies with 3 planets; some planets must have been destroyed.
|Tags||dec14, graph, hard, pavel1996|
|Time Limit:||2.5 sec|
|Source Limit:||50000 Bytes|
|Languages:||C, CPP14, JAVA, PYTH, PYTH 3.6, 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, PYP3, CLOJ, FS|
Fetching successful submissions
If you are still having problems, see a sample solution here.