当前位置:网站首页>【暑期每日一题】洛谷 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
NoAC 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;
}边栏推荐
- The stock price has repeatedly hit new lows, and the real estate SaaS giant is in trouble. How should Mingyuan Cloud transform and save itself?
- Vscode连接远程服务器出现‘Acquiring lock on/home/~’问题
- Toolbox App 1.25 新功能一览 | 版本更新
- 打卡day05
- CAT1 4G+Ethernet development board Tencent cloud mobile phone WeChat applet display temperature and delivery control
- 解决C#非静态字段、方法或属性“islandnum.Program.getIslandCount(int[][], int, int)”要求对象引用
- 专家见解|经济低迷期把握创新机会的 3 大方法
- 数据库概论之MySQL表的增删改查1
- mysql索引失效的常见9种原因详解
- Leading the demand and justifying the HR value - the successful launch of the "Human Resource Leading Model HRLM"
猜你喜欢

At age 94, pioneer Turing award winner, computational complexity theory, Juris Hartmanis, died

See the picture to understand | How to choose sales indicators to measure the health of business growth

Nodejs installation and global configuration (super detailed)

aTrust项目的相关操作与分享

MySQL Advanced Statements (1)

node安装及环境配置

View source and switch mirrors in two ways: npm and nrm

MySQL high-level statements (1)

MySQL驱动jar包的下载--保姆教程

Specified URL is not reachable,caused by :‘Read timed out
随机推荐
Leetcode周赛304
MySQL高级学习笔记
2022年7月18日-7月31日(Ue4视频教程和文档,20小时。合计1412小时,剩8588小时)
APP专项测试:流量测试
打卡day05
Dataset: A detailed guide to the download link collection of commonly used datasets in machine learning
Node installation and environment configuration
暑期总结(三)
(部分不懂,笔记整理未完成)【图论】差分约束
MySQL Index Common Interview Questions (2022 Edition)
View source and switch mirrors in two ways: npm and nrm
武汉高性能计算大会2022举办,高性能计算生态发展再添新动力
HCIP 第二天
Launch Space on-premises deployment (local) Beta!
HCIP day one
Nacos installation configuration and single-machine deployment tutorial
MySQL - 多表查询与案例详解
(笔记整理未完成)【图论】图的遍历
Connection reset by peer 问题解析
aTrust项目的相关操作与分享