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 ...
随机推荐
- Android项目部署时,发生AndroidRuntime:android.view.InflateException: Binary XML file line #168: Error inflating class错误
这个错误也是让我纠结了一天,当时写的项目在安卓虚拟机上运行都很正常,于是当我部署到安卓手机上时,点击登陆按钮跳转到用户主界面的时候直接结束运行返回登陆界面. 当时,我仔细检查了一下自己的代码,并 ...
- 学习和研究下unity3d的四元数 Quaternion
学习和研究下unity3d的四元数 Quaternion 今天准备学习和研究下unity3d的四元数 Quaternion 四元数在电脑图形学中用于表示物体的旋转,在unity中由x,y,z,w 表示 ...
- 嵌入式Linux驱动学习之路(二十)USB设备驱动
USB在接入系统的时候,以0的设备ID和主机通信,然后由主机为其分配新的ID. 在主机端,D+和D-都是下拉接地的.而设备端的D-接上拉时,表明此设备为高速设备:12M/s. D+接上拉时则是全速设备 ...
- quartz-1.8.5 Demo
最近在研究Spring中的定时任务功能,最好的办法当然是使用Quartz来实现.对于一个新手来说,花了我不少时间,这里我写个笔记,给大家参考. 我使用的是Maven来管理项目,需要的Jar包我给大家贴 ...
- 使用python实现appium的屏幕滑动
前些日子写一个滑动手机页面的小脚本,看到大家给的内容都是swipe方法,这里对swipe方法做一个小介绍: Swipe(int start x,int start y,int end x,int y, ...
- iis
IIS架构 1. 概述 为了提高IIS的可靠性,安全性以及可用性,与IIS5.0和以前更早的版本不同,IIS6.0提供了一个全新的IIS架构.这个架构的详细情况如下图所示: ...
- .net Global.asax文件使用
一.Application_start: 第一个访问网站的用户会触发该方法. 通常会在该方法里定义一些系统变量,如聊天室的在线总人数统计,历史访问人数统计的初始化等等均可在这里定义. Applicat ...
- IO(三)----序列流
SequenceInputStream 表示其他输入流的逻辑串联.它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件末尾,接着从第二个输入流读取,依次类推,直到到达包含的最后一个输入流的 ...
- 【OpenJudge 1793】矩形覆盖
http://noi.openjudge.cn/ch0405/1793/ 好虐的一道题啊. 看数据范围,一眼状压,然后调了好长时间QwQ 很容易想到覆盖的点数作为状态,我用状态i表示至少覆盖状态i表示 ...
- js ie中实现拖拽
获取鼠标移动信息 开始我们需要获取鼠标的坐标.我们添加一个document.onmousemove 就可以达到此目的: Javascript: document.onmousemove = mou ...