其他分享
首页 > 其他分享> > X-Plosives UVA - 1160----并查集+思维

X-Plosives UVA - 1160----并查集+思维

作者:互联网

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.
Each test case 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

题意:现在手上有一些化合物,每个化合物都不相同且由两个不同整数构成,当你手上存在这种情况时:
手上有至少N(N>2)个化合物且其中有N个化合物正好包含N个不同的整数(即这N个整数每个出现了2次)。那么此时化合物不稳定。比如你有化合物(1,2),(2,3),(3,1)那么就是不稳定的,但是你如果只有(1,2),(2,3) 那么就是稳定的。
现在给出所有化合物给你的顺序,你要保证不会出现化合物不稳定的情况,输出你需要拒绝化合物的个数。
输入:包含多组实例。每个实例由连续的一对整数构成(整数属于[0,10^5])。不同实例之间是一个空行,没有重复的化合物会出现,且单行-1表示当前实例的输入结束。
输出:输出你需要拒绝的化合物个数。

解析:图中有环必要条件---- 化合物不稳定

所以我们只需要判段化合物的两种元素是否都出现在一个联通分量中,若是拒绝。



#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=100000+1000;
int pa[MAXN];
int findset(int x)
{
    if(pa[x]==x)return x;
    return pa[x]=findset(pa[x]);
}
void bind(int x,int y)
{
    int fa=findset(x);
    int fb=findset(y);
    pa[fa]=fb;
}
int main()
{
    int x,y;
    while(scanf("%d",&x)==1)
    {
        int sum=0;
        if(x==-1)
        {
            printf("0\n");
            continue;
        }
        scanf("%d",&y);
        for(int i=0;i<MAXN;i++)
            pa[i]=i;
        bind(x,y);
        while(scanf("%d",&x)==1&&x>=0)
        {
            scanf("%d",&y);
            int fa=findset(x);
            int fb=findset(y);
            if(fa==fb)//x与y已经是同一个连通分量了
                sum++;
            else
                bind(x,y);
        }
        printf("%d\n",sum);
 
    }
    return 0;
}

AKone123456 发布了289 篇原创文章 · 获赞 6 · 访问量 3939 私信 关注

标签:pairs,1160,compounds,int,查集,binding,findset,化合物,UVA
来源: https://blog.csdn.net/qq_43690454/article/details/104066143