nyoj 1022 合纵连横 经典并查集

              思路:关键在于并查集的删点操作。 给每个诸侯国一个另外的编号,比如box[i]表示诸侯国i现在处于第box[i]个联盟,可以随时改变它的联盟编号,并且让box[i] = k, 实现删除操作。以前联盟中的那个点是一个虚点,只是帮助子节点找到根结点。查找时通过联盟编号查询。

AC代码

#include <cstdio>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000") 
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int> 
typedef long long LL;
const int maxn = 100000 + 5;
int p[maxn<<1], box[maxn], vis[maxn<<1];
int find(int x) {
	return p[x] == x ? x : p[x] = find(p[x]);
}

void unionset(int x, int y) {
	int px = find(box[x]), py = find(box[y]);
	if(px != py) p[px] = py;
}

int cur; 
void del(int x) { //删点 
	box[x] = cur++;
	p[box[x]] = box[x];
}

int main() {
	int n, m, kase = 1;
	while(scanf("%d%d", &n, &m) == 2) {
		cur = n;
		for(int i = 0; i < n; ++i) {
			p[i] = i;
			box[i] = i;
		}
		int x, y;
		char ch;
		for(int i = 0; i < m; ++i) {
			getchar();
			scanf("%c", &ch);
			if(ch == 'U') {
				scanf("%d%d", &x, &y);
				unionset(x, y);
			}
			else {
				scanf("%d", &x);
				del(x);
			}
		}
		memset(vis, 0, sizeof(vis));
		for(int i = 0; i < n; ++i) {
			vis[find(box[i])] = 1;
		}
		int ans = 0;
		for(int i = 0; i < cur; ++i) {
			if(vis[i]) ++ans;
		}
		printf("Case #%d: %d\n", kase++, ans);
	}
	return 0;
}

如有不当之处欢迎指出!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值