国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

PAT甲級1118

2019-11-08 19:33:15
字體:
來源:轉載
供稿:網友

1118. Birds in Forest (25)

時間限制150 ms內存限制65536 kB代碼長度限制16000 B判題程序Standard作者CHEN, Yue

Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in the same picture belong to the same tree. You are supposed to help the scientists to count the maximum number of trees in the forest, and for any pair of birds, tell if they are on the same tree.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive number N (<= 104) which is the number of pictures. Then N lines follow, each describes a picture in the format:K B1 B2 ... BKwhere K is the number of birds in this picture, and Bi's are the indices of birds. It is guaranteed that the birds in all the pictures are numbered continuously from 1 to some number that is no more than 104.

After the pictures there is a positive number Q (<= 104) which is the number of queries. Then Q lines follow, each contains the indices of two birds.

Output Specification:

For each test case, first output in a line the maximum possible number of trees and the number of birds. Then for each query, PRint in a line "Yes" if the two birds belong to the same tree, or "No" if not.

Sample Input:
43 10 1 22 3 44 1 5 7 83 9 6 4210 53 7Sample Output:
2 10YesNo
#include<cstdio>#include<algorithm>using namespace std;const int maxn = 10000 + 10;bool flag[maxn] = { false };int N, K, B;int father[maxn];int findFather(int x){	if (x == father[x])		return x;	else	{		int F = findFather(father[x]);		father[x] = F;		return F;	}}int main(){	for (int i = 0; i < maxn; i++)	{		father[i] = i;	}	scanf("%d", &N); int count = 0;	for (int i = 0; i < N; i++)	{		scanf("%d", &K);		int bf;		for (int j = 0; j < K; j++)		{			scanf("%d", &B);			if (!flag[B])			{				count++;				flag[B] = true;			}			if (j == 0)			{				bf = findFather(B);			}			else if(findFather(B) != findFather(bf))			{				father[findFather(B)] = findFather(bf);			}		}	}	int trees = 0;	for (int i = 1; i <= count; i++)	{		if (father[i] == i)		{			trees++;		}	}	printf("%d %d/n", trees, count);	int Q;	scanf("%d", &Q);	int a, b;	for (int i = 0; i < Q; i++)	{		scanf("%d %d", &a, &b);		if (findFather(a) == findFather(b))			printf("Yes/n");		else			printf("No/n");	}	return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 保山市| 那曲县| 丰镇市| 汉阴县| 江门市| 屯门区| 兴国县| 沭阳县| 宽城| 乌拉特中旗| 高安市| 青州市| 菏泽市| 景宁| 繁昌县| 黑龙江省| 哈巴河县| 拉萨市| 台北县| 桦南县| 乌苏市| 潞城市| 秭归县| 内江市| 平罗县| 日土县| 图木舒克市| 博野县| 镶黄旗| 泾阳县| 罗城| 攀枝花市| 潍坊市| 昌都县| 克什克腾旗| 晋城| 运城市| 龙里县| 哈密市| 江安县| 丘北县|