X-Plosives (并查集)

题目:

A secret service developed a new kind of explosive that attain its volatile property only when a specific association of products occurs. Each product is a mix of two different simple compounds, to which we call a binding pair. If N>2, then mixing N different binding pairs containing N simple compounds creates a powerful explosive. For example, the binding pairs 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 guy in a delivery agency with one dangerous problem: receive binding pairs in sequential order and place them in a cargo ship. However, you must avoid placing in the same room an explosive association. So, after placing a set of pairs, if you receive one pair that might produce an explosion with some of the pairs already in stock, you must refuse it, otherwise, you must accept it.

An example. Lets assume you receive the following sequence: A+B, G+B, D+F, A+E, E+G, F+H. You would accept the first four pairs but then refuse E+G since it would be possible to make the following explosive with the previous pairs: A+B, G+B, A+E, E+G (4 pairs with 4 simple compounds). Finally, you would accept the last pair, F+H.

Compute the number 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 integers to 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 a line with the number –1. You may assume that no repeated binding pairs appears in the input.

Output

For each test case,the output must follow the description below .a single line with the number of refusals.

Sample Input

1 2
3 4
3 5
3 1
2 3
4 1
2 6
6 5
-1  

Sample Output

3
大意:
特勤处研制出一种新的xx,只有当发生特定的产品关联时,xx才能挥发。每个产品是两种不同的简单化合物的混合物,我们称之为结合对。
如果n>2,则混合n个不同的含有n个简单化合物的结合对产生强烈的爆炸物。
例如,结合对A+B,B+C,A+C(三对,三个化合物)导致爆炸,而A+B,B+C,A+D(三对,四个化合物)不发生。
你不是一个特工,但只有一个人在一个交付代理有一个危险的问题:接收绑定对顺序,并把它们放在货船上。
但是,你必须避免在同一房间里发生爆炸性的联想。
所以,在放置一套成对后,如果你收到一对可能会发生爆炸的成双成对的库存,你必须拒绝它,否则,你必须接受它。
一个例子。假设你收到以下序列:A+B,G+B,D+F,A+E,E+G,F+H。
你会接受前四对,然后拒绝E+G,因为它可能会与前一对产生如下爆炸:A+B,G+B,A+E,E+G(4对,含4个简单化合物)。
最后,你会接受最后一对,f+h。
有一些化合物,每个化合物都由两种元素组成的(每个元素用一个大写字母表示)。
你是一个装箱的工人,从实验员那里按照顺序依次把一些简单化合物装到车上。但这里存在一个安全隐患:
如果车上存在k个简单的化合物,正好包含k中元素,那么它们将组成一个易爆的混合物。
为了安全起见,每当你拿到一个化合物时,如果它和已装的化合物形成易爆混合物,你就应当拒绝装车;
否则就应该装车。编程输出有多少个没有装车的化合物
不能存在K个混合物,使有K个元素,即图中不能有环,求不能存在的混合物个数

 分析:

先进行输入输入,一个循环就够了,通过判断是否为-1去选择是否进行输出,就是判断环,出现环计数,最后输出数量,注意数据初始化位置,详见代码。

附代码:

#include<map>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int sum,p[100005];
int find(int x)
{
    return p[x]==x?x:p[x]=find(p[x]);
}
void intt()
{
    for(int i=0; i<100005; i++)
        p[i]=i;
}
void nion(int a,int b)
{
    int f1=find(a);
    int f2=find(b);
    if(f1!=f2)
        p[f2]=f1;
    else
        sum++;//计数
}
int main()
{
    int n,m;
    intt();//初始化
    sum=0;
    while(~scanf("%d",&n))
    {
        if(n==-1)
        {
            intt();
            printf("%d
",sum);
            sum=0;//归零
            continue;
        }
        scanf("%d",&m);
        nion(n,m);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/She-Chuan/p/9494406.html