首页 > 其他 > 详细

LA 3644 简单并查集

时间:2014-02-25 13:56:45      阅读:451      评论:0      收藏:0      [点我收藏+]

题目大意:有一些简单的化合物,每个化合物由两种元素组成,把这些化合物按顺序装车,若k个化合物正好包含k种元素,那么就会爆炸。避免爆炸,有些化合物就不能装车。求有多少个不能装车。

题目分析:若k个化合物正好包含k种元素,那么就会爆炸。我们把每种元素看成一个顶点,每种化合物看成一条边,若有环存在的时候正好是爆炸的情况,所以避免成环记录不能放的数量就行了。

 

bubuko.com,布布扣
#include<iostream>
#include<cstdio>
using namespace std;

const int maxn=100010;
int f[maxn];

int findset(int x){ return f[x]==x?x:f[x]=findset(f[x]); }
int main()
{
    int x,y,i,remain;
    while(scanf("%d",&x) == 1)
    {
        remain=0;
        for(i=0;i<maxn;i++) f[i]=i;
        while(x!=-1)
        {
            scanf("%d",&y);
            x=findset(x);y=findset(y);
            if(x==y) remain++;
            else f[x]=y;
            scanf("%d",&x);
        }
        printf("%d\n",remain);
    }
    return 0;
}
bubuko.com,布布扣

LA 3644 简单并查集

原文:http://www.cnblogs.com/xiong-/p/3564958.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!