Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

[Submit]   [Go Back]   [Status]

Description

 

X-Plosives

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 explosiveFor 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. LetÕs 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. The input 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, 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

Source

Southwestern 2006-2007

每个物体看成一个点,每对物品看成一题边,如果出现环,就不能放。。。。。

 #include <iostream>
#include <cstdio>
#include <cstring> using namespace std; int father[],ran[],ans; int Init()
{
ans=;
for(int i=;i<=;i++) { ran[i]=,father[i]=i; }
} int find(int x)
{
if(x==father[x]) return x;
return father[x]=find(father[x]);
} void Union(int a,int b)
{
int x=find(a),y=find(b);
if(x==y) {ans++; return ;}
if(ran[x]<=ran[y])
{
father[x]=y;
ran[x]+=ran[y];
}
else
{
father[y]=x;
ran[y]+=ran[x];
}
return ;
} int main()
{
int x,y;
Init();
while(scanf("%d",&x)!=EOF)
{
if(x==-)
{
printf("%d\n",ans);
Init(); continue;
}
scanf("%d",&y);
Union(x,y);
}
return ;
}

UVALive 3644 X-Plosives的更多相关文章

  1. 【暑假】[实用数据结构]UVAlive 3644 X-Plosives

    UVAlive X-Plosives 思路:    “如果车上存在k个简单化合物,正好包含k种元素,那么他们将组成一个易爆的混合物”  如果将(a,b)看作一条边那么题意就是不能出现环,很容易联想到K ...

  2. UVALive - 3644 X-Plosives (并查集)

    思路:每一个product都可以作一条边,每次添加一条边,如果这边的加入使得某个集合构成环,就应该refuse,那么就用并查集来判断. AC代码: //#define LOCAL #include & ...

  3. UVALive - 3644 X-Plosives (并查集)

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

  4. X-Plosives

    uvaLive 3644:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&pa ...

  5. UVALive(LA) 3644 X-Plosives (并查集)

    题意: 有一些简单化合物,每个化合物都由两种元素组成的,你是一个装箱工人.从实验员那里按照顺序把一些简单化合物装到车上,但这里存在安全隐患:如果车上存在K个简单化合物,正好包含K种元素,那么他们就会组 ...

  6. UVALive - 4108 SKYLINE[线段树]

    UVALive - 4108 SKYLINE Time Limit: 3000MS     64bit IO Format: %lld & %llu Submit Status uDebug ...

  7. UVALive - 3942 Remember the Word[树状数组]

    UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...

  8. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

  9. 思维 UVALive 3708 Graveyard

    题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...

随机推荐

  1. [转]如何在ASP.NET Core中实现一个基础的身份认证

    本文转自:http://www.cnblogs.com/onecodeonescript/p/6015512.html 注:本文提到的代码示例下载地址> How to achieve a bas ...

  2. Hibernate 延迟加载和立即加载

    概念 什么是延迟加载:所谓延迟加载就是当在真正需要数据的时候,才真正执行数据加载操作.可以简单理解为,只有在使用的时候,才会发出sql语句进行查询,数据是分N次读取. 什么是立即加载:所谓立即加载既是 ...

  3. java读取excel文件

    package com.execl; import java.io.File; import java.io.FileInputStream; import java.io.IOException; ...

  4. 环境变量NLS_LANG

    NLS_LANG是一个环境变量,用于定义语言,地域以及字符集属性.对于非英语的字符集,NLS_LANG的设置就非常重要.NLS:‘National Language Support (NLS)’ 当我 ...

  5. [No000085]C#反射Demo,通过类名(String)创建类实例,通过方法名(String)调用方法

    using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Sy ...

  6. Java 8简明教程

    本文由 ImportNew 欢迎阅读我编写的Java 8介绍.本教程将带领你一步一步地认识这门语言的新特性.通过简单明了的代码示例,你将会学习到如何使用默认接口方法,Lambda表达式,方法引用和重复 ...

  7. Nginx Rewrite规则

    location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # 因为所有的地址都以 / 开头,所以这条规则将匹配 ...

  8. Mysql完全手册(笔记一,底层与内置函数)

    1.MySQL由五个主子系统组成.协同工作,这五个主子系统是: (1)查询引擎 (2)存储管理器 (3)缓冲管理器 (4)事务管理器 (5)恢复管理器 查询引擎: 这个子系统包含三个相互关联的部件: ...

  9. httpUrlConnection中文乱码

    public void getFeiInfo(String sessionId) throws IOException{ //发送的请求参数,发送的格式也是Json的 String requestSt ...

  10. 在intellj idea下用sbt的坑

    version : SBT 0.13.7 intellij 14 新建SBT项目以后无法编译,总是显示 Can not resolve symble stackoverflow 给出了暂时的解决办法. ...