146 - 亲戚

时间限制:1秒 内存限制:128兆


<dl class="detail" style="color&#58;rgb&#40;51&#44;51&#44;51&#41;&#59;font&#45;family&#58;&#39;Helvetica Neue&#39;&#44; Helvetica&#44; Arial&#44; sans&#45;serif&#59;font&#45;size&#58;14px&#59;"> <dt style="line&#45;height&#58;1&#46;42857&#59;text&#45;transform&#58;uppercase&#59;font&#45;size&#58;18px&#59;"> 题目描述 </dt> <dd style="line&#45;height&#58;1&#46;7em&#59;font&#45;size&#58;1&#46;465em&#59;font&#45;family&#58;&#39;minion&#45;pro&#45;1&#39;&#44; &#39;minion&#45;pro&#45;2&#39;&#44; Palatino&#44; Georgia&#44; &#39;Times New Roman&#39;&#44; serif&#59;">

或许你并不知道,你的某个朋友是你的亲戚。他可能是你的曾祖父的外公的女婿的外甥女的表姐的孙子。如果能得到完整的家谱,判断两个人是否是亲戚应该是可行的,但如果两个人的最近公共祖先与他们相隔好几代,使得家谱十分庞大,那么检验亲戚关系实非人力所能及。在这种情况下,最好的帮手是计算机。
    为了将问题简化,你将得到一些亲戚关系的信息,如同Xuebin和Grant是亲戚,Grant和Tension是亲戚等,从这些信息中,你可以推出xuebin和Tension是亲戚。请写一个程序,对于我们的关于亲戚关系的提问,以最快的速度给出答案。

</dd> <dt style="line&#45;height&#58;1&#46;42857&#59;text&#45;transform&#58;uppercase&#59;font&#45;size&#58;18px&#59;"> 输入 </dt> <dd style="line&#45;height&#58;1&#46;7em&#59;font&#45;size&#58;1&#46;465em&#59;font&#45;family&#58;&#39;minion&#45;pro&#45;1&#39;&#44; &#39;minion&#45;pro&#45;2&#39;&#44; Palatino&#44; Georgia&#44; &#39;Times New Roman&#39;&#44; serif&#59;">

输入由两部分组成:
第一部分以N、M开始。N为问题涉及到的人的个数(1<N<20000)。这些人的编号为1、2、3、…、N。下面有M行(1<M<1000000),每行有两个数ai、bi,表示已知ai和bi是亲戚。
第二部分以Q开始。以下Q行有Q个询问(1<Q<1000000),每行为ci、di,表示询问ci和di是否为亲戚。

</dd> <dt style="line&#45;height&#58;1&#46;42857&#59;text&#45;transform&#58;uppercase&#59;font&#45;size&#58;18px&#59;"> 输出 </dt> <dd style="line&#45;height&#58;1&#46;7em&#59;font&#45;size&#58;1&#46;465em&#59;font&#45;family&#58;&#39;minion&#45;pro&#45;1&#39;&#44; &#39;minion&#45;pro&#45;2&#39;&#44; Palatino&#44; Georgia&#44; &#39;Times New Roman&#39;&#44; serif&#59;">
输出有若干行,
对于每个询问ci、di,若ci和di为亲戚,则输出Yes,否则输出No。
</dd> <dt style="line&#45;height&#58;1&#46;42857&#59;text&#45;transform&#58;uppercase&#59;font&#45;size&#58;18px&#59;"> 样例输入 </dt> <dd style="line&#45;height&#58;1&#46;7em&#59;font&#45;size&#58;1&#46;465em&#59;font&#45;family&#58;&#39;minion&#45;pro&#45;1&#39;&#44; &#39;minion&#45;pro&#45;2&#39;&#44; Palatino&#44; Georgia&#44; &#39;Times New Roman&#39;&#44; serif&#59;">
10 7
2 4
5 7
1 3
8 9
1 2
5 6
2 3
3
3 4
7 10
8 9
</dd> <dt style="line&#45;height&#58;1&#46;42857&#59;font&#45;weight&#58;bold&#59;text&#45;transform&#58;uppercase&#59;font&#45;size&#58;18px&#59;color&#58;rgb&#40;51&#44;51&#44;51&#41;&#59;font&#45;family&#58;&#39;Helvetica Neue&#39;&#44; Helvetica&#44; Arial&#44; sans&#45;serif&#59;"> 样例输出 </dt> <dd style="line&#45;height&#58;1&#46;7em&#59;font&#45;size&#58;1&#46;465em&#59;font&#45;family&#58;&#39;minion&#45;pro&#45;1&#39;&#44; &#39;minion&#45;pro&#45;2&#39;&#44; Palatino&#44; Georgia&#44; &#39;Times New Roman&#39;&#44; serif&#59;color&#58;rgb&#40;51&#44;51&#44;51&#41;&#59;">
Yes
No
Yes
</dd> </dl>


这道题并查集思路就是将同一集合中元素都归到第一个元素

#include<iostream>
using namespace std;

const int maxn = 20050;
int relative[maxn];

int judge(int a)
{
	if (relative[a] == a)
		return a;
	else
		//return judge(relative[a]);
		return relative[a] = judge(relative[a]);
		//这样直接出结果,比直接return judge(relative[a])节约时间,
}

int main(void)
{
	int n, p;
	cin >> n >> p;
	for (int i = 0; i < n; i++)
		relative[i] = i;

	int a, b;
	for (int i = 0; i < p; i++) {
		cin >> a >> b;
		a = judge(a);
		b = judge(b);
		relative[a] = b;
	}

	int x, y, q;
	cin >> q;
	for (int i = 0; i < q; i++) {
		cin >> x >> y;
		x = judge(x);
		y = judge(y);
		if (x == y)
			cout << "Yes" << endl;
		else
			cout << "No" << endl;
	}

	return 0;
}