您的位置:首页 > 其它

LA 3644 - X-Plosives

2015-03-26 01:19 337 查看
题目:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1645

并查集,成环的不加入ref++。

#include<iostream>
using namespace std;
const int MAXN = 100000 + 10;
int F[MAXN];
int find(int t)
{
	if (F[t] == t) return t;
	return F[t] = find(F[t]);
}

int main()
{
	int n, m;	
	while (cin >> n)
	{
		for (int i = 0; i <= MAXN; i++)
			F[i] = i;
		int ref = 0;
		while (n!=-1)
		{
			cin >> m;
			n = find(n), m = find(m);
			if (n==m) ref++;
			else F
 = m;
			cin >> n;
		}
		cout << ref << endl;
	}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: