| 比赛 | 
    20120712 | 
    评测结果 | 
    AAAAAAAAAA | 
    | 题目名称 | 
    爆炸化合物 | 
    最终得分 | 
    100 | 
    | 用户昵称 | 
    Citron酱 | 
    运行时间 | 
    0.271 s  | 
    | 代码语言 | 
    C++ | 
    内存使用 | 
    0.67 MiB  | 
    | 提交时间 | 
    2012-07-12 10:10:37 | 
显示代码纯文本
#include <cstdio>
#define I_F "explosion.in"
#define O_F "explosion.out"
const long MAXn=100000+1;
long c[MAXn];
long ans=0;
void Prework();
long Root(const long&);
inline void Union(const long&, const long&);
int main()
{
	long a,b;
	Prework();
	freopen(I_F,"r",stdin);
	scanf("%ld",&a);
	while (a>=0)
	{
		scanf("%ld",&b);
		if (Root(a)!=Root(b))
			Union(a,b);
		else
			++ans;
		scanf("%ld",&a);
	}
	freopen(O_F,"w",stdout);
	printf("%ld\n",ans);
	return 0;
}
void Prework()
{
	for (long i=0; i<MAXn; ++i)
		c[i]=i;
}
long Root(const long &x)
{
	if (x==c[x])
		return x;
	c[x]=Root(c[x]);
	return c[x];
}
inline void Union(const long &a, const long &b)
{
	c[Root(b)]=Root(a);
}