problems / 150

Two Teams for the Picnic

medium graphs dfs 1000 ms · 256 MB

Two Teams for the Picnic

Alice and Bob are organising a picnic for their n friends, numbered from 1 to n. The friends will play games all afternoon, so Alice and Bob want to split everyone into two teams: Alice's team (team 1) and Bob's team (team 2). Unfortunately, some friends are rivals, and two rivals placed on the same team will spend the whole picnic arguing instead of playing.

You are given m rivalries. Each rivalry is a pair of different friends u and v, and it means that u and v must be on different teams. Rivalries are symmetric, and the same pair may be listed more than once. Friends who are not involved in any rivalry can go to either team.

Call a split valid if every friend is on exactly one of the two teams and no rivalry pair shares a team. A valid split is rarely unique, so Alice and Bob agree on a tie-breaking rule. Say that two friends are linked if one can get from one to the other through a chain of rivalries (every friend is linked to themselves). Within each group of mutually linked friends, the friend with the smallest number always joins Alice's team.

If a valid split exists, this rule determines it completely. Print it, or report that no valid split exists.

Input

The first line contains two integers n and m (1 ≤ n ≤ 2·10^5, 0 ≤ m ≤ 2·10^5): the number of friends and the number of rivalries.

Each of the next m lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v): a rivalry between friends u and v.

Output

If no valid split exists, print the single word IMPOSSIBLE.

Otherwise print n integers t_1, t_2, ..., t_n separated by spaces, where t_i is 1 if friend i joins Alice's team and 2 if they join Bob's team, following the rule above.

Notes

In the first example, friends 1, 2, 3, 4 are linked, and friend 1 goes to Alice's team. Then 2 and 4 (rivals of 1) must go to Bob's team, and 3 (a rival of 2) to Alice's team. Friends 5 and 6 form their own group, whose smallest member 5 goes to Alice's team. Friend 7 has no rivals and is alone in their group, so they join Alice's team.

In the second example, friends 1, 2 and 3 are pairwise rivals. Two of them must end up on the same team, so the answer is IMPOSSIBLE.

Example 1

Input

7 5
2 3
1 2
3 4
6 5
4 1

Output

1 2 1 2 1 2 1

Example 2

Input

4 3
1 2
2 3
3 1

Output

IMPOSSIBLE

Hints · reveal one at a time if you're stuck

Hint 1

Think of friends as nodes and rivalries as edges. Once one friend's team is fixed, does a rival of that friend, or a rival of a rival, have any choice left?

Hint 2

Handle each linked group separately, starting from its smallest friend on team 1, and spread the team assignment outward so that each rivalry flips it. Use an explicit stack or queue rather than recursion, and report IMPOSSIBLE as soon as a rivalry joins two friends on the same team.

report an issue Prefer your own editor? sooks prep 150 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.