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

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

1118. Birds in Forest (25) (并查集,注意順序)

2019-11-08 18:52:05
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

題目地址

https://www.patest.cn/contests/pat-a-PRactise/1118

題目描述

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 … BK where 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 7

Sample Output:

2 10YesNo

ac

#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <iostream>#include <string>#include <vector>#include <queue>#include <algorithm>#include <sstream>#include <list>#include <stack> #include <map> #include <set> #include <iterator> #include <unordered_map>using namespace std;const int INF = 0x7fffffff;typedef long long int LL;const int N = 10000 + 5;int n;int fa[N];int Find(int x){ if(x == fa[x]) return x; return fa[x] = Find(fa[x]);}void Merg(int x,int y){ int xx = Find(x); int yy = Find(y); if(xx < yy) { fa[yy] = xx; }else{ fa[xx] = yy; }}int main(){ //freopen("in.txt", "r" , stdin); while(scanf("%d", &n) != EOF) { for(int i=0;i<N;i++) fa[i] = i; set<int> se; for(int i=0;i<n;i++) { int k; int bi; scanf("%d", &k); vector<int> tmp; for(int j = 0;j<k;j++) { scanf("%d", &bi); se.insert(bi); tmp.push_back(bi); } sort(tmp.begin(), tmp.end()); for(int j = 1;j<k;j++) { if(Find(tmp[j]) != Find(tmp[0])){ Merg(tmp[j], tmp[0]); } } } set<int> se2; set<int>::iterator it = se.begin(); while(it != se.end()) { se2.insert(Find(*it)); ++it; } int m1 = se.size(); int m2 = se2.size(); printf("%d %d/n", m2, m1); int Q; scanf("%d", &Q); for(int i=0;i<Q;i++) { int a,b; scanf("%d%d", &a, &b); if(Find(a) == Find(b)) { printf("Yes/n"); }else{ printf("No/n"); } } } //printf("/n"); return 0;}
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 高阳县| 南通市| 商水县| 大城县| 孟州市| 昆山市| 额尔古纳市| 湘阴县| 西乌珠穆沁旗| 兴国县| 赣州市| 峡江县| 浙江省| 万全县| 水城县| 朝阳市| 同德县| 高台县| 岳西县| 平定县| 内丘县| 潞城市| 灵丘县| 河西区| 绥宁县| 湖南省| 湟源县| 三门峡市| 榆林市| 乐亭县| 遂溪县| 元氏县| 通江县| 临安市| 革吉县| 西丰县| 武邑县| 济宁市| 尉犁县| 渝北区| 鹤山市|