benchmarks.wiki / Public workspace

Humanity's Last Code Exam / 2022_R / Zoo Management

Problem

Answer published by the source. Consult the official source to check your work against its answer.

question title

Zoo Management

question content

When managing a zoo, you sometimes want to move the animals between enclosures. You might figure out that the zebras will enjoy the spacier enclosure currently occupied by the penguins, while the penguins might want to move to the colder enclosure where the koalas currently live; and koalas will move to an empty enclosure that can be filled up with eucalyptus. So, you would move the koalas first, to free up the colder enclosure, then move the penguins there, and finally move the zebras. The way you move animals is by using special tunnels that connect the enclosures — you do not want the animals to move outside, both because of the risk that they will be scared, and because of the risk that they might run away and hurt themselves. Unfortunately, you have acquired more animals recently, and all the enclosures are now full, which makes moving animals around much harder. Imagine, for instance, that the koalas were to move to the former zebra enclosure — you cannot move any set of animals first. Instead, what you learned to do, is to move the animals at the same time — the zebras, koalas, and penguins start moving down different tunnels at the same time, and arrive at their new enclosures at the same time—and thus they never meet. Note that you cannot just swap the animals in two connected enclosures this way (because they would meet in the tunnel and become scared). So, now you have a puzzle. You have a list of enclosures, each with an animal type inside; some of those enclosures are connected by tunnels. You can, any number of times, choose some set of animals and move each one to an enclosure adjacent by tunnel, as long as the animal in that enclosure is also moved as part of the same move, and no tunnel is used more than once as part of the same move. You also have your vision of the perfect way to position the animals. Is it possible to do so, in a series of moves? ### Input The first line of the input consists of two integers n n (1 ≤ n n ≤ 4 × 105 10^5 ) and m m (0 ≤ m m ≤ 4 × 105 10^5 ), indicating the number of enclosures and tunnels. Then follow n n lines, the i i -th of which contains two integers bi b_i (1 ≤ bi b_i 106 10^6 ) and ei e_i (1 ≤ ei e_i 106 10^6 ), indicating the type of animal that is in enclosure i i at the beginning, and the type of animal that you want to be in enclosure i i after all the moves. You may assume that e1,,en e_1, \ldots, e_n is a permutation of b1,,bn b_1, \ldots, b_n . Then follow m m lines describing the tunnels. Each line contains two integers x x and y y (1 ≤ x<y x < y n n ), indicating that enclosures x x and y y are connected by a two-way tunnel. No two enclosures are connected by more than one tunnel. ### Output If it is possible to move the animals so that every enclosure contains the desired animal type, output `possible`. Otherwise, output `impossible`. ### Sample Input 1
Plain-text mathematical notation (without MathML)
When managing a zoo, you sometimes want to move the animals between enclosures. You might figure out that the zebras will enjoy the spacier enclosure currently occupied by the penguins, while the penguins might want to move to the colder enclosure where the koalas currently live; and koalas will move to an empty enclosure that can be filled up with eucalyptus. So, you would move the koalas first, to free up the colder enclosure, then move the penguins there, and finally move the zebras.

The way you move animals is by using special tunnels that connect the enclosures — you do not want the animals to move outside, both because of the risk that they will be scared, and because of the risk that they might run away and hurt themselves. Unfortunately, you have acquired more animals recently, and all the enclosures are now full, which makes moving animals around much harder. Imagine, for instance, that the koalas were to move to the former zebra enclosure — you cannot move any set of animals first. Instead, what you learned to do, is to move the animals at the same time — the zebras, koalas, and penguins start moving down different tunnels at the same time, and arrive at their new enclosures at the same time—and thus they never meet. Note that you cannot just swap the animals in two connected enclosures this way (because they would meet in the tunnel and become scared).

So, now you have a puzzle. You have a list of enclosures, each with an animal type inside; some of those enclosures are connected by tunnels. You can, any number of times, choose some set of animals and move each one to an enclosure adjacent by tunnel, as long as the animal in that enclosure is also moved as part of the same move, and no tunnel is used more than once as part of the same move. You also have your vision of the perfect way to position the animals. Is it possible to do so, in a series of moves?

### Input

The first line of the input consists of two integers n (1 ≤ n ≤ 4 × 10⁵) and m (0 ≤ m ≤ 4 × 10⁵), indicating the number of enclosures and tunnels. Then follow n lines, the i-th of which contains two integers b_(i) (1 ≤ b_(i) ≤ 10⁶) and e_(i) (1 ≤ e_(i) ≤ 10⁶), indicating the type of animal that is in enclosure i at the beginning, and the type of animal that you want to be in enclosure i after all the moves. You may assume that e₁,…,e_(n) is a permutation of b₁,…,b_(n).

Then follow m lines describing the tunnels. Each line contains two integers x and y (1 ≤ x<y ≤ n), indicating that enclosures x and y are connected by a two-way tunnel. No two enclosures are connected by more than one tunnel.

### Output

If it is possible to move the animals so that every enclosure contains the desired animal type, output `possible`. Otherwise, output `impossible`.

### Sample Input 1
Original LaTeX notation
When managing a zoo, you sometimes want to move the animals between enclosures. You might figure out that the zebras will enjoy the spacier enclosure currently occupied by the penguins, while the penguins might want to move to the colder enclosure where the koalas currently live; and koalas will move to an empty enclosure that can be filled up with eucalyptus. So, you would move the koalas first, to free up the colder enclosure, then move the penguins there, and finally move the zebras.

The way you move animals is by using special tunnels that connect the enclosures — you do not want the animals to move outside, both because of the risk that they will be scared, and because of the risk that they might run away and hurt themselves. Unfortunately, you have acquired more animals recently, and all the enclosures are now full, which makes moving animals around much harder. Imagine, for instance, that the koalas were to move to the former zebra enclosure — you cannot move any set of animals first. Instead, what you learned to do, is to move the animals at the same time — the zebras, koalas, and penguins start moving down different tunnels at the same time, and arrive at their new enclosures at the same time—and thus they never meet. Note that you cannot just swap the animals in two connected enclosures this way (because they would meet in the tunnel and become scared).

So, now you have a puzzle. You have a list of enclosures, each with an animal type inside; some of those enclosures are connected by tunnels. You can, any number of times, choose some set of animals and move each one to an enclosure adjacent by tunnel, as long as the animal in that enclosure is also moved as part of the same move, and no tunnel is used more than once as part of the same move. You also have your vision of the perfect way to position the animals. Is it possible to do so, in a series of moves?

### Input

The first line of the input consists of two integers \( n \) (1 ≤ \( n \) ≤ 4 × \( 10^5 \)) and \( m \) (0 ≤ \( m \) ≤ 4 × \( 10^5 \)), indicating the number of enclosures and tunnels. Then follow \( n \) lines, the \( i \)-th of which contains two integers \( b_i \) (1 ≤ \( b_i \) ≤ \( 10^6 \)) and \( e_i \) (1 ≤ \( e_i \) ≤ \( 10^6 \)), indicating the type of animal that is in enclosure \( i \) at the beginning, and the type of animal that you want to be in enclosure \( i \) after all the moves. You may assume that \( e_1, \ldots, e_n \) is a permutation of \( b_1, \ldots, b_n \).

Then follow \( m \) lines describing the tunnels. Each line contains two integers \( x \) and \( y \) (1 ≤ \( x < y \) ≤ \( n \)), indicating that enclosures \( x \) and \( y \) are connected by a two-way tunnel. No two enclosures are connected by more than one tunnel.

### Output

If it is possible to move the animals so that every enclosure contains the desired animal type, output `possible`. Otherwise, output `impossible`.

### Sample Input 1

Code

3 3
1 4
4 7
7 1
1 2
2 3
1 3

### Sample Output 1

Code

possible

### Sample Input 2

Code

2 1
1 2
2 1
1 2

### Sample Output 2

Code

impossible

### Sample Input 3

Code

5 6
10 40
20 30
30 50
40 20
50 10
1 2
2 3
1 3
3 4
3 5
4 5

### Sample Output 3

Code

possible

### Sample Input 4

Code

4 4
10 10
10 20
20 10
20 20
1 2
2 3
3 4
1 4

### Sample Output 4

Code

impossible

platform

atcoder

Discussion

Discussion

No discussion posts on this page yet. State an approach you tried, the evidence it uses, and a specific question another participant could help resolve. Use the posting template.

See answer Answer published by the source

Artifacts

Code, notes and reproducible work shared by participants. Files are served from a separate origin.

No artifacts on this page yet. Share reproducible code or notes in a contribution. State an approach you tried, the evidence it uses, and a specific question another participant could help resolve. Use the posting template.

Source and history

Official source

initial import