UVA 1160 - X-Plosives 即LA3644 并查集判断是否存在环
X-Plosives
A secret service developed a new kind ofexplosive that attain its volatile property only when a specific association ofproducts occurs. Each product is a mix of two different simple compounds, towhich we call abinding pair. If N>2, thenmixing N different binding pairs containing N simple compounds creates apowerful explosive.For example, the bindingpairs 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 guyin a delivery agency with one dangerous problem: receive binding pairs insequential order and place them in a cargo ship. However, you must avoidplacing in the same room an explosive association. So, after placing a set ofpairs, if you receive one pair that might produce an explosion with some of thepairs already in stock, you must refuse it, otherwise, you must accept it.
An example. Let’s assume you receive thefollowing sequence: A+B, G+B, D+F, A+E, E+G, F+H. You would accept the firstfour pairs but then refuse E+G since it would be possible to make the followingexplosive with the previous pairs: A+B, G+B, A+E, E+G (4 pairs with 4 simplecompounds). Finally, you would accept the last pair, F+H.
Compute thenumber 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 integersto 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. The input ends in aline with the number –1. You may assume that no repeated binding pairsappears in the input.
Output
For each test case, a single line with the number ofrefusals.
Sample Input
1 2
3 4
3 5
3 1
2 3
4 1
2 6
6 5
-1
Sample Output
3
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3601
题意: 有一些简单化合物 每个化合物由2中不同元素组成 然后按照顺序依次将这些化合物放进车里 但是如果车上存在k个简单化合物 且正好包含k中元素的话
那么他们将变成易爆的化合物 为安全起见 每当你拿到一个化合物的时候 如果它和已装车的化合物形成易爆化合物 你就应当拒绝装车 否则就应该装车
请输出有多少个化合物没有装车
思路:
注意题目要求k个简单化合物 且正好包含k中元素 是任意k个化合物 也就是说 只要车里存在任意k个化合物 如果他们含有元素也为k个 则不能装入这样的一个化合物
可以把每个元素看成顶点 一个化合物作为一条边 当整个图存在环的时候 组成环的边对应的化合物就是危险的 否则是安全的
判断是否会组成环 可以通过并查集 如果要添加的边 x y同时在同一个集合中 那么它将组成环 拒绝它 (参考刘汝佳 入门经典 )
#include<stdio.h>
#include<string.h>
const int size=100001;
int parent[size],rank[size],count;
void init()
{
int i;
for(i=0;i<size;i++)
{
parent[i]=i;
rank[i]=1;
}
count=0;
}
int find(int n)
{
return n==parent[n]?n:parent[n]=find(parent[n]);
}
void join(int a,int b)
{
if(rank[a]>rank[b])
{
parent[b]=a;
rank[a]+=rank[b];
}
else
{
parent[a]=b;
rank[b]+=rank[a];
}
}
int main()
{
int a,b;
init();
while(scanf("%d",&a)!=EOF)
{
if(a==-1)
{
printf("%d\n",count); init(); continue;
}
scanf("%d",&b);
a=find(a);
b=find(b);
if(a==b) count++;
else join(a,b);
}
return 0;
}
UVA 1160 - X-Plosives 即LA3644 并查集判断是否存在环的更多相关文章
- HDU - 1272 小希的迷宫 并查集判断无向环及连通问题 树的性质
小希的迷宫 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一 ...
- UVa 10129 Play on Words(并查集+欧拉路径)
题目链接: https://cn.vjudge.net/problem/UVA-10129 Some of the secret doors contain a very interesting wo ...
- hdu--1878--欧拉回路(并查集判断连通,欧拉回路模板题)
题目链接 /* 模板题-------判断欧拉回路 欧拉路径,无向图 1判断是否为连通图, 2判断奇点的个数为0 */ #include <iostream> #include <c ...
- HDU - 5438 Ponds(拓扑排序删点+并查集判断连通分量)
题目: 给出一个无向图,将图中度数小于等于1的点删掉,并删掉与他相连的点,直到不能在删为止,然后判断图中的各个连通分量,如果这个连通分量里边的点的个数是奇数,就把这些点的权值求和. 思路: 先用拓扑排 ...
- P1197 [JSOI2008]星球大战(并查集判断连通块+正难则反)
P1197 [JSOI2008]星球大战(并查集判断连通块+正难则反) 并查集本来就是连一对不同父亲的节点就的话连通块就少一个. 题目描述 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统 ...
- UVA 1395 苗条的生成树(最小生成树+并查集)
苗条的生成树 紫书P358 这题最后坑了我20分钟,怎么想都对了啊,为什么就wa了呢,最后才发现,是并查集的编号搞错了. 题目编号从1开始,我并查集编号从0开始 = = 图论这种题真的要记住啊!!题目 ...
- Uva 12361 File Retrieval 后缀数组+并查集
题意:有F个单词,1 <= F <=60 , 长度<=10^4, 每次可以输入一个字符串,所有包含该字串的单词会形成一个集合. 问最多能形成多少个不同的集合.集合不能为空. 分析:用 ...
- UVA 3027 Corporative Network 带权并查集、
题意:一个企业要去收购一些公司把,使的每个企业之间互联,刚开始每个公司互相独立 给出n个公司,两种操作 E I:询问I到I它连接点最后一个公司的距离 I I J:将I公司指向J公司,也就是J公司是I公 ...
- HDU HDU1558 Segment set(并查集+判断线段相交)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1558 解题报告:首先如果两条线段有交点的话,这两条线段在一个集合内,如果a跟b在一个集合内,b跟c在一 ...
随机推荐
- SQL Server索引进阶:第六级,标签
原文地址: Stairway to SQL Server Indexes: Level 6,Bookmarks 本文是SQL Server索引进阶系列(Stairway to SQL Server I ...
- Cookie获取、设置值
设置: HttpCookie cookie = new HttpCookie("cookieName"); cookie.Value = "name1" Htt ...
- HTML静态网页(框架)
1.frameset frameset最外层,使用时需要去除body改用frameset. <frameset rows="100,*" frameborder=" ...
- HTML静态网页(标签、表格)
HTML静态网页: 打开DREAMWEAVER,新建HTML,如下图: body的属性: bgcolor 页面背景色 background 背景壁纸.图片 text 文字颜色 topmargin ...
- (转)CreateThread与_beginthreadex本质区别
本文将带领你与多线程作第一次亲密接触,并深入分析CreateThread与_beginthreadex的本质区别,相信阅读本文后你能轻松的使用多线程并能流畅准确的回答CreateThread与_beg ...
- BZOJ 1878: [SDOI2009]HH的项链( BIT )
离线处理 , 记下询问的左右端点并排序 , 然后可以利用树状数组 , 保证查询区间时每种颜色只计算一次 ------------------------------------------------ ...
- Linux中ssh的免密码登陆
原理: Hadoop的各个节点要实时的进行各种通信的,ssh就是能让各个节点免密码的相互访问相互通信. 操作步骤: 这里用的加密方式是非对称的加密方式,具体的操作是: <1>执行命令ssh ...
- php开发中的一些常用统计的日期
<?php echo '<br>今天:<br>'; echo date("Y-m-d",strtotime("now")), &q ...
- HDU1171-Big Event in HDU
描述: Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don ...
- FreeBSD 10安装KDE桌面环境简介(亲测bsdconfig命令有效)
FreeBSD 10出来一段时间了,自己摸索装上KDE环境,网上介绍的都是10以前版本的,要么对现在的不合适,走了一大圈弯路还是装不好:要么太繁琐且装了一堆无用的软件.本着让更多人能快速方面的入门Fr ...