C. Web of Lies

题目: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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值