A secret service developed a new kind of explosive that attain its volatile property only when a specific 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 first 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

题解:
  好久没有发博客了,写一道水题。

  首先k和k想到了图论中的环,把每个颜色看成一个点,一个元素看成边,如果出现环就显然不合法,所以并查集判环就可以了。

代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
#include <queue>
#define MAXN 100100
using namespace std;
int x,y,fa[MAXN]; int find(int x){
if(x!=fa[x]) fa[x]=find(fa[x]);
return fa[x];
} int main()
{
int x,y;
while(scanf("%d",&x)==){
for(int i=;i<=MAXN-;i++) fa[i]=i;
int cnt=;
while(x!=-){
scanf("%d",&y);
x=find(x),y=find(y);
if(x==y) cnt++;
else fa[x]=y;
scanf("%d",&x);
}
printf("%d\n",cnt);
}
return ;
}

UVA - 1160 X-Plosives的更多相关文章

  1. LA 3644 - X-Plosives ( 也即UVA 1160)

    LA看题 请点击:传送门 UVA 上也有这题 :UVA 1160 - X-Plosives 题目大意就是如果车上存在 k 个简单化合物,正好包含 k 种元素 ,那么它们将有危险,此时你应该拒绝装车. ...

  2. UVA 1569 Multiple

    题意: 给定m个1位数字,要求用这些数字组成n的倍数的最小数字,如果无法组成就输出0 分析: BFS,由于n最大5000,余数最多5000,利用余数去判重,并记录下路径即可 代码: #include ...

  3. UVA 1395 Slim Span

    题意: 要求的是所有生成树中最大边与最小边差值最小的那个. 分析: 其实可以利用最小瓶颈生成树,就是最小生成树这一性质,枚举原图的最小边,然后找相应生成树的最大边 代码: #include <i ...

  4. UVA 1160 - X-Plosives 即LA3644 并查集判断是否存在环

    X-Plosives A secret service developed a new kind ofexplosive that attain its volatile property only ...

  5. UVA - 11400 Lighting System Design

    题文: You are given the task to design a lighting system for a huge conference hall. After doing a lot ...

  6. UVA 1160 X-Plosives

    题意是一次装入物品,物品由两种元素组成,当遇到即将装入的物品与已经装入的物品形成k个物品,k种元素,跳过该物品的装入.可以将每种元素看成顶点,物品看成一条边.这样问题就转化为利用并查集求环的情况. 算 ...

  7. UVA - 1160(简单建模+并查集)

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

  8. uva 11324 The Largest Clique

    vjudge 上题目链接:uva 11324 scc + dp,根据大白书上的思路:" 同一个强连通分量中的点要么都选,要么不选.把强连通分量收缩点后得到SCC图,让每个SCC结点的权等于它 ...

  9. uva 11728 Alternate Task

    vjudge 上题目链接:uva 11728 其实是个数论水题,直接打表就行: #include<cstdio> #include<algorithm> using names ...

随机推荐

  1. 第二章(Kotlin基础)

    基本要素:函数和变量 函数 函数定义规则 函数通过关键字 fun 用来声明一个函数 参数的类型与函数返回类型写在它的名称后面,这和变量声明一样 函数可以定义在文件的最外层,不一定要把它放在类中 示例: ...

  2. 【Offer】[47] 【礼物的最大价值】

    题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 在一个m*n的棋盘的每一格都放有一个礼物,每个礼物都有一定的价值(价值大于0).你可以从棋盘的左上角开始拿格子里的礼物,并每次向左(以自 ...

  3. linux常用命令三

    linux常用命令三 系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 ...

  4. 使用ECMAScript 6 模块封装代码

    JavaScript 用"共享一切"的方法加载代码,这是该语言中最容易出错且最容易让人感到困惑的地方.其他语言使用诸如包这样的概念来定义代码作用域,但在 ECMAScript 6 ...

  5. 【第十篇】easyui-datagrid排序 (转)

    本文体验datagrid的排序. □ 思路 当点击datagrid的标题,视图传递给Controller的Form Data类似这样:page=1&rows=10&sort=Custo ...

  6. 一次误用CSRedisCore引发的redis故障排除经历

    前导 上次Redis MQ分布式改造完成之后, 编排的容器稳定运行了一个多月,昨天突然收到ETL端同事通知,没有采集到解析日志了. 赶紧进服务器看了一下,用于数据接收的receiver容器挂掉了, 尝 ...

  7. apache ignite系列(四):持久化

    ignite持久化与固化内存 1.持久化的机制 ignite持久化的关键点如下: ignite持久化可防止内存溢出导致数据丢失的情况: 持久化可以定制化配置,按需持久化; 持久化能解决在大量缓存数据情 ...

  8. Decommission Datanode

    Decommission Datanode就是把Datanode从HDFS集群中移除掉.那问题来了,HDFS在设计时就把诸如机器故障考虑进去了,能否直接把某台运行Datanode的机器关掉然后拔走呢? ...

  9. asp.net 导出excel带图片

    protected void btgua_Click(object sender, EventArgs e) { DataTable dt = ds.Tables[0]; if (dt != null ...

  10. 会用python把linux命令写一遍的人,进大厂有多容易?

    看过这篇<2000字谏言,给那些想学Python的人,建议收藏后细看!>的读者应该都对一个命令有点印象吧?没错,就是 linux 中经常会用到的 ls 命令. 文章中我就提到如何提升自己的 ...