当前位置:网站首页>【暑期每日一题】洛谷 P1551 亲戚
【暑期每日一题】洛谷 P1551 亲戚
2022-08-02 06:07:00 【AC_Dragon】
题目链接:P1551 亲戚 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
题目背景
若某个家族人员过于庞大,要判断两个是否是亲戚,确实还很不容易,现在给出某个亲戚关系图,求任意给出的两个人是否具有亲戚关系。
题目描述
规定:x 和 y 是亲戚,y 和 z 是亲戚,那么 x 和 z 也是亲戚。如果 x,y 是亲戚,那么 x 的亲戚都是 y 的亲戚,y 的亲戚也都是 x 的亲戚。
输入格式
第一行:三个整数 n,m,p,(n,m,p <= 5000),分别表示有 n 个人,m 个亲戚关系,询问 p 对亲戚关系。
以下 m 行:每行两个数 Mi,Mj,1 <= Mi,Mj<=N,表示 Mi 和 Mj 具有亲戚关系。
接下来 p 行:每行两个数 Pi,Pj,询问 Pi 和 Pj 是否具有亲戚关系。
输出格式
p 行,每行一个 Yes 或 No。表示第 i 个询问的答案为“具有”或“不具有”亲戚关系。
样例 #1
样例输入 #1
6 5 3
1 2
1 5
3 4
5 2
1 3
1 4
2 3
5 6
样例输出 #1
Yes
Yes
No
AC code:
#include<iostream>
#include<algorithm>
using namespace std;
int f[5010];
// 初始化
void init(int n)
{
for(int i=1;i<=n;i++)
f[i]=i;
}
int getf(int v)
{
if(f[v]==v)
return v;
else
{
f[v]=getf(f[v]);
return f[v];
}
}
void merge(int v,int u)
{
int t1,t2;
t1=getf(v);
t2=getf(u);
if(t1!=t2)
{
f[t2]=t1;
}
}
int main()
{
int sum=0;
int n,m,p;
cin>>n>>m>>p;
init(n);
while(m--)
{
int x,y;
cin>>x>>y;
merge(x,y);
}
while(p--)
{
int x,y;
cin>>x>>y;
if(getf(x)==getf(y))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
return 0;
}
边栏推荐
- GCC编译器技术解析
- abaqus如何快速导入其他cae文件的assembly?
- typescript 'props' is declared but its value is never read solution
- MySQL高级语句(一)
- 振兴农村循环经济 和数链串起农业“生态链”
- Xgboost报错 ValueError: Invalid shape: (1650, 2) for label
- APT + Transform to realize multi module Application distributed Application life cycle
- chrome 插件开发指南
- In-depth analysis of the initialization of member variables and local variables
- MySQL high-level --- storage engine, index, lock
猜你喜欢
optional
【npm install 报错问题合集】- npm ERR! code ENOTEMPTY npm ERR! syscall rmdir
abaqus如何快速导入其他cae文件的assembly?
振兴农村循环经济 和数链串起农业“生态链”
Leading the demand and justifying the HR value - the successful launch of the "Human Resource Leading Model HRLM"
HCIP BGP Comprehensive Experiment Establishing peers, route reflectors, federation, route announcement and aggregation
APP专项测试:流量测试
Nodejs安装教程
关于ue4.27像素流送打包后的本地服务器问题
数据库概论之MySQL表的增删改查2
随机推荐
Ue after video tutorial first
zabbix auto-discovery and auto-registration
nodejs的安装和全局配置(超详细哦)
享年94岁,图灵奖得主、计算复杂性理论先驱Juris Hartmanis逝世
nacos源码启动找不到istio包
关于ue4.27像素流送打包后的本地服务器问题
Xgboost报错 ValueError: Invalid shape: (1650, 2) for label
View source and switch mirrors in two ways: npm and nrm
request.getSession(), the story
abaqus如何快速导入其他cae文件的assembly?
typescript 'props' is declared but its value is never read solution
MySQL高级学习笔记
How does abaqus quickly import the assembly of other cae files?
npm ---- install yarn
zabbix email alarm and WeChat alarm
node安装及环境变量配置
Toolbox App 1.25 New Features at a Glance | Version Update
有人开源全凭“为爱发电”,有人却用开源“搞到了钱”
Dataset: A detailed guide to the download link collection of commonly used datasets in machine learning
chrome plugin development guide