#P84. [CSP-S 2022] Galaxy war

[CSP-S 2022] Galaxy war

Person in Charge

Problem Description

In this round of interstellar warfare, our side has established nn outposts in the universe, connected by mm unidirectional wormholes. All wormholes that end at outpost uu are classified as the wormholes of outpost uu.

Amid the raging war, these wormholes are hard to maintain for long, and enemy attacks may come at any time. The effective enemy attacks can be divided into two categories:

  1. The enemy destroys a specific wormhole, which makes the two connected outposts unable to reach each other directly through this wormhole, but such an attack will not destroy the two connected outposts.
  2. The enemy destroys a specific outpost. Since the core technology of wormholes is concentrated at their exit, all undestroyed wormholes of this outpost will be destroyed along with it. However, the wormholes departing from this outpost will not be destroyed.

Note: Destruction only renders the wormholes unavailable, and does not eliminate their physical existence.

To fight against the enemy and maintain the connection between troops and outposts, our side has developed two types of special forces responsible for repairing wormholes:

  • Type A special forces can repair a specific wormhole.
  • Type B special forces can repair all damaged wormholes of a specific outpost.

Considering the characteristics of enemy attacks, our side has not stockpiled excessive strategic materials at the outposts. Therefore, an outpost is deemed available if at least one of its wormholes is repaired and in an available state.

Our side has mastered an exacting spatial characteristic. By utilizing this characteristic, our warships can teleport along wormholes to the enemy camp and launch precise strikes.

To seize the optimal time to launch a counteroffensive, the headquarters must monitor all changes on the battlefield to find a feasible moment for the counterattack. The commander-in-chief defines the following criteria:

  • An outpost is able to launch a counterattack if starting from this outpost, warships can perform an infinite number of wormhole traversals by choosing an appropriate route (revisiting the same outpost or wormhole multiple times is allowed).
  • To ensure the continuity of wormhole traversals and minimize the mass-energy loss when warships switch wormholes at an outpost, an outpost can achieve continuous traversal if and only if there is exactly one available wormhole departing from this outpost.
  • A moment is deemed an optimal counterattack moment if all our outposts can both launch a counterattack and achieve continuous traversal.

The commander-in-chief has ordered you to promptly report whether an optimal counterattack moment is reached based on real-time battlefield information after each change.

Input Format

The first line of input contains two positive integers n,mn, m.

The next mm lines each contain two integers u,vu, v, representing a unidirectional wormhole departing from outpost uu and arriving at outpost vv. It is guaranteed that uvu \ne v and no duplicate wormholes exist. All wormholes and outposts are intact initially.

The next line contains a positive integer qq, representing the number of operations and queries.

The next qq lines each represent an operation or query, starting with a positive integer tt indicating the type of instruction:

  • If t=1t = 1, followed by two integers u,vu, v: the enemy destroys the wormhole from outpost uu to outpost vv. It is guaranteed that the wormhole exists and is currently undestroyed.
  • If t=2t = 2, followed by one integer uu: the enemy destroys outpost uu. This attack has no effect if all wormholes of the outpost have already been destroyed.
  • If t=3t = 3, followed by two integers u,vu, v: our side repairs the wormhole from outpost uu to outpost vv. It is guaranteed that the wormhole exists and is currently destroyed.
  • If t=4t = 4, followed by one integer uu: our side repairs outpost uu. This repair has no effect if there are no destroyed wormholes belonging to the outpost.

After executing each instruction, you need to determine whether a counterattack can be launched. Output YES if possible, otherwise output NO.

Output Format

Output a total of qq lines. For each instruction, output the judgment result after the execution of the instruction.

Samples

3 6
2 3
2 1
1 2
1 3
3 1
3 2
11
1 3 2
1 2 3
1 1 3
1 1 2
3 1 3
3 3 2
2 3
1 3 1
3 1 3
4 2
1 3 2
NO
NO
YES
NO
YES
NO
NO
NO
YES
NO
NO

Sample (1) Explanation

The state of the wormholes can be referred to the image below, where the edges represent the existing and undestroyed wormholes:

An image is available here. If not visible, download it from the "Attachments" section.

Sample (2)

See galaxy/galaxy2.in and galaxy/galaxy2.ans in the attachments.

Sample (3)

See galaxy/galaxy3.in and galaxy/galaxy3.ans in the attachments.

Sample (4)

See galaxy/galaxy4.in and galaxy/galaxy4.ans in the attachments.

Data Range

For all test data, it is guaranteed that 1n5×1051 \le n \le 5 \times 10^5, 1m5×1051 \le m \le 5 \times 10^5, 1q5×1051 \le q \le 5 \times 10^5.

Test Case No. nn \le mm \le qq \le Special Restriction
131 \sim 3 1010 2020 5050 None
484 \sim 8 10310^3 10410^4 10310^3
9109 \sim 10 5×1055 \times 10^5 5×1055 \times 10^5 No operations of type t=2t=2 and t=4t=4
111211 \sim 12 No operations of type t=4t=4
131613 \sim 16 10510^5 None
172017 \sim 20 5×1055 \times 10^5