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. 架构实例之Demo_JSP_JavaBean

    架构实例之Demo_JSP_JavaBean 1.开发工具和开发环境      开发工具: MyEclipse10,JDK1.6.0_13(32位),Tomcat7.0(32位),mysql5.7.1 ...

  2. HAOI2015 泛做

    T1 有一棵点数为N的树,树边有边权.给你一个在0~N之内的正整数K,你要在这棵树中选择K个点,将其染成黑色,并将其他的N-K个点染成白色.将所有点染色后,你会获得黑点两两之间的距离加上白点两两之间的 ...

  3. linux解压包

    1.命令格式:tar[必要参数][选择参数][文件] 2.命令功能:用来压缩和解压文件.tar本身不具有压缩功能.他是调用压缩功能实现的 3.命令参数:必要参数有如下:-A 新增压缩文件到已存在的压缩 ...

  4. Python list

    序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. Python有6个序列的内置类型,但最常见的是列表和元组. 序列 ...

  5. Python 过算符和数据类型

    一.算术运算符 二.逻辑运算符

  6. BZOJ 2286 消耗战 (虚树+树形DP)

    给出一个n节点的无向树,每条边都有一个边权,给出m个询问,每个询问询问ki个点,问切掉一些边后使得这些顶点无法与顶点1连接.最少的边权和是多少.(n<=250000,sigma(ki)<= ...

  7. StackExchange.Redis通用封装类分享(转)

    阅读目录 ConnectionMultiplexer 封装 RedisHelper 通用操作类封 String类型的封装 List类型的封装 Hash类型的封装 SortedSet 类型的封装 key ...

  8. Type.IsContextful 说明

    首先看MSDN的说明: 中文:获取一个值,通过该值指示 Type 在上下文中是否可以被承载. 英文:Gets a value indicating whether the Type can be ho ...

  9. 20145208 《Java程序设计》第0周学习总结

    20145208 <Java程序设计>第0周学习总结 阅读心得 读了老师推荐的几个文章,虽然第四个文章"为什么一定要自学"报告资源不存在而无法阅读,其他的三篇文章都言之 ...

  10. SharePoint下载服务器资源

    使用IE浏览器