UVALive - 3644 X-Plosives

来源:互联网 发布:淘宝钱货一方 编辑:程序博客网 时间:2024/06/06 17:10

题意:每个化合物都是有两种元素组成的,如果车上存在k个简单化合物时,如果它和已装车的化合物形成易燃物的话,你就应该拒绝装车,否则装车,输出没有装车的个数

思路:简单的并查集应用

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAXN = 100005;int f[MAXN];int find(int x){    if (x != f[x])        f[x] = find(f[x]);    return f[x];}int main(){    int x,y;    while (scanf("%d",&x) != EOF){        for (int i = 0; i < MAXN; i++)            f[i] = i;        int ans = 0;        while (x != -1){            scanf("%d",&y);            int fx = find(x),fy = find(y);            if (fx == fy)                ans++;            else f[fx] = fy;            scanf("%d",&x);        }        printf("%d\n",ans);    }    return 0;}



0 0
原创粉丝点击