题目:https://codeforces.com/contest/1549/problem/C
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.
Add a friendship between nobles u and v.
Remove a friendship between nobles u and v.
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
The first line contains the integers n and m (1≤n≤2⋅105, 0≤m≤2⋅105) — the number of nobles and number of original friendships respectively.
The next m lines each contain the integers u and v (1≤u,v≤n, u≠v), describing a friendship. No friendship is listed twice.
The next line contains the integer q (1≤q≤2⋅105) — 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≤u,v≤n, u≠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≤u,v≤n, u≠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
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.
Examples
input
4 3
2 1
1 3
3 4
4
3
1 2 3
2 3 1
3
output
2
1
input
4 3
2 3
3 4
4 1
1
3
output
1
一道简答题,都不用建图,,在比赛过程中都不能快速过,太菜了太菜了我
AC代码:
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
int g[200050];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n,m;
cin >> n >> m;
int u,v;
int ans = 0;
for(int i = 1;i<=m;i++){
cin >> u >> v;
if(u > v) swap(u,v);
g[u]++;
if(g[u] == 1) ans++;
}
int q; cin >> q;
for(int i = 1;i<=q;i++){
int table;
cin >> table;
switch(table){
case 1:
cin >> u >> v;
if(u > v) swap(u,v);
g[u]++;
if(g[u] == 1) ans++;
break;
case 2:
cin >> u >> v;
if(u > v) swap(u,v);
g[u]--;
if(g[u] == 0) ans--;
break;
case 3:
cout << n - ans << endl;
break;
}
}
return 0;
}