PAT甲级-1118 Birds in Forest (25 分)
题目:1118 Birds in Forest (25 分)
分析:并查集,和1107 social cluster很像,本题要在findFa函数中进行路径压缩,否则有一个测试点会超时,并查集还是要多练,太菜了
#include <iostream>
#include<cstring>
#include<vector>
#include<map>
#include<math.h>
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define MAX 999999999
int n,m,k;
int fa[10001];
int bird[10001];
int ans[10001];
int findFa(int x)
{if(x == fa[x]) return x;return fa[x] = findFa(fa[x]);
}
void uni(int a,int b)
{fa[findFa(a)] = findFa(b);
}
int main()
{cin>>n;for(int i = 1; i <= 10001;i++)fa[i] = i;int num = 0;for(int i = 1; i <= n ;i++){int k;scanf("%d",&k);int fro ;for(int j = 0; j < k ; j++){int x;scanf("%d",&x);if(j == 0)fro = x;elseuni(x,fro);if(bird[x] == 0) num++;bird[x] = 1;}}int cnt = 0;for(int i = 1; i <= 10001;i++)if(fa[i] == i && bird[i] == 1)cnt++;cout<<cnt<<" "<<num<<endl;cin>>k;for(int i = 0 ; i < k ;i++){int a,b;cin>>a>>b;if(findFa(a) == findFa(b))cout<<"Yes\n";elsecout<<"No\n";}return 0;
}
PAT甲级-1118 Birds in Forest (25 分)相关推荐
- PAT甲级1118 Birds in Forest :[C++题解]并查集
文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:并查集的合并和查询. 问:一张照片上的鸟如何合并?相邻的合并(笔者采用的方式)或者全合并到第一只鸟就行,遍历一遍.所有照片中的鸟,合并 ...
- 【题解】1118 Birds in Forest (25分)⭐⭐ 【并查集】
[题解]1118 Birds in Forest (25分)⭐⭐ [并查集] 题解: 简单并查集,并一下查一下就好了,没学的同学抓紧学一下 经验小结: #include<bits/stdc++. ...
- PAT题解-1118. Birds in Forest (25)-(并查集模板题)
如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...
- PAT A 1118. Birds in Forest (25)【并查集】
并查集合并 #include<iostream> using namespace std; const int MAX = 10010; int father[MAX],root[MAX] ...
- PAT甲级1009 Product of Polynomials (25分)
PAT甲级1009 Product of Polynomials (25分) 题目: 题目分析:和之前有一道多项式相加很像,注意点是不仅仅数组系数会变,还可能会出现之前没有的指数,因此要开2001大小 ...
- 17冬第二题 PAT甲级 1141 Ranking of Institutions (25分) 有点鸡贼
题目 After each PAT, the PAT Center will announce the ranking of institutions based on their students' ...
- PAT甲级 -- 1009 Product of Polynomials (25 分)
This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: Each ...
- PAT甲级-1021 Deepest Root(25分)
题目:1021 Deepest Root(25分) 分析:找出以某个节点为根时,最深的根,这题可能会超时要用vector来表示二维数组 疑问:代码一是第二次写的超时了,代码二是第一次写的AC了,找不出 ...
- PAT甲级--1007 Maximum Subsequence Sum (25 分)
题目详情 - 1007 Maximum Subsequence Sum (25 分) (pintia.cn) Given a sequence of K integers { N1, N2, .. ...
- PAT甲级 -- 1007 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
最新文章
- 【从零开始的ROS四轴机械臂控制】(七)- ROS与arduino连接
- 利用ServletFileUpload组件上传文件
- 谁是创业板的支柱?兼驳《上海证券报》
- Android 混淆打包
- HDU5692(线段树+dfs序)
- 极兔收购百世快递,谁是赢家?
- vue 用jsp 还是html,如何在jsp页面里使用vue
- Python-关于正则表达式的总结
- python数据处理实战
- Flutter TextField 文本输入框的基本属性及详解
- APUE习题[实现dup2函数功能,不使用fcntl]
- Matlab R2014a安装教程
- 华为云CDN+芒果TV,是如何做出“云”榜样?
- (附源码)小程序 口腔诊所预约系统 毕业设计 201738
- Python+PyQt5构建电影天堂电影搜索工具
- ORBSLAM2 理论部分_高斯金字塔(二)
- Layer Norm
- Android常用库和插件避免重复造轮子(持续更新)
- html 拼图游戏,HTML5人物拼图游戏
- apache ii评分怎么评_如何正确进行APACHE II评分