UVALive 3644 X-Plosives
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
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 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. 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的更多相关文章
- 【暑假】[实用数据结构]UVAlive 3644 X-Plosives
UVAlive X-Plosives 思路: “如果车上存在k个简单化合物,正好包含k种元素,那么他们将组成一个易爆的混合物” 如果将(a,b)看作一条边那么题意就是不能出现环,很容易联想到K ...
- UVALive - 3644 X-Plosives (并查集)
思路:每一个product都可以作一条边,每次添加一条边,如果这边的加入使得某个集合构成环,就应该refuse,那么就用并查集来判断. AC代码: //#define LOCAL #include & ...
- UVALive - 3644 X-Plosives (并查集)
A secret service developed a new kind of explosive that attain its volatile property only when a spe ...
- X-Plosives
uvaLive 3644:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&pa ...
- UVALive(LA) 3644 X-Plosives (并查集)
题意: 有一些简单化合物,每个化合物都由两种元素组成的,你是一个装箱工人.从实验员那里按照顺序把一些简单化合物装到车上,但这里存在安全隐患:如果车上存在K个简单化合物,正好包含K种元素,那么他们就会组 ...
- UVALive - 4108 SKYLINE[线段树]
UVALive - 4108 SKYLINE Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- 思维 UVALive 3708 Graveyard
题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...
随机推荐
- 针对github权限导致hexo部署失败的解决方案
hexo deplay出错,没有反应 今天想在自己的另一个博客地址(链接地址)上更新一下博客,没想到hexo deplay没有反应,以下是解决过程:(更新于 2016-12-24 11:17:43) ...
- Machine Learning Algorithms Study Notes(5)—Reinforcement Learning
Reinforcement Learning 对于控制决策问题的解决思路:设计一个回报函数(reward function),如果learning agent(如上面的四足机器人.象棋AI程序)在决定 ...
- 【Bootstrap Demo】入门例子创建
本文简单介绍下如何来使用 Bootstrap,通过引入 Bootstrap,来实现一个最基本的入门例子. 在前一篇博文[Bootstrap]1.初识Bootstrap 基础之上,我们完全可以更加方便快 ...
- iOS 2D绘图 (Quartz2D)之阴影和渐变(shadow,Gradient)
原博地址:http://blog.csdn.net/hello_hwc/article/details/49507881 Shadow Shadow(阴影) 的目的是为了使UI更有立体感,如图 sha ...
- 如何使iframe透明
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- docker搭建ros-indigo-arm交叉编译环境
ROS运行环境:ARM ubuntu14.04 + ROS indigo在arm环境下编译ros应用程序,速度极慢,无法忍受,尝试在x86机器上搭建docker+ros交叉编译环境. 交叉编译环境的搭 ...
- 在Windows Server 2012 R2上安装SharePoint 2013 with SP1失败,提示没有.net4.5的解决办法
现在的Server用Windows Server 2012 R2的越来越多了,在部署带Sp1的SharePoint2013的时候,走完预安装工具后,点击setup提示缺少.net4.5. 其实Wind ...
- 1122MySQL性能优化之 Nested Loop Join和Block Nested-Loop Join(BNL)
转自http://blog.itpub.net/22664653/viewspace-1692317/ 一 介绍 相信许多开发/DBA在使用MySQL的过程中,对于MySQL处理多表关联的方式或者说 ...
- 结构体里的“位域”(bit-field)结构
首先看一个题目: #include <iostream> using namespace std; #include <string.h> typedef struct AA{ ...
- FFT小总结
FFT实质上做的是循环卷积,ck=sigam(ai*bj,(i+j)%n=k),其中n是倍长后的长度,所以我们有时候需要的只是普通的卷积,我们就需要把原数组倍长,再用FFT求卷积,由于高位都是0,所以 ...