X-Plosives +uvalive+并查集+水题

来源:互联网 发布:网络改造 编辑:程序博客网 时间:2024/05/22 21:28


A secret service developed a new kind of explosive that attain its volatile property only when a speci c

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 rst 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

解决方案:题目意思也就是要看看添加的过程有没有环,元素做节点,一个化合物就是一个边,可用并查集,若添加的那两个化合物的根节点一样的话,则必有环,弃之。

代码:

#include<iostream>#include<cstdio>using namespace std;int pa[1000000];int fa(int x){return x!=pa[x]?pa[x]=fa(pa[x]):x;}///递归回溯,查找操作,并压缩路径int main(){    int x,y;    while(~scanf("%d",&x)){    for(int i=0;i<1000000;i++){        pa[i]=i;    }    int cnt=0;    while(x!=-1){        scanf("%d",&y);        x=fa(x),y=fa(y);        if(x==y) cnt++;        else pa[x]=y;        scanf("%d",&x);    }    printf("%d\n",cnt);    }    return 0;}

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 奖虫账号和密码不匹配怎么办 开淘宝店余额宝里的钱怎么办 京东找不到所有宝贝的链接了怎么办 淘宝店链接改了标题没流量怎么办 淘宝手淘搜索自然流量少怎么办 4个月宝贝不食欲下降怎么办 淘宝商家预售时间到了不发货怎么办 淘宝修改类目被处置管控了怎么办 淘宝换手机号是已经注册了的怎么办 闲鱼违规把我淘宝店铺冻结了怎么办 开网店店主想买自己的东西怎么办 以前用微博登录头条的账号怎么办 公众号注册提示邮箱已被占用怎么办 学信网注册时出现邮箱被占用怎么办 手机淘宝店铺接不到买家消息怎么办 买了东西淘宝店铺没了怎么办 淘宝店铺直播间虚假交易违规怎么办 手机淘宝退款物流单号填错了怎么办 淘宝评价删除了卖家没有返现怎么办 想摆摊卖麻辣烫不知怎么摆怎么办 在淘宝卖东西买家退款不退货怎么办 在闲鱼网上买东西被骗了怎么办 开通花呗的手机号不用了怎么办 饿了么开业第一天没单量怎么办 淘宝新开店铺没有人来呀怎么办 刚开的淘宝店铺没有人买东西怎么办 开了一家龙虾店生意很淡怎么办? 新买的皮质小白鞋有点板脚怎么办 手机无效安装包与系统不兼容怎么办 发好的海参放进速冻后变小了怎么办 厨房地砖下面的下水管漏水怎么办? 埋在地下的暖气管漏水怎么办 安装个视频播放器视频全没了怎么办 奇迹mu圣导转职任务打不过怎么办 丈夫死后房产证过给妻子怎么办 学业与志愿活动冲突时你会怎么办 往宅正门前面有半园幕墙玻璃怎么办 模拟城市5石油抽干了怎么办 一户一宅我父亲没有宅基怎么办? 在日本坐新干线期间车票丢了怎么办 街电共享充电宝丢了怎么办