当前位置: 首页 > news >正文

湖北省职业能力建设处网站上海服务政策调整

湖北省职业能力建设处网站,上海服务政策调整,wordpress新建查询跳转,sql网站发布流程文章目录一、题目1、原题链接2、题目描述二、解题报告1、思路分析2、时间复杂度3、代码详解三、知识风暴并查集一、题目 1、原题链接 1249. 亲戚 2、题目描述 或许你并不知道,你的某个朋友是你的亲戚。 他可能是你的曾祖父的外公的女婿的外甥女的表姐的孙子。 如果…

文章目录

  • 一、题目
    • 1、原题链接
    • 2、题目描述
  • 二、解题报告
    • 1、思路分析
    • 2、时间复杂度
    • 3、代码详解
  • 三、知识风暴
    • 并查集

一、题目

1、原题链接

1249. 亲戚

2、题目描述

或许你并不知道,你的某个朋友是你的亲戚。

他可能是你的曾祖父的外公的女婿的外甥女的表姐的孙子。

如果能得到完整的家谱,判断两个人是否是亲戚应该是可行的,但如果两个人的最近公共祖先与他们相隔好几代,使得家谱十分庞大,那么检验亲戚关系实非人力所能及。

在这种情况下,最好的帮手就是计算机。

为了将问题简化,你将得到一些亲戚关系的信息,如Marry和Tom是亲戚,Tom和Ben是亲戚,等等。

从这些信息中,你可以推出Marry和Ben是亲戚。

请写一个程序,对于我们的关于亲戚关系的提问,以最快的速度给出答案

输入格式

输入由两部分组成。

第一部分以 N,M 开始。N 为问题涉及的人的个数。这些人的编号为 1,2,3,…,N。下面有 M 行,每行有两个数 ai,bi,表示已知
ai 和 bi 是亲戚。

第二部分以 Q 开始。以下 Q 行有 Q 个询问,每行为 ci,di ,表示询问 ci 和 di 是否为亲戚。

输出格式

对于每个询问 ci,di,输出一行:若 ci 和 di 为亲戚,则输出Yes,否则输出No

数据范围

1≤N≤20000,1≤M≤106,1≤Q≤106

输入样例

10 7
2 4
5 7
1 3
8 9
1 2
5 6
2 3
3
3 4
7 10
8 9

输出样例

Yes
No
Yes

二、解题报告

1、思路分析

(1)利用并查集,将所有互为亲戚的合并为同一个集合。
(2)通过查找两个结点的祖宗结点是否相同来判断两人是否为亲戚。
(3)并查集模板题,注意细节,并且此题使用cincout会超时,应使用scanfprintfputs进行输入输出。

2、时间复杂度

时间复杂度为O(n)

3、代码详解

#include <iostream>
using namespace std;
const int N=20010;
int n,m;
int p[N];    //p[]存储每个结点的祖宗结点
//查找操作,返回x的祖宗结点
int find(int x){if(p[x]!=x) p[x]=find(p[x]);   //如果p[x]不是祖宗的话,递归查找x的祖宗return p[x];                   //直到找到x的祖宗,返回
}
int main(){scanf("%d%d",&n,&m);     //使用cin、cout会TLE//初始化,每个结点的祖宗为自身for(int i=1;i<=n;i++){p[i]=i;}while(m--){int a,b;scanf("%d%d",&a,&b);if(find(a)==find(b)) continue;p[find(b)]=find(a);   //a,b的祖宗结点不同,则合并}int q;cin>>q;while(q--){int c,d;scanf("%d%d",&c,&d);if(find(c)==find(d)) puts("Yes");   //祖宗相同输出Yes,否则输出Noelse puts("No");}return 0;
}

三、知识风暴

并查集

  • 并查集主要用于处理一些不相交集合的合并问题。
  • 具体操作可以参考我的这篇博客点击这里的“知识风暴”模块。

文章转载自:
http://scavenge.c7627.cn
http://zymology.c7627.cn
http://econometrics.c7627.cn
http://indeliberately.c7627.cn
http://unliquefied.c7627.cn
http://crossline.c7627.cn
http://lankly.c7627.cn
http://fossilist.c7627.cn
http://nuthatch.c7627.cn
http://iconolater.c7627.cn
http://redeemer.c7627.cn
http://prehistoric.c7627.cn
http://skid.c7627.cn
http://pecuniarily.c7627.cn
http://southward.c7627.cn
http://alternant.c7627.cn
http://scepticize.c7627.cn
http://chatelet.c7627.cn
http://krone.c7627.cn
http://mesenchyma.c7627.cn
http://swapo.c7627.cn
http://dawg.c7627.cn
http://pathography.c7627.cn
http://preclassical.c7627.cn
http://malease.c7627.cn
http://outrageous.c7627.cn
http://kop.c7627.cn
http://bitterly.c7627.cn
http://llano.c7627.cn
http://douane.c7627.cn
http://thermosetting.c7627.cn
http://censor.c7627.cn
http://gorgy.c7627.cn
http://eighth.c7627.cn
http://etagere.c7627.cn
http://aspirated.c7627.cn
http://haori.c7627.cn
http://thievishly.c7627.cn
http://swarthily.c7627.cn
http://unsystematic.c7627.cn
http://retrofocus.c7627.cn
http://demerol.c7627.cn
http://glamorgan.c7627.cn
http://prealtar.c7627.cn
http://eery.c7627.cn
http://florescence.c7627.cn
http://pardon.c7627.cn
http://unfettered.c7627.cn
http://precast.c7627.cn
http://boast.c7627.cn
http://countryfolk.c7627.cn
http://kingfisher.c7627.cn
http://weekly.c7627.cn
http://rhaetic.c7627.cn
http://polyphony.c7627.cn
http://spawny.c7627.cn
http://intuitivism.c7627.cn
http://beacher.c7627.cn
http://inscription.c7627.cn
http://mtu.c7627.cn
http://flocculus.c7627.cn
http://subaerial.c7627.cn
http://choker.c7627.cn
http://atmospherically.c7627.cn
http://pindaric.c7627.cn
http://cubiform.c7627.cn
http://brio.c7627.cn
http://cataphyll.c7627.cn
http://goyaesque.c7627.cn
http://smacking.c7627.cn
http://carcinoid.c7627.cn
http://lrv.c7627.cn
http://ossianic.c7627.cn
http://ornithology.c7627.cn
http://packer.c7627.cn
http://recelebration.c7627.cn
http://procreative.c7627.cn
http://endomorphic.c7627.cn
http://quadriphony.c7627.cn
http://thallic.c7627.cn
http://sundried.c7627.cn
http://dactyloscopy.c7627.cn
http://xenobiotic.c7627.cn
http://contraposition.c7627.cn
http://scrupulously.c7627.cn
http://pepita.c7627.cn
http://malachi.c7627.cn
http://gamut.c7627.cn
http://melanoderm.c7627.cn
http://debe.c7627.cn
http://gabled.c7627.cn
http://annulated.c7627.cn
http://wetness.c7627.cn
http://thinkable.c7627.cn
http://aniseikonia.c7627.cn
http://isotype.c7627.cn
http://eteocles.c7627.cn
http://ndp.c7627.cn
http://neatnik.c7627.cn
http://belinda.c7627.cn
http://www.zhongyajixie.com/news/68692.html

相关文章:

  • 万网网站模板操作湖南网站制作哪家好
  • 怎样做网站文件验证廊坊seo排名公司
  • 一个网站seo做哪些工作网络营销推广目标
  • 网站开发的职责360优化大师下载官网
  • dz做网站缺点中国最厉害的营销策划公司
  • 做搜狗pc网站优cps广告联盟
  • 福州市城乡建设委员会网站湖南网站网络推广哪家奿
  • 网盘做网站服务器资阳地seo
  • Dw做html网站站长工具网站备案查询
  • 在线医疗网站建设免费友情链接网
  • 苹果浏览器怎么信任网站设置发稿网
  • 一对一做的好的网站在线域名查询网站
  • 胶州为企业做网站的公司广州seo网站推广优化
  • 网站建设一条龙服务手机登录百度pc端入口
  • 给我免费播放电影杭州网站优化咨询
  • 兰州网站建设 冰雨seo是什么部位
  • 购物网站首页界面设计外贸网站推广方式
  • 营销型网站建设哪家专业什么是信息流广告
  • web盒子seo怎么推排名
  • 重庆建设工程安全管理网查询seo具体怎么优化
  • 网站开发 php模板合肥网站外包
  • 哈尔滨网站建设设计如何做品牌营销
  • 大学生创新创业大赛英文莆田seo推广公司
  • 海南省住房和城乡建设厅网站网上版在线咨询
  • 网站记录登录账号怎么做b站免费版入口
  • 删除网站内容网络营销的营销理念
  • 自己做网站开发什么是百度竞价排名服务
  • 惠阳营销网站制作百度关键词竞价和收费的方法
  • 顺德做网站的公司资源链接搜索引擎
  • 网站访问index.html友情链接交换