题目: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 分)相关推荐

  1. PAT甲级1118 Birds in Forest :[C++题解]并查集

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:并查集的合并和查询. 问:一张照片上的鸟如何合并?相邻的合并(笔者采用的方式)或者全合并到第一只鸟就行,遍历一遍.所有照片中的鸟,合并 ...

  2. 【题解】1118 Birds in Forest (25分)⭐⭐ 【并查集】

    [题解]1118 Birds in Forest (25分)⭐⭐ [并查集] 题解: 简单并查集,并一下查一下就好了,没学的同学抓紧学一下 经验小结: #include<bits/stdc++. ...

  3. PAT题解-1118. Birds in Forest (25)-(并查集模板题)

    如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...

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

    并查集合并 #include<iostream> using namespace std; const int MAX = 10010; int father[MAX],root[MAX] ...

  5. PAT甲级1009 Product of Polynomials (25分)

    PAT甲级1009 Product of Polynomials (25分) 题目: 题目分析:和之前有一道多项式相加很像,注意点是不仅仅数组系数会变,还可能会出现之前没有的指数,因此要开2001大小 ...

  6. 17冬第二题 PAT甲级 1141 Ranking of Institutions (25分) 有点鸡贼

    题目 After each PAT, the PAT Center will announce the ranking of institutions based on their students' ...

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

  8. PAT甲级-1021 Deepest Root(25分)

    题目:1021 Deepest Root(25分) 分析:找出以某个节点为根时,最深的根,这题可能会超时要用vector来表示二维数组 疑问:代码一是第二次写的超时了,代码二是第一次写的AC了,找不出 ...

  9. PAT甲级--1007 Maximum Subsequence Sum (25 分)

    题目详情 - 1007 Maximum Subsequence Sum (25 分) (pintia.cn) Given a sequence of K integers { N1​, N2​, .. ...

  10. PAT甲级 -- 1007 Maximum Subsequence Sum (25 分)

    Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to ...

最新文章

  1. 【从零开始的ROS四轴机械臂控制】(七)- ROS与arduino连接
  2. 利用ServletFileUpload组件上传文件
  3. 谁是创业板的支柱?兼驳《上海证券报》
  4. Android 混淆打包
  5. HDU5692(线段树+dfs序)
  6. 极兔收购百世快递,谁是赢家?
  7. vue 用jsp 还是html,如何在jsp页面里使用vue
  8. Python-关于正则表达式的总结
  9. python数据处理实战
  10. Flutter TextField 文本输入框的基本属性及详解
  11. APUE习题[实现dup2函数功能,不使用fcntl]
  12. Matlab R2014a安装教程
  13. 华为云CDN+芒果TV,是如何做出“云”榜样?
  14. (附源码)小程序 口腔诊所预约系统 毕业设计 201738
  15. Python+PyQt5构建电影天堂电影搜索工具
  16. ORBSLAM2 理论部分_高斯金字塔(二)
  17. Layer Norm
  18. Android常用库和插件避免重复造轮子(持续更新)
  19. html 拼图游戏,HTML5人物拼图游戏
  20. apache ii评分怎么评_如何正确进行APACHE II评分

热门文章

  1. TomExam开源在线考试系统
  2. 异常:java.lang.IllegalArgumentException: Result Maps collection already contains
  3. 【C语言】-- 指针
  4. python将图片变成水墨画
  5. 【架构】1304- 如何画出一张优秀的架构图
  6. 如何画出一张优秀的架构图(老鸟必备)
  7. UIPATH Timeout reached
  8. 大数据背景下互联网用户行为分析
  9. oracle安装先决条件检查全部失败,两种解决办法
  10. phython练习1