#P10648. [NordicOI 2023] Island Alliances

[NordicOI 2023] Island Alliances

Background

Translated from NordicOI 2023 Problem C Island Alliances.

Problem Description

There are nn islands on the sea. Among them, there are mm pairs of islands that have a hostile relationship. However, because the weather is harsh, an island cannot survive alone, so some islands propose forming “alliances”.

There are qq alliance requests. Each request contains a pair (ai,bi)(a_i,b_i), meaning that island aia_i wants to form an alliance with island bib_i. However, due to hostile relationships, this proposal may not be accepted. If none of the islands in the alliance that originally contains aia_i has a hostile relationship with any of the islands in the alliance that contains bib_i, then the two sides can agree to form an alliance; otherwise, they will refuse.

Note that once the alliance is approved, the two alliances are immediately merged into a single alliance.

Input Format

The first line contains three integers nn, mm, and qq, meaning there are nn islands, mm hostile pairs of islands, and qq proposals.

The next mm lines each contain a pair of integers (ui,vi)(u_i,v_i), meaning island uiu_i and island viv_i are hostile to each other.

Then the next qq lines each contain two integers aia_i and bi (aibi)b_i\ (a_i \neq b_i), meaning island aia_i wants to form an alliance with island bib_i.

Output Format

For each proposal, output APPROVE if it is accepted; otherwise output REFUSE.

3 1 2
1 2
2 1
1 3
REFUSE
APPROVE
8 3 7
1 2
2 3
3 4
1 2
4 5
5 6
7 8
3 4
1 3
2 4
REFUSE
APPROVE
APPROVE
APPROVE
REFUSE
APPROVE
APPROVE

Hint

This problem uses bundled testdata.

  • Subtask 1 (15 points): 2n5002 \leq n \leq 500, 1m1051 \leq m \leq 10^5, 1q1051 \leq q \leq 10^5.
  • Subtask 2 (17 points): 2n1052 \leq n \leq 10^5, 1m2501 \leq m \leq 250, 1q1051 \leq q \leq 10^5.
  • Subtask 3 (20 points): 2n50002 \leq n \leq 5000, 1m50001 \leq m \leq 5000, 1q1051 \leq q \leq 10^5.
  • Subtask 4 (23 points): It is guaranteed that the number of refused proposals is at most once.
  • Subtask 5 (25 points): No special constraints.

For all testdata, 2n1052 \le n \le 10^5, 1m,q1051 \le m,q \le 10^5, 1ai,bin1 \le a_i,b_i \le n, and each pair (ai,bi)(a_i,b_i) is distinct.

Translated by ChatGPT 5