当前位置:网站首页>【暑期每日一题】洛谷 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;
}边栏推荐
- .NET Static Code Weaving - Rougamo Release 1.1.0
- MySQL 23 classic interviews hang the interviewer
- Servlet
- Nacos installation configuration and single-machine deployment tutorial
- MySQL高级-MVCC(超详细整理)
- pointer arithmetic in c language
- APP专项测试:流量测试
- C# FileInfo类
- 解决C#非静态字段、方法或属性“islandnum.Program.getIslandCount(int[][], int, int)”要求对象引用
- odoo field 设置匿名函数domain
猜你喜欢

MySQL经典50道练习题及全网最详细解析

PMP新考纲通关秘籍,告别抓瞎

Expert Insights | 3 ways to seize innovation opportunities in a downturn

MySQL - Multi-table query and case detailed explanation

宝塔+FastAdmin 404 Not Found

【21天学习挑战赛】顺序查找

Nacos installation configuration and single-machine deployment tutorial

MySQL高级学习笔记

MySQL 23 classic interviews hang the interviewer

PMP新考纲考试内容介绍
随机推荐
typescript 'props' is declared but its value is never read solution
MySQL 23 classic interviews hang the interviewer
Pagoda+FastAdmin 404 Not Found
Vscode连接远程服务器出现‘Acquiring lock on/home/~’问题
Nacos installation configuration and single-machine deployment tutorial
Launch Space on-premises deployment (local) Beta!
Specified URL is not reachable,caused by :‘Read timed out
The second day HCIP
数据库概论之MySQL表的增删改查1
两篇不错的php debug教程
有点奇怪!访问目的网址,主机能容器却不行
提交代码流程
The nacos source code can not find the istio package
About the local server problem after ue4.27 pixel streaming package
SphereEx苗立尧:云原生架构下的Database Mesh研发实践
MySQL Advanced Statements (1)
C# FileInfo class
MySQL Advanced - MVCC (ultra-detailed finishing)
(Notes are not completed) [Graph Theory] Traversal of graphs
MySQL Advanced Statements (1)