LA 3644 易爆物

题目链接:https://vjudge.net/problem/UVALive-3644

题意:

有一些化合物,由2中元素组成的,一次把他们装到车上,有一个安全隐患,如果存在 k 个化合物,恰好包含 k 中元素,会发生爆照。

求:有多少个没有装车的化合物。

分析:

把元素看成一个结点,一种化合物连一条边,可以发现,其实,安全隐患就是一个环,即不能有环。

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <vector>
 4 #include <cstdio>
 5 
 6 const int maxn = 100005;
 7 
 8 int father[maxn];
 9 int ans;
10 void init() {
11     for(int i=0;i<maxn;i++)
12         father[i] = i;
13     ans = 0;
14 }
15 
16 int find_set(int x) {
17     if(father[x]!=x) {
18         father[x] = find_set(father[x]);
19     }
20     return father[x];
21 }
22 
23 int main(int argc, char** argv) {
24     
25     //freopen("in.txt","r",stdin);
26     int a,b;
27     init();
28     while(scanf("%d",&a)!=EOF) {
29         if(a==-1) {
30             printf("%d\n",ans);
31             init();
32             continue;
33         }
34         scanf("%d",&b);
35         
36 //        printf("%d %d\n",a,b);
37 
38         int fx = find_set(a);
39         int fy = find_set(b);
40         
41         if(fx==fy)
42         {
43             ans++;
44         }
45         father[fy] = fx;
46     }
47     
48     return 0;
49 }
View Code

 

转载于:https://www.cnblogs.com/TreeDream/p/6612426.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值