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. Clementine 12.0 的使用安装(数据挖掘)

    1.下载[统计数据挖掘工具].TLF-SOFT-SPSS_Clementine_v12.0-CYGiSO.bin 2.下载虚拟光驱安装软件 本人使用的是DTLite4402-0131. 3.如果需要汉 ...

  2. SQL基础常用语法

    一.基础 1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 DROP database dbname 3.说明:创建新表 create table ...

  3. Android 开发笔记“调用.net webservice遇到的问题”

    1.An exception occurred: org.ksoap2.SoapFault SoapFault - faultcode: 'soap:Server' faultstring: '服务器 ...

  4. 使得fiddler来抓包查看微信浏览器的网页源码

    需要工具:http://www.telerik.com/fiddler 下载安装后 第二步: 打开这个选项: 设置代理:allow remote computer to connect  端口为888 ...

  5. RAW模板开发--入口文件官方规范

    每个人都有自己的习惯,为了RAW模板能进一步推广,使RAW模板的开发也更有条理,所以写了这个文章. 规范1格式: <?php /*RAW标记声明-start*/ /*RAW标记声明-over*/ ...

  6. bzoj 1040: [ZJOI2008]骑士 树形dp

    题目链接 1040: [ZJOI2008]骑士 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3054  Solved: 1162[Submit][S ...

  7. poj 2773 Happy 2006 容斥原理+二分

    题目链接 容斥原理求第k个与n互质的数. #include <iostream> #include <vector> #include <cstdio> #incl ...

  8. Loadrunner11点击录制脚本无响应,IE页面弹不出——解决方案汇总

    以前用Loadrunner的时候都没有遇到过这个问题,后来将服务器重装系统(win7)后,重新安装Loadrunner11,浏览器版本刚开始为IE11,后来降为IE8,IE访问部署在虚拟机里的平台能正 ...

  9. cout输出流的执行顺序

    一道题目: #include <iostream> using namespace std; ; template<typename T> int foo() { int va ...

  10. docpad建站记录

    记一下用docpad建站的过程作为备忘.不定时更新 why docpad wordpress对我来说太过于臃肿,我就想要个代码干净的小站来写东西.想要个markdown为基础的静态站. 比较流行的St ...