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
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.
4 1 2 3 4 5 6 1 6 4 1 2 3 4 5 6 7 8
4 2HintA 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.
要用结构体记录结点的度:
#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;
}