并查集之种类并查集

种类并查集就是将不同种类的合并在一起(qaq好像是废话)

直接看题吧

1.P1525 关押罪犯

输入 #1复制

4 6
1 4 2534
2 3 3512
1 2 28351
1 3 6618
2 4 1805
3 4 12884

输出 #1复制

3512

可以直接用并查集 先贪心从大到小排序 然后加一个数组来判断他的敌人  如果他有敌人了 将它敌人和敌人合并

当他和敌人在一起时break 输出此时的仇恨值(总的来说还是挺水的)

#include<bits/stdc++.h>
const int maxn = 1e5 + 5;
const int maxm = 2e4 + 5;
int fa[maxn];
int d[maxn];
int n, m;
using namespace std;
struct node{
	int s;
	int t;
	int w;
}e[maxn];

bool cmp(node a, node b){
	return a.w > b.w;
}

int find(int x){
	if(fa[x] == x)
		return x;
	return fa[x] = find(fa[x]);
}

bool f(int a, int b){
	if(find(a) == find(b))
		return 1;
	return 0;
}

int unit(int a, int b){
	fa[find(a)] = find(b);
} 

int main(){
	cin >> n >> m;
	for(int i = 1; i <= n; i++)
		fa[i] = i;
	for(int i = 1; i <= m; i++){
		cin >> e[i].s >> e[i].t >> e[i].w;
	}
	sort(e + 1, e + m + 1, cmp);
	for(int i = 1; i <= m + 1; i++){
		if(f(e[i].s, e[i].t)){
			cout << e[i].w << endl;
			break;
		}
		else{
			if(!d[e[i].s])	d[e[i].s] = e[i].t;
			else
				unit(d[e[i].s], e[i].t);
			if(!d[e[i].t])	d[e[i].t] = e[i].s;
			else
				unit(d[e[i].t], e[i].s);
		}
			
	}
}
 

第二题

A Bug's Life

 HDU - 1829 

Background 
Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs and their interactions were easy to identify, because numbers were printed on their backs. 

Problem 
Given a list of bug interactions, decide whether the experiment supports his assumption of two genders with no homosexual bugs or if it contains some bug interactions that falsify it. 

Input

The first line of the input contains the number of scenarios. Each scenario starts with one line giving the number of bugs (at least one, and up to 2000) and the number of interactions (up to 1000000) separated by a single space. In the following lines, each interaction is given in the form of two distinct bug numbers separated by a single space. Bugs are numbered consecutively starting from one.

Output

The output for every scenario is a line containing "Scenario #i:", where i is the number of the scenario starting at 1, followed by one line saying either "No suspicious bugs found!" if the experiment is consistent with his assumption about the bugs' sexual behavior, or "Suspicious bugs found!" if Professor Hopper's assumption is definitely wrong.

Sample Input

2
3 3
1 2
2 3
1 3
4 2
1 2
3 4

Sample Output

Scenario #1:
Suspicious bugs found!

Scenario #2:
No suspicious bugs found!

其实吧 第二题跟第一题是差不多的 都是两堆

但是我调了好久的数组大小(丢人)

#include<bits/stdc++.h>
const int maxn = 2e6 + 5;
const int maxm = 2e4 + 5;
int fa[maxn];
int d[maxn];
int n, m;
using namespace std;
struct node{
	int s;
	int t;
}e[maxn];

int find(int x){
	if(fa[x] == x)
		return x;
	return fa[x] = find(fa[x]);
}

bool f(int a, int b){
	if(find(a) == find(b))
		return 1;
	return 0;
}

int unit(int a, int b){
	fa[find(a)] = find(b);
} 

int main(){
	int t ;
	cin >> t;
	int u = t;
	while(t--){
		cin >> n >> m;
		for(int i = 0; i <= 2000; i++)
			fa[i] = i,d[i] = 0;
		for(int i = 1; i <= m; i++){
			int a, b;
			cin >> e[i].s >> e[i].t;
		}
		cout << "Scenario #" << u - t << ":" << endl;
		for(int i = 1; i <= m + 1; i++){
			if(i == m + 1){
				cout << "No suspicious bugs found!" << endl;
				cout << endl;
			}
				
			else{
				if(f(e[i].s, e[i].t)){
					cout << "Suspicious bugs found!" << endl;
					cout << endl;
					break;
				}
				else{
					if(!d[e[i].s])	d[e[i].s] = e[i].t;
					else
						unit(d[e[i].s], e[i].t);
					if(!d[e[i].t])	d[e[i].t] = e[i].s;
					else
						unit(d[e[i].t], e[i].s);
			}
			}
				
		}
		
	}
	
}
 

 3.P2024 [NOI2001]食物链

这是最经典的题目了

输入输出样例

输入 #1复制

100 7
1 101 1
2 1 2
2 2 3
2 3 3
1 1 3
2 3 1
1 5 5

输出 #1复制

3

说明/提示

1 ≤ N ≤ 5 ∗ 10^4

1 ≤ K ≤ 10^5

这时有个拆点的思想

就是在同一范围的为同类

1-n 吃 n-2n

n-2n吃2n-3n

2n-3n吃1-n

然后就很好写了

#include<bits/stdc++.h>
const int maxn = 2e6 + 5;
const int maxm = 2e4 + 5;
int fa[maxn];
int n, m;
using namespace std;

int find(int x){
	if(fa[x] == x)
		return x;
	return fa[x] = find(fa[x]);
}

void unit(int a, int b){
	if(find(a) != find(b))
		fa[find(a)] = find(b);
}

int main(){
	cin >> n >> m;
	for(int i = 1; i <= 3 * n; i++)
		fa[i] = i;
	int ans = 0;
	for(int i = 1; i <= m; i++){
		int q, a, b;
		cin >> q >> a >> b;
		if(a > n || b > n){
			ans++;
			continue;
		}
		if(q == 1){
			if(find(a) == find(b + n) || find(a) == find(b + 2 * n)){
				ans++;
				continue;
			}
			unit(a, b);
			unit(a + n, b + n);
			unit(a + 2 * n, b + 2 * n);	
		}
		if(q == 2){
			if(find(a) == find(b) || find(a) == find(b + 2 * n)){
				ans++;
				continue;
			}
			unit(a, b + n);
			unit(a + n, b + 2 * n);
			unit(a + 2 * n, b);
		}
	}
		
	cout << ans << endl;
}
 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值