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. 使用Kubectl部署应用

    目录 使用Kubectl部署应用  Kubectl部署流程  部署一个简单的Demo网站  一旦运行了Kubernetes集群,就可以在其上部署容器化应用程序.因此在开始之前,我们需要先确保集群已经准 ...

  2. 在Word指定位置插入富文本域值(html文本)

    遇到此问题,首先想到的就是各种百度.结果度娘了一会并没有发现有用的有效的解决方法,哎,看来还得靠自己啊. 首先整理了下手头上的资源,一是HtmlAgilityPack,专门解析Html文本用的:二是我 ...

  3. 如何完美激活pycharm2019.2.2

    本号持续关注pycharm的更新,这不本月11号迎来新版本,为防走丢,请关注公众号,让我们携手并行!有道是"予人玫瑰手留余香",分享的确是件令人愉快的事,这也是我创建公众号的初心. ...

  4. springcloud(五):Spring Cloud 配置中心的基本用法

    Spring Cloud 配置中心的基本用法 1. 概述 本文介绍了Spring Cloud的配置中心,介绍配置中心的如何配置服务端及配置参数,也介绍客户端如何和配置中心交互和配置参数说明. 配置中心 ...

  5. jsp页面直接输出了html代码

    可能出现的情况: 1.修改web.xml中springMVC的过滤器路径如下: "/"与"/*区别" 其实/和/*都可以匹配所有的请求资源,但其匹配的优先顺序是 ...

  6. Nginx安装及详解

    Nginx简介: Nginx(发音engine x)专为性能优化而开发的开源软件,是HTTP.反向代理.邮件代理.TCP/UDP协议代理软件,由俄罗斯的作者Igor Sysoev开发,其最知名的优点是 ...

  7. Net基础篇_学习笔记_第十一天_面向对象(类)

    类语法:[public] class 类名{ 字段; 属性; 方法;}写好了一个类之后,我们需要创建这个类的对象,那么,我们管创建这个类的对象过程称之为类的实例化.使用关键字 new. this:表示 ...

  8. 记录一次oracle的坑

    背景:程序正常运行中,突然技术支持人员反映数据库数据好久没有增加,于是乎各种排查问题,但是一直没有找到原因,由于代码比较久,也不是本人所写,更气的是居然用的是oracle数据库,并且是通过java代码 ...

  9. linux无法用root账号ssh登录(putty)

    解决方法: 在kali打开终端,修改ssh配置文件. vi /etc/ssh/sshd_config 将PermitRootLogin, 改成PermitRootLogin yes.(允许用root登 ...

  10. .Net Core 添加 Swagger 支持

    1. NuGet  中添加 Swashbuckle.AspNetCore 2.添加 Startup 信息 将 Swagger 生成器添加到 Startup.ConfigureServices 方法中的 ...