[ABC302E] Isolation

题意

有一张 nn 个点,初始时没有边的无向图。

你要维护 qq 次操作,每次操作可能是下面的其中一个:

  1. 添加一条 uuvv 的边。
  2. 删除与 uu 相连的所有边。

每次操作后,输出孤立点数量。

解法

发现添加只会添加一条边,而删除则可能删除许多。一条边如果被加入一次,那么最多只会被删除一次。

于是我们可以暴力删除,并且维护每个点的度。每个操作都将涉及到的点暴力重新算贡献。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <cstdlib>
#include <string>
using namespace std;

#define ll long long

const int N = 3e5 + 5, INF = 2e9, MOD = 1e9 + 7;

inline int read()
{
	int op = 1, x = 0;
	char ch = getchar();
	while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
	while (ch == '-')
	{
		op = -op;
		ch = getchar();
	}
	while (ch >= '0' and ch <= '9')
	{
		x = (x << 1) + (x << 3) + (ch ^ 48);
		ch = getchar();
	}
	return x * op;
}

inline void write(int x)
{
	if (x < 0) putchar('-'), x = -x;
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}

int n, m, t;
set<int> G[N];
int d[N];
int ans;
int main()
{
	// freopen("*.in", "r", stdin);
	// freopen("*.out", "w", stdout);
	scanf("%d%d", &n, &m);
	ans = n;
	while (m--)
	{
		int op;
		scanf("%d", &op);
		if (op == 1)
		{
			int u, v;
			scanf("%d%d", &u, &v);
			G[u].insert(v);
			G[v].insert(u);
			ans -= (d[u] == 0);
			ans -= (d[v] == 0);
			d[u]++;
			d[v]++;
		}
		else
		{
			int x;
			scanf("%d", &x);
			for (auto i : G[x])
			{
				d[i]--;
				ans += (d[i] == 0);
				G[i].erase(x);
			}
			G[x].clear();
			ans += (d[x] != 0);
			d[x] = 0;
		}
		printf("%d\n", ans);
	}
	return 0;
}
posted @ 2023-05-22 18:25  HappyBobb  阅读(6)  评论(0)    收藏  举报  来源