codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1548A.md (3391B)


      1 Source: https://codeforces.com/problemset/problem/1548/A
      2 
      3 A. Web of Lies
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 When you play the game of thrones, you win, or you die. There is no middle ground.
     22 
     23 Cersei Lannister, A Game of Thrones by George R. R. Martin
     24 
     25 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.
     26 
     27 A noble is defined to be vulnerable if both of the following conditions are satisfied:
     28 
     29 * the noble has at least one friend, and
     30 * all of that noble's friends have a higher power.
     31 
     32 You will have to process the following three types of queries.
     33 
     34 1. Add a friendship between nobles $u$ and $v$.
     35 2. Remove a friendship between nobles $u$ and $v$.
     36 3. Calculate the answer to the following process.
     37 
     38 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.
     39 
     40 Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive!
     41 
     42 Input
     43 
     44 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.
     45 
     46 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.
     47 
     48 The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) — the number of queries.
     49 
     50 The next $q$ lines contain the queries themselves, each query has one of the following three formats.
     51 
     52 * $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.
     53 * $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.
     54 * $3$ — print the answer to the process described in the statement.
     55 
     56 Output
     57 
     58 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.
     59 
     60 Examples
     61 
     62 Input
     63 
     64 ```
     65 4 3
     66 2 1
     67 1 3
     68 3 4
     69 4
     70 3
     71 1 2 3
     72 2 3 1
     73 3
     74 ```
     75 
     76 Output
     77 
     78 ```
     79 2
     80 1
     81 ```
     82 
     83 Input
     84 
     85 ```
     86 4 3
     87 2 3
     88 3 4
     89 4 1
     90 1
     91 3
     92 ```
     93 
     94 Output
     95 
     96 ```
     97 1
     98 ```
     99 
    100 Note
    101 
    102 Consider the first example. In the first type 3 query, we have the diagram below.
    103 
    104 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$.
    105 
    106  ![](https://espresso.codeforces.com/483bb69a9e0172a45f351cb5e2f302d79332da72.png) 
    107 
    108 In the second type 3 query, the only surviving noble is $4$.
    109 
    110 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.
    111 
    112  ![](https://espresso.codeforces.com/30e26ef380f9ffa399c28c4afc4f8f65b32290fd.png)