Web of Lies
Contest: Codeforces Round 736 (Div. 1)
Difficulty rating: 1400
Tags: brute force, graphs, greedy

There are $$$n$$$ nobles, numbered from $$$1$$$ to $$$n$$$. Noble $$$i$$$ has a power of $$$i$$$. There are also $$$m$$$ "friendships". A friendship between nobles $$$a$$$ and $$$b$$$ is always mutual.

A noble is defined to be vulnerable if both of the following conditions are satisfied:

- the noble has at least one friend, and
- all of that noble's friends have a higher power.

You will have to process the following three types of queries.

1. Add a friendship between nobles $$$u$$$ and $$$v$$$.
2. Remove a friendship between nobles $$$u$$$ and $$$v$$$.
3. Calculate the answer to the following process.

The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles.

Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive!

Input format:
The first line contains the integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2\cdot 10^5$$$, $$$0 \le m \le 2\cdot 10^5$$$) — the number of nobles and number of original friendships respectively.

The next $$$m$$$ lines each contain the integers $$$u$$$ and $$$v$$$ ($$$1 \le u,v \le n$$$, $$$u \ne v$$$), describing a friendship. No friendship is listed twice.

The next line contains the integer $$$q$$$ ($$$1 \le q \le 2\cdot {10}^{5}$$$) — the number of queries.

The next $$$q$$$ lines contain the queries themselves, each query has one of the following three formats.

- $$$1$$$ $$$u$$$ $$$v$$$ ($$$1 \le u,v \le n$$$, $$$u \ne v$$$) — add a friendship between $$$u$$$ and $$$v$$$. It is guaranteed that $$$u$$$ and $$$v$$$ are not friends at this moment.
- $$$2$$$ $$$u$$$ $$$v$$$ ($$$1 \le u,v \le n$$$, $$$u \ne v$$$) — remove a friendship between $$$u$$$ and $$$v$$$. It is guaranteed that $$$u$$$ and $$$v$$$ are friends at this moment.
- $$$3$$$ — print the answer to the process described in the statement.

Output format:
For each type $$$3$$$ query print one integer to a new line. It is guaranteed that there will be at least one type $$$3$$$ query.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
Consider the first example. In the first type 3 query, we have the diagram below.

In the first round of the process, noble $$$1$$$ is weaker than all of his friends ($$$2$$$ and $$$3$$$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $$$3$$$ is weaker than his only friend, noble $$$4$$$, and is therefore killed. At this point, the process ends, and the answer is $$$2$$$.

In the second type 3 query, the only surviving noble is $$$4$$$.

The second example consists of only one type $$$3$$$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $$$1$$$, since only one noble survives.

Example input:
4 3
2 1
1 3
3 4
4
3
1 2 3
2 3 1
3

Example output:
2
1

Example input:
4 3
2 3
3 4
4 1
1
3

Example output:
1

Editorial:
Due to the queries, actually simulating the process will be too expensive.
Proof that the Process will End: Assume after round $$$x$$$, $$$x_i$$$ nobles are killed. If $$$x_i=0$$$, then the state of the graph doesn't change, so no more nobles will die in later rounds, so the process will have ended. If $$$x_i > 0$$$, then the number of nobles decreases. Thus, the maximum number of rounds the process can last is $$$N$$$, so it must end.
$$$\textbf{Lemma 1:}$$$ At the end of the process, no two nobles will still be friends.
Proof by Contradiction: Assume that nobles $$$u$$$ and $$$v$$$ (assume WLOG that $$$u < v$$$) are still friends and the process is over. In order for $$$u$$$ to avoid being killed, there must be a noble $$$w$$$ weaker than $$$u$$$ that is also a friend of $$$u$$$. The same logic then applies to $$$w$$$, and we have an infinite descent argument. There are only a finite number of nobles weaker than $$$u$$$, so there will be a contradiction.
$$$\textbf{Lemma 2:}$$$ If all of a noble's friends are weaker than it, that noble cannot be killed.
Direct Proof: Since none of the noble's friends are stronger than it, it is impossible for $$$\textit{all}$$$ of them to be stronger at any point in the process.
$$$\textbf{Final Idea:}$$$ By combining Lemmas 1 and 2, we can prove that if ALL of a noble's friends are weaker than it, that noble survives, otherwise it can't survive. This leads to the solution.
Maintain in an array the number of nobles weaker than noble $$$i$$$. Since the updates guarantee that the edge being removed/added does/doesn't exist respectively, we only need to keep track of the number of edges of each noble. After linear precomputation, updates and queries take constant time.
The final time complexity is: $$$\mathcal{O}(N+M+Q)$$$.