X-Plosives UVALive - 3644(并查集)

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. Lets 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.
Each test case 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, the output must follow the description below.
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

题目大意:有一些简单点的化合物,每个化合物都有两种元素组成,你是一个装箱工人,从实验员那里按照顺序依次把一些简单的化合物装到车上。但是这里存在一个安全隐患:如果车上存在k个化合物,正好包含k中元素,那么他们将组成一个易爆混合物。为了安全起见,当你拿到一个化合物时,如果它和车上化合物形成易爆化合物,你应当拒绝撞车,编程输出由多少个没有撞车的化合物。
思路:
好像是一个裸题,先将元素并起来,如果并起来的元素他们根节点相同,说明车上已经有这两种化合物了,再装上车可能会形成易爆物,这时候计数一下,这个输入格式有点无语。。。。
代码:

#include<iostream>
#define MAXN 100005
using namespace std;

int a,b;
int parent[MAXN];
int ans=0;
void defin()
{
	for(int i=1;i<=MAXN;i++){
		parent[i]=i;
	}
}
int FIND_X(int x)
{
	if(x!=parent[x]){
		parent[x]=FIND_X(parent[x]);
	}
	return parent[x];
}
void UNION_S(int a,int b)
{
	int x=FIND_X(a);
	int y=FIND_X(b);
	    if(x!=y){
			parent[x]=y;
		}else{
			ans++;
		}
}
int main()
{
	while(cin>>a&&a!=-1)
	{
		defin();
		ans=0;
		cin>>b;
		UNION_S(a,b);
		while(cin>>a&&a!=-1){
			cin>>b;
			UNION_S(a,b);
		}
		cout<<ans<<endl;
	}
} 
全部评论

相关推荐

03-03 14:54
河南大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# AI面会问哪些问题? #
24847次浏览 491人参与
# 中国电信笔试 #
31080次浏览 283人参与
# 米连集团26产品管培生项目 #
12951次浏览 285人参与
# 你的实习产出是真实的还是包装的? #
18817次浏览 330人参与
# 如果秋招能重来,我会____ #
96691次浏览 500人参与
# 春招至今,你的战绩如何? #
59982次浏览 543人参与
# 开放七大实习专项,百度暑期实习值得冲吗 #
14146次浏览 209人参与
# i人适合做什么工作 #
36914次浏览 124人参与
# 我是面试官,请用一句话让我破防 #
79511次浏览 219人参与
# 哪些公司真双非友好? #
69200次浏览 287人参与
# 金三银四,你的春招进行到哪个阶段了? #
21567次浏览 277人参与
# 找AI工作可以去哪些公司? #
7673次浏览 186人参与
# 从事AI岗需要掌握哪些技术栈? #
7676次浏览 251人参与
# 投递几十家公司,到现在0offer,大家都一样吗 #
339915次浏览 2165人参与
# 面试尴尬现场 #
220755次浏览 861人参与
# 五一之后,实习真的很难找吗? #
102797次浏览 584人参与
# 你做过最难的笔试是哪家公司 #
30108次浏览 193人参与
# 你小时候最想从事什么职业 #
159840次浏览 2072人参与
# 应届生第一份工资要多少合适 #
20483次浏览 84人参与
# 阿里笔试 #
176460次浏览 1302人参与
# 一张图晒出你司的标语 #
3821次浏览 72人参与
# 面试被问期望薪资时该如何回答 #
382459次浏览 2163人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务