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 并查集判断是否存在环的更多相关文章

  1. HDU - 1272 小希的迷宫 并查集判断无向环及连通问题 树的性质

    小希的迷宫 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一 ...

  2. UVa 10129 Play on Words(并查集+欧拉路径)

    题目链接: https://cn.vjudge.net/problem/UVA-10129 Some of the secret doors contain a very interesting wo ...

  3. hdu--1878--欧拉回路(并查集判断连通,欧拉回路模板题)

     题目链接 /* 模板题-------判断欧拉回路 欧拉路径,无向图 1判断是否为连通图, 2判断奇点的个数为0 */ #include <iostream> #include <c ...

  4. HDU - 5438 Ponds(拓扑排序删点+并查集判断连通分量)

    题目: 给出一个无向图,将图中度数小于等于1的点删掉,并删掉与他相连的点,直到不能在删为止,然后判断图中的各个连通分量,如果这个连通分量里边的点的个数是奇数,就把这些点的权值求和. 思路: 先用拓扑排 ...

  5. P1197 [JSOI2008]星球大战(并查集判断连通块+正难则反)

    P1197 [JSOI2008]星球大战(并查集判断连通块+正难则反) 并查集本来就是连一对不同父亲的节点就的话连通块就少一个. 题目描述 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统 ...

  6. UVA 1395 苗条的生成树(最小生成树+并查集)

    苗条的生成树 紫书P358 这题最后坑了我20分钟,怎么想都对了啊,为什么就wa了呢,最后才发现,是并查集的编号搞错了. 题目编号从1开始,我并查集编号从0开始 = = 图论这种题真的要记住啊!!题目 ...

  7. Uva 12361 File Retrieval 后缀数组+并查集

    题意:有F个单词,1 <= F <=60 , 长度<=10^4, 每次可以输入一个字符串,所有包含该字串的单词会形成一个集合. 问最多能形成多少个不同的集合.集合不能为空. 分析:用 ...

  8. UVA 3027 Corporative Network 带权并查集、

    题意:一个企业要去收购一些公司把,使的每个企业之间互联,刚开始每个公司互相独立 给出n个公司,两种操作 E I:询问I到I它连接点最后一个公司的距离 I I J:将I公司指向J公司,也就是J公司是I公 ...

  9. HDU HDU1558 Segment set(并查集+判断线段相交)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1558 解题报告:首先如果两条线段有交点的话,这两条线段在一个集合内,如果a跟b在一个集合内,b跟c在一 ...

随机推荐

  1. URAL 1036(dp+高精度)

    Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Pract ...

  2. PHPExcel 导出

    <?php include '../init.inc.php'; include "../db.inc.php"; /* @func 引入类 */ include ROOT. ...

  3. C#文本文件读写

    算机在最初只支持ASCII编码,但是后来为了支持其他语言中的字符(比如汉字)以及一些特殊字符(比如€),就引入了Unicode字符集.基于Unicode字符集的编码方式有很多,比如UTF-.UTF-. ...

  4. codeforces 522D. Closest Equals 线段树+离线

    题目链接 n个数m个询问, 每次询问输出给定区间中任意两个相同的数的最近距离. 先将询问读进来, 然后按r从小到大排序, 将n个数按顺序插入, 并用map统计之前是否出现过, 如果出现过, 就更新线段 ...

  5. Vmware虚拟机时间不准问题

    测试程序时碰到虚拟机经常时间不准,深受困扰,后来发现虚拟机有一个设置可以同步虚拟机和宿主机的时间: 该功能需要vmware tools安装成功才能有效.vmware tools的安装就不多细说了,至于 ...

  6. [转]IOS 学习笔记(8) 滚动视图(UIScrollView)的使用方法

    下面介绍pageControl结合ScrollView实现连续滑动翻页的效果,ScrollView我们在应用开发中经常用到,以g这种翻页效果还是很好看的,如下图所示: 通过这个例子,我们重点学习UIS ...

  7. 有意思的C宏

    在Linux内核.嵌入式代码等传统的C代码里,会有一些难以识别的宏定义.我记得在eCos, UBoot, FFmpeg有一些比较BT的宏定义,很难读懂.对于C++程序员来说,最好将这种难读的宏定义转成 ...

  8. libcprops

    Install Howto Download the latest epel-release rpm from http://dl.fedoraproject.org/pub/epel/6/x86_6 ...

  9. 手动修改VisualStudio IISExpress的配置

    <VisualStudio> <FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}"> ...

  10. Protel99se轻松入门:特殊技巧和高级设置(一)

    这里简单介绍一下自动布线和手动布线方面的设置问题 1.如何进入PCB的这个布线规则选项: 2.电气安全距离的设置 3.导线宽度的设置 4.学会了设置图层就可以做单面板以及多层板,而不只是双面板 5.布 ...