博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
X-Plosives UVA - 1160 (并查集,判断是否成环)
阅读量:6657 次
发布时间:2019-06-25

本文共 2718 字,大约阅读时间需要 9 分钟。

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

 

简单说就是判断是否成环

在每次合并时判断是否根节点相同即可,如相同则不加

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define int long long#define endl '\n'#define sc(x) scanf("%lld",&x)using namespace std;const int size=1e5+5;int fa[size];vector
u,v;void init(int n){ for(int i=1;i<=n;i++) { fa[i]=i; }}int find(int x){ return x==fa[x]?x:fa[x]=find(fa[x]);}int merge(int x,int y){ int fx=find(x),fy=find(y); if(fx==fy) return 1; fa[fx]=fy; return 0;} int32_t main(){ int a,b; int n=0; int cnt=0; while(~sc(a)) { if(a==-1) { init(n); int ans=0; for(int i=0;i
n) n=t; } return 0;}

 

转载于:https://www.cnblogs.com/fly-white/p/10092761.html

你可能感兴趣的文章
从 datetime2 数据类型到 datetime 数据类型的转换产生一个超出范围的值
查看>>
HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)
查看>>
Sensor types
查看>>
CodeForces 35D Animals
查看>>
Atitit.工作流 与 规则引擎
查看>>
文字排版--斜体(font-style)
查看>>
使用ImageView
查看>>
sn 密钥注册
查看>>
IntelliJ IDEA安装、配置、测试
查看>>
[React] Using the classnames library for conditional CSS
查看>>
JStorm环境搭建
查看>>
代码保存好
查看>>
iOS: 字体样式
查看>>
hadoop程序MapReduce之MaxTemperature
查看>>
面试题16:反转链表
查看>>
iOS开发基础知识--碎片22
查看>>
【最终幻想15 国王之剑】制作介绍2:最大限度满足角色,背景和道具的要求
查看>>
Selenium Web 自动化 - 项目实战环境准备
查看>>
mysql乱码
查看>>
FT项目开发技术点(三)
查看>>