并查集的解题集合

1.   UVA 1160                             X-Plosives

A secret service developed a new kind ofexplosive that attain its volatile property only when a specific association ofproducts occurs. Each product is a mix of two different simple compounds, towhich we call abinding pair. If N>2, thenmixing N different binding pairs containing N simple compounds creates apowerful explosive.For example, the bindingpairs A+B, B+C, A+C (three pairs, three compounds) result in an explosive,while A+B, B+C, A+D (three pairs, four compounds) does not.

 

You are not a secret agent but only a guyin a delivery agency with one dangerous problem: receive binding pairs insequential order and place them in a cargo ship. However, you must avoidplacing in the same room an explosive association. So, after placing a set ofpairs, if you receive one pair that might produce an explosion with some of thepairs already in stock, you must refuse it, otherwise, you must accept it.

 

An example. Let’s assume you receive thefollowing sequence: A+B, G+B, D+F, A+E, E+G, F+H. You would accept the firstfour pairs but then refuse E+G since it would be possible to make the followingexplosive with the previous pairs: A+B, G+B, A+E, E+G (4 pairs with 4 simplecompounds). Finally, you would accept the last pair, F+H.

 

Compute thenumber of refusals given a sequence of binding pairs.

 

Input

The input will contain several test cases, each of them as described below.Consecutive test cases are separated by a single blank line.

Instead of letters we will use integersto represent compounds. The input contains several lines. Each line(except the last) consists of two integers (each integer lies between 0 and 105)separated by a single space, representing a binding pair. The input ends in aline with the number –1. You may assume that no repeated binding pairsappears in the input.

 

Output

For each test case, a single line with the number ofrefusals.

 

Sample Input

1 2

3 4

3 5

3 1

2 3

4 1

2 6

6 5

-1

 

Sample Output

3


并查集找不同的数,当第1~X组共有N个不同元素,且X=N时会发生爆炸,结果累加1,删除这一组,下一组变为第X组继续判断。

#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
#define max 100005
int p[max];

int find(int a){
	return p[a]==a?a:(p[a]=find(p[a]));
}

int main(){
	int i,a,b,ans;
	while(scanf("%d",&a)&&a!=-1){
		for(i=0;i<max;i++)
			p[i]=i;
			ans=0;
		while(a!=-1){
			scanf("%d",&b);
			int tx=find(a);
			int ty=find(b);
			if(tx==ty) ans++;
			else p[tx]=ty;
			scanf("%d",&a);
		}
		printf("%d\n",ans);
	}
	return 0;
} 

2.   HDU  1856

More is better

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others)
Total Submission(s): 22370    Accepted Submission(s): 8132


Problem Description
Mr Wang wants some boys to help him with a project. Because the project is rather complex,  the more boys come, the better it will be. Of course there are certain requirements.

Mr Wang selected a room big enough to hold the boys. The boy who are not been chosen has to leave the room immediately. There are 10000000 boys in the room numbered from 1 to 10000000 at the very beginning. After Mr Wang's selection any two of them who are still in this room should be friends (direct or indirect), or there is only one boy left. Given all the direct friend-pairs, you should decide the best way.
 

Input
The first line of the input contains an integer n (0 ≤ n ≤ 100 000) - the number of direct friend-pairs. The following n lines each contains a pair of numbers A and B separated by a single space that suggests A and B are direct friends. (A ≠ B, 1 ≤ A, B ≤ 10000000)
 

Output
The output in one line contains exactly one integer equals to the maximum number of boys Mr Wang may keep. 
 

Sample Input
  
  
4 1 2 3 4 5 6 1 6 4 1 2 3 4 5 6 7 8
 

Sample Output
  
  
4 2
Hint
A and B are friends(direct or indirect), B and C are friends(direct or indirect), then A and C are also friends(indirect). In the first sample {1,2,5,6} is the result. In the second sample {1,2},{3,4},{5,6},{7,8} are four kinds of answers.
 

Author
lxlcrystal@TJU
 

Source
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   1272  1325  1879  1863  1875 


要用结构体记录结点的度:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
#define max 10000005
struct node{
	int parent;  //父节点 
	int rank;  //度 
}p[max];
int find(int x){
	return p[x].parent==x?x:(p[x].parent=find(p[x].parent));
}
int main(){
	int n,ans,i,a,b;
	while(scanf("%d",&n)!=EOF){
		for(i=0;i<max;i++){
			p[i].parent=i;
			p[i].rank=1;
		}
		ans=0;
		while(n--){
			scanf("%d%d",&a,&b);
			int ta=find(a);
			int tb=find(b);
			if(ta!=tb){
				p[ta].parent=tb;
				p[tb].rank+=p[ta].rank;
			}
		}
		for(i=0;i<max;i++){
			if(p[i].parent==i){
				if(p[i].rank>ans)
					ans=p[i].rank;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
} 





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值