pat 1118. Birds in Forest (25) 并查集

STM32N6570-DK ISP调试 将下载线接入STLINK V3EC接口,将BOOT1拨码拨至右边,即将其置为H,上电或已经上电的按复位,打开STM32CubeProgrammer,设置start address:0x70000000。板子消耗电流较大,即使缩短数据线接了USB供电依然不能正常点亮显示屏,最终通过板上CN2端子加了外部5V供电,仍然需要560mA的外部电流。3.烧录bin文件:STM32N6_ISP_IQTune_App_revC01-v1.1.0-trusted.bin。 阅读详情

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 ... 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:
4
3 10 1 2
2 3 4
4 1 5 7 8
3 9 6 4
2
10 5
3 7
Sample Output:
2 10
Yes
No


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

using namespace std;

int N, Q;
int f[10005], a[10005];
int maxn, minn;
set<int> s;

int getf(int x) {
	return f[x] == x ? x : f[x] = getf(f[x]);
}

int main()
{
	cin >> N;
	for (int i = 1; i <= 10003; i++) {
		f[i] = i;
	}
	maxn = -1;
	for (int i = 0; i < N; i++) {
		int K;
		scanf("%d", &K);
		minn = 99999;
		for (int j = 0; j < K; j++) {
			scanf("%d", &a[j]);
			maxn = max(maxn, a[j]);
			minn = min(minn, a[j]);
		}
		for (int j = 0; j < K; j++) {
			f[getf(a[j])] = getf(minn);  //debug f[a[j]] -> f[getf(a[j])]
		}
	}
	for (int i = 1; i <= maxn; i++) {
		s.insert(getf(i));
	}
	printf("%d %d\n", s.size(), maxn);
	cin >> Q;
	while (Q--) {
		int x, y;
		scanf("%d%d", &x, &y);
		if (getf(x) == getf(y)) {
			puts("Yes");
		}
		else {
			puts("No");
		}
	}
	return 0;
}




单片机通识之UART通信协议(如何用汇编实现) 使用汇编实现UART通信 阅读详情

相关推荐

AI应用架构师实战:高等教育智能课程推荐系统开发

性能:选课季并发量达1000QPS,推荐响应时间≤500ms(否则学生不耐烦)。数据隐私:符合《教育数据安全管理规范》,学生姓名、学号等敏感信息需匿名化存储。可扩展性:支持新增课程类型(如MOOC、实践课)、新增数据源(如竞赛获奖记录)。教育AI要“以教为中心”:不能纯技术驱动,要和教育工作者深度合作(如教师参与定义课程的前置关系)。数据是核心:数据质量决定推荐效果,要重视数据治理和隐私保护。持续迭代:教育需求是动态的(如每年新增课程、学生目标变化),要定期收集用户反馈,更新模型和策略。张三。

AI大模型应用之禅 1365

【题解】【PAT甲】1118 Birds in Forest (25 分)(并查集)

题目链接 PTA | 程序设计类实验辅助教学平台 题目描述 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 tree..

Elephant_King的博客 301

开关电源拓扑结构选型指南:从Buck到Full-Bridge的实战解析

本文为硬件工程师提供了开关电源拓扑结构的实战选型指南。文章系统解析了从基础的Buck、Boost、Buck-Boost,到隔离型的Flyback、Forward,再到中高功率的半桥、全桥以及高效率的LLC谐振拓扑的适用场景与核心特点。通过对比分析各拓扑的优缺点,并结合输入输出电压、功率等级、成本与效率等关键参数,给出了清晰的选型决策路径,帮助工程师根据具体项目需求选择最合适的电源“骨架”。

weixin_29306261的博客 120

PAT甲级——1118 Birds in Forest (并查集)

1118Birds in Forest(25分) 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...

小熊猫的博客 363

PostgreSQL高可用之主备复制

多个PostgreSQL可协同工作,如果主库发生宕机备库将迅速进行接管并对外提供服务以实现高可用。本文将从主备复制、主备切换、故障转移等角度进行实际测试演练。第一章主备复制是基础,通过主备复制可以得到一个或多个相同的实例。在主备复制的基础上通过第二章主备切换介绍了如何手工将主库与备库互换角色。在第三章中,采用了Keepalived进行实时监测,在主库宕机后,使用主备切换中提到的命令使备库升级为主库并对外提供服务,以达到PostgreSQL高可用的目标。

asdasdsaff的博客 1354

xenomai内核解析--嵌入式实时linux概述

Linux系统作为一个GPOS(通用操作系统)发展至今已经非常成熟可靠了,并且由于遵循GPL协议,开放所有系统源代码,非常易于裁剪。更重要的是,与其他开源的GPOS或RTOS相比,Linux系统支持多种处理器、开发板,提供多种软件开发工具,同时Linux系统对网络和图形界面的支持非常出色。显然,选择Linux操作系统在产品的开发周期和成本控制方面都有巨大优势。 虽然Linux系统功能强大、实用性强...

wsg1100 3714

1118 Birds in Forest (25分)(map解决,非并查集常规方法)

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 maxi...

zbchenchanghao的博客 206

Langchain与Milvus集成中的pymilvus.exceptions.MilvusException: channel not found错误解析

本文深入解析了在Langchain与Milvus集成时常见的pymilvus.exceptions.MilvusException: channel not found错误。该错误通常由客户端配置错误、网络问题、服务端状态异常或版本不兼容导致。文章提供了从基础连通性测试到高级网络排查的完整故障排除指南,并针对本地Docker、远程服务器及Kubernetes等典型部署场景给出了具体解决方案,帮助开发者快速定位并修复这一连接层问题。

weixin_29229989的博客 225

1118 Birds in Forest (25point(s)) - C语言 PAT 甲级

并查集 * 边读取数据,边合并集合 * 最后有几个集合,就有几颗树 * 对于每次查询,直接返回并查集结果

有三只土豆 187

【滚雪球学数学建模】第4.1节·概率与统计建模,一文搞懂!

🎓 本文收录于 《滚雪球学数学建模》专栏,本专栏专注于从零基础出发,带你循序渐进掌握数学建模的核心方法与思维路径。通过“滚雪球式”学习法,从最简单的生活化案例到经典建模工具,再到省赛、国赛真题实战演练,逐步积累知识与技能,帮助你搭建完整的建模体系。无论你是刚接触建模的新生,还是希望提升科研与竞赛能力的学者与工程师,本专栏都将为你提供一条清晰、高效、可实操的建模成长路线,助力你快速进阶,突破难点,迈向数学建模竞赛与实际应用的更高峰 🚀!

**My Coding Family** 1384

从入门到精通:3D视觉与SLAM实战全流程 mastery 教程

本文系统介绍了3D视觉与SLAM技术的应用前景、核心技术和实战路径。3D视觉通过RGB-D相机、激光雷达等传感器获取环境三维信息,SLAM技术实现自主定位与地图构建,二者结合广泛应用于自动驾驶、机器人导航等领域。文章详细拆解了点云处理、视觉里程计、回环检测等关键技术,并提供了ORB-SLAM2算法复现、语义SLAM开发的实战教程,涵盖硬件选型、环境配置到代码实现全流程。最后规划了从入门到专家的学习路径,推荐了相关书籍、开源项目和数据集资源,为读者掌握这一前沿技术提供了完整指南。

m0_71589036的博客 157

PAT甲组1118 Birds in Forest思路解析和代码

A1118 题目链接 个人思路 本题考查并查集,不同的树当做不同的集合,选出一只鸟当做集合的根节点,属于同一集合的鸟进行合并;根节点相同,说明两只鸟属于同一棵树;不同根节点的个数表示树的个数;由于鸟的编号连续,最大编号即为鸟的个数 注意数据输入时合并,只能得到局部的根节点,后面需要再次调用findFather(),通过路径压缩更新全局根节点 个人思路代码 #include <bits/std...

九筒的博客 234

Debezium 性能调优实战:吞吐量、延迟与资源平衡

摘要: 本文分享了团队如何通过系统性调优将Debezium从初始3,200 TPS/2.8秒延迟提升至15,200 TPS/210ms延迟的实战经验。优化涵盖四个层面:1)Debezium连接器配置(批处理、快照并行、消息精简);2)Kafka分区策略与Avro序列化缓存;3)JVM改用ZGC及OS参数调优;4)MySQL的binlog与I/O优化。关键发现包括分区数黄金公式、sync_binlog调整带来56%的I/O负载下降,以及避免盲目增大队列内存等陷阱。最终实现吞吐量提升4.75倍,并强调性能调优需

爱丹丹的博客 365

【PAT】1118. Birds in Forest (25)【并查集】

题目描述 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...

tjj1998的博客 315

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

1118 Birds in Forest (25分)【关于PAT中使用并查集的坑点】

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 maxi...

爱写代码的坤坤的博客 295

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

【PAT】1118 Birds in Forest (25 分) - 并查集

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 maxi...

mdjfk的博客 197

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

1118. Birds in Forest (25)-PAT甲级真题(并查集)

1118. Birds in Forest (25) 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

柳婼 の blog 3360

PAT A1118 Birds in Forest (25 分)——并查集

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...

weixin_30376453的博客 141

PAT A1118 Birds in Forest (25 分) 并查集

题目大意:给出N张照片,每张照片上有一些鸟的编号(<10^4),同一张照片上的鸟被认为属于同一棵树,求出最大有多少棵树和多少只鸟,并判断给出的两只鸟是否属于同一棵树。 常规并查集问题,如果不用路径压缩的话测试点3会超时。 AC代码: #include <vector> #include <cstdio> using namespace std...

HackingGate的博客 225

【PAT甲级】1118 Birds in Forest (25 分)【并查集】

1118 Birds in Forest (25 分) 思路 输入输出: 第一行为照片数量nnn,接下来nnn行,每行第一个数字是 kkk,后面是 kkk 个数字,表示该照片中的鸟的编号 鸟的个数最多是10410^4104,nnn的个数最多是10410^4104,kkk的个数最多是10 qqq个查询,qqq最大可达10410^4104 输出的第一行是最大可能的树的数量与鸟的数量,接下来n行每行输出Yes/No表示给定的两个数是否在同一个树上 具体实现: 首先需要一定的并查集的知识,具体实现可见htt

郭同学如是说 167

PAT甲级 1118 Birds in Forest (25分) 并查集/C++

1118 Birds in Forest (25分) 题目大意:给定n张照片,每张照片中有k只鸟在同一棵树上,再给出几对鸟,判断它们是否在同一棵树上。 典型的并查集问题!不懂可以看中国大学mooc上浙江大学的《数据结构》课程(在树(下),讲的很好,也有代码讲解和完整的代码实现) #include<iostream> //输入输出流头文件 #include<vector> //变长数组容器 using namesp

qq_43955154的博客 146

PAT(并查集)——1118. Birds in Forest (25)

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 maxi...

WangXin Programmer 200

pat 甲级 并查集 A1118 Birds in Forest (25分)

1118 Birds in Forest (25分) 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 f

weixin_42358356的博客 162

PAT甲级 1118 Birds in Forest (25分) 并查集

题目中规中矩,没有什么难度,标准的并查集就能拿下。 #include<iostream> #include<vector> using namespace std; int father[10010]; int findFather(int x) { int a = x; while (x != father[x]) { x = father[x]; } while (a != father[a]) { int z = a; father[a] = x; .

zs201609的博客 152

PAT A1118. Birds in Forest (25)(并查集)

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 maxi

jolivan的博客 310

PAT - 甲级 - 1118. Birds in Forest (25) (并查集)

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 maxi

i逆天耗子 746
上一篇: pat甲级 1020. Tree Traversals map递归建树 + BFS
下一篇: pat 1127. ZigZagging on a Tree (30) 递归建树 + BFS
Daemoonn
博客等级 码龄11年 42粉丝 198原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址