【易爆物】 LA3644

【题目分析】

本题可以用图论解决,难点在于抽象出模型:就是如果产生易爆物品的话则必定在图里存在回路。

【AC代码】

#include<iostream>
using namespace std;
#define maxn 100009
int pa[maxn];
int findset(int x)
{
    return pa[x]!=x?pa[x]=findset(pa[x]):x;
}

int main()
{
    int x,y;
    for(int i=0;i<=maxn;i++)
    pa[i]=i;
    while(scanf("%d",&x)==1)
    {
        int refusal=0;
        while(x!=-1)
        {
            scanf("%d",&y);
             x=findset(x);y=findset(y);
            if(x==y) refusal++;
            else
             pa[x]=y;
            scanf("%d",&x);
        }
        printf("%d\n",refusal);
    }
   return 0;
}
时间: 2024-07-30 04:14:02

【易爆物】 LA3644的相关文章

【并查集】LA3644 X-Plosives

[并查集]LA3644 X-Plosives n个元素的集合应用问题--并查集. 题目大意 装车,把一些两个元素组成的化合物按输入次序往车上装,如果会发生爆炸(存在k个简单化合物,正好包含k种元素),记录,输出不能装车的化合物总数. 说一下思路 怎样理解"存在k个简单化合物,正好包含k种元素"? 每个化合物在顶点处分摊到1/2个元素,成环!根节点相同!问题都是等价的! 想一想并查集的查找和合并操作,在合并过程中计数那些根节点相同的边就可以了~ 参考代码 #include<bits

LA 3644 易爆物

https://vjudge.net/problem/UVALive-3644 简单的并查集题目. 1 #include<iostream> 2 using namespace std; 3 4 const int maxn = 100000 + 5; 5 6 int p[maxn]; 7 8 int find(int x) 9 { 10 return p[x] != x ? p[x] = (find(p[x])) : x; 11 } 12 13 int main() 14 { 15 //fr

(DS 《算法竞赛入门经典》)LA 3644 X-Plosives(并查集)

解题思路: 并查集 A secret service developed a new kind of explosive that attain its volatile property only when a specificassociation of products occurs. Each product is a mix of two different simple compounds, to which wecall a binding pair. If N > 2, then