博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1118 Birds in Forest (25 分)
阅读量:6818 次
发布时间:2019-06-26

本文共 2165 字,大约阅读时间需要 7 分钟。

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:

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
 
#include 
using namespace std;int p[10010];int visit[10010];int a[10010];int n,m,k;int found(int a){ if(a==p[a]){ return a; } return p[a]=found(p[a]);}void unite(int a,int b){ int x = found(a); int y = found(b); if(x!=y){ p[x] = y; } return ;}bool istrue(int a,int b){ return found(a) == found(b);}int main(){ int x,y; set
s; for(int i=1;i<=10010;i++){ p[i] = i; } scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&m); for(int j=1;j<=m;j++) { scanf("%d",&a[j]); s.insert(a[j]); } for(int j=2;j<=m;j++) { unite(a[j-1],a[j]); } } int sum = s.size(); set
ss; for(int i=1;i<=sum;i++) { ss.insert(found(i)); } cout<
<<" "<
<

 

转载于:https://www.cnblogs.com/tonyyy/p/10545377.html

你可能感兴趣的文章
HNOI2015题解
查看>>
【项目实战】多线程环境下正确创建单例
查看>>
linux centos 7 nodejs 的安装
查看>>
mssqlserver分区表的左值与右值
查看>>
推荐系统
查看>>
HoloLens | 世界的每一次变化,其实都提前打好了招呼
查看>>
seq命令
查看>>
线性表接口的实现_Java
查看>>
利用安卓手机搭建WEB服务器
查看>>
小巧玲珑:机器学习届快刀XGBoost的介绍和使用
查看>>
intellij开发安卓与genymotion配合
查看>>
jmeter大神博客笔记
查看>>
java.lang.NoClassDefFoundError: javax/annotation/Priority
查看>>
skimage的安装,scikit-image
查看>>
springmvc-mvc:resource标签使用
查看>>
如何理解php的依赖注入
查看>>
洛谷P2084 进制转换
查看>>
直接上手从项目中学习很重要
查看>>
[转载]非常量引用的初始值必须为左值的问题
查看>>
C# 线程池执行操作例子
查看>>