UVALive - 3644 - X-Plosives (并查集!!)

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

并查集!!

AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 100000 + 10;
int pa[maxn];

int find(int x)
{		//并查集的查找操作,带路径压缩
	return pa[x] != x ? pa[x] = find(pa[x]) : x;
}

int main()
{
	int x, y;
	while(scanf("%d", &x) != EOF)
	{
		for(int i = 0; i < maxn; i++) pa[i] = i;		//并查集首先要初始化
		int ref = 0;
		while(x != -1)
		{
			scanf("%d", &y);
			x = find(x); y = find(y);		//这之后,x,y分别是两个集合的代表元
			if(x == y) ref++;				//如果x和y代表同一个集合,则拒绝
			else pa[x] = y;					//否则合并,注意这里不是启发式合并
			scanf("%d", &x);
		}
		printf("%d\n", ref);
	}
	return 0;
}
时间: 2024-09-30 14:23:42

UVALive - 3644 - X-Plosives (并查集!!)的相关文章

UVALive 4487 Exclusive-OR 加权并查集神题

已知有 x[0-(n-1)],但是不知道具体的值,题目给定的信息 只有 I P V,说明 Xp=V,或者 I P Q V,说明 Xp ^ Xq=v,然后要求回答每个询问,询问的是 某任意的序列值 Xp1^Xp2,,,,X^pk 这个题目用加权并查集是这么处理的: 1. f[]照样是代表父节点,照样进行路径压缩,把每个 V[i]=V[i]^V[f[i]],即节点存储的值实际是它与它父亲的异或的值.为什么要这样呢,因为异或首先满足交换律,而且异或同一个数偶数次,即相当于本身,那么这个题目的其中一个要

(DS 《算法竞赛入门经典》)LA 3644 X-Plosives(并查集)

解题思路: 并查集 A secret service developed a new kind of explosive that attain its volatile property only when a specificassociation of products occurs. Each product is a mix of two different simple compounds, to which wecall a binding pair. If N > 2, then

UVALive 4730 线段树+并查集

点击打开链接 题意:在坐标上给n个点,r的操作是将两个点连起来,l的操作是问你y=u的这条线连接的集合块数和这些集合内的点的个数 思路:很麻烦的一道题,在网上看了题意和做法后,开始了一下午的调bug过程,做法很好懂,我开了两个线段树,一个维护点代表的直线的集合个数,另一个则是路过集合内的点的个数,然后集合的判断直接用并查集就行了,这是两个核心,然后就是自己瞎写的了,代码丑的可以而且好像除了本人别人看着可能要骂人了,有兴趣研究的可以留言我来解答,那难的部分其实就是并查集合并时该怎么将这两个要维护的

UVALive 6091 - Trees (并查集)

题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4102 ----------------------------------------------------------------------------------------------------------------------------------

UVALive 4730 Kingdom 线段树+并查集

题目链接:点击打开链接 题意见白书P248 思路: 先把读入的y值都扩大2倍变成整数 然后离散化一下 用线段树来维护y轴 区间上每个点的 城市数量和联通块数量, 然后用并查集维护每个联通块及联通块的最大最小y值,还要加并查集的秩来记录每个联通块的点数 然后就是模拟搞.. T^T绝杀失败题..似乎数组开小了一点就过了,== #include<stdio.h> #include<math.h> #include<vector> #include<string.h>

[LA] 3644 - X-Plosives [并查集]

A secret service developed a new kind of explosive that attain its volatile property only when a specicassociation of products occurs. Each product is a mix of two di?erent simple compounds, to which wecall a binding pair. If N > 2, then mixing N di?

UVALive - 3644X-Plosives(并查集)

题目:UVALive - 3644X-Plosives(并查集) 题目大意:给出K个简单的化合物,正好包含K种元素,那么将它们装车的时候,已经拿到的化合物再来的时候就应该拒绝装车,安全起见,然后给你装车的化合物列表,问你需要拒绝装车的次数. 解题思路:并查集.将已经装过的化合物记录下来,那么如果下次的化合物如果已经在集合中了,就说明需要拒绝装车. 代码: #include <cstdio> #include <cstring> const int maxn = 1e5 + 5; i

UVALive - 3027Corporative Network(带权并查集)

题目: UVALive - 3027Corporative Network(带权并查集) 题目大意:有n和节点,初始时每个节点的父节点都不存在,然后有下面两种操作:I 操作 I a,b 将a的父节点变成b.E操作 E a,查询a到它的父节点的距离. 解题思路:带权并查集.注意这里距离的变化是a -> b,那么a到根节点的距离就是a到b的距离的绝对值 % 1000 + b到它的根节点的距离. 代码: #include <cstdio> #include <cstring> #i

UVALive - 3027 - Corporative Network (并查集!!)

UVALive - 3027 Corporative Network Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A very big corporation is developing its corporative network. In the beginning each of the N enterprises of the cor