PAT

Nothing to fear


种一棵树最好的时间是十年前,其次是现在!

那些你早出晚归付出的刻苦努力,你不想训练,当你觉的太累了但还是要咬牙坚持的时候,那就是在追逐梦想,不要在意终点有什么,要享受路途的过程,或许你不能成就梦想,但一定会有更伟大的事情随之而来。 mamba out~


人一我十,人十我百,追逐青春的梦想,怀着自信的心,永不言弃!

Birds in Forest

考点: 裸并查集

题目大意

就是裸的并查集没什么特别的考点

分析

需要注意的点:

完整代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;
const int N = 10005;
int f[N] , n , q,vis[N];
int tot , tree , t[N];
int find(int k)
{
	if(k == f[k])return k;
	else return f[k] = find(f[k]);
}
int Union(int a , int b)
{
	int A = find(a), B = find(b);
	f[A] = B;
}
int main()
{
	for(int i = 0;i < N;i ++)f[i] = i;
	cin >> n;
	for(int i = 0;i < n;i ++)
	{
		int k;cin >> k;
		for(int j = 0;j < k;j ++)
		{
			scanf("%d", &t[j]);
			if(vis[t[j]])continue;
			vis[t[j]] = 1;tot++;
		}	
		for(int j = 0;j < k - 1;j ++)
			Union(t[j] , t[j + 1]);
	}
	for(int i = 0;i < N;i ++){
		if(vis[i] && find(i) == i)tree++;
	}
	cout << tree << " " << tot << endl;
	cin >> q;int a , b;
	for(int i = 0;i < q;i ++)
	{
		cin >> a >> b;
		if(find(a) != find(b))cout << "No" << endl;
		else cout << "Yes" << endl;
 	}
	return 0;
}
原文地址:https://www.cnblogs.com/wlw-x/p/13322900.html