当前位置:网站首页>Motion Rule (16)-Union Check Basic Questions-Relations
Motion Rule (16)-Union Check Basic Questions-Relations
2022-08-04 12:33:00 【H_Cisco】
亲戚(Relations)
或许你并不知道,你的某个朋友是你的亲戚.He may be the grandson of your great-grandfather's grandfather's son-in-law's nephew's cousin's grandson.如果能得到完整的家谱,It should be feasible to judge whether two people are relatives,但如果两个人的最近公共祖先与他们相隔好几代,使得家谱十分庞大,那么检验亲戚关系实非人力所能及.在这种情况下,最好的帮手就是计算机.
为了将问题简化,你将得到一些亲戚关系的信息,如同Marry和Tom是亲戚,Tom和Ben是亲戚,等等.从这些信息中,你可以推出Marry和Ben是亲戚.请写一个程序,Questions for our concerned relatives,以最快的速度给出答案.
Reference input and output format输入由两部分组成.
第一部分以N,M开始.N为问题涉及的人的个数(1 ≤ N ≤ 20000).这些人的编号为1,2,3,…,N.下面有M行(1 ≤ M ≤ 1000000),每行有两个数ai, bi,表示已知ai和bi是亲戚.
第二部分以Q开始.以下Q行有Q个询问(1 ≤ Q ≤ 1 000 000),每行为ci, di,表示询问ci和di是否为亲戚.
对于每个询问ci, di,若ci和di为亲戚,则输出Yes,否则输出No.
样例输入与输出
输入
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
#include <cstdio>
#include <iostream>
using namespace std;
int fa[20001], n, m, x, y, r1, r2;
int find(int u)
{
return fa[u] == u ? u : fa[u] = find(fa[u]);
}
int read()
{
int x = 0;
char ch = getchar();
while (!isdigit(ch))
ch = getchar();
while (isdigit(ch))
x = x * 10 + ch - 48, ch = getchar();
return x;
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
fa[i] = i;
for (int i = 1; i <= m; i++)
{
x = read();
y = read();
r1 = find(x);
r2 = find(y);
if (r1 != r2)
fa[r1] = r2;
}
cin >> m;
for (int i = 1; i <= m; i++)
{
x = read();
y = read();
r1 = find(x);
r2 = find(y);
if (r1 == r2)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
边栏推荐
猜你喜欢
03 多线程与高并发 - ReentrantLock 源码解析
酷开科技 × StarRocks:统一 OLAP 分析引擎,全面打造数字化的 OTT 模式
推荐一款优秀的通用管理后台
项目里的各种配置,你都了解吗?
情人节浪漫3D照片墙【附源码】
数据中台建设(九):数据中台资产运营机制
A comprehensive understanding of MOS tubes, an article is enough
Small program on how to play in the construction of e-government service platform value
鲜花“刺客”收割七夕
Do you understand the various configurations in the project?
随机推荐
“蔚来杯“2022牛客暑期多校训练营4 N
Cache character stream
MOSFET米勒平台(Miller Plateau)
手搓一个“七夕限定”,用3D Engine 5分钟实现烟花绽放效果
基于BiLSTM的回归预测方法
移动跨端技术方案分析对比
“蔚来杯“2022牛客暑期多校训练营3 C
从数学角度和编码角度解释 熵、交叉熵、KL散度
The head module of the yolo series
【黑马早报】尚乘数科上市13天,市值超阿里;北大终止陈春花聘用合同;新东方花近200亿退学费和遣散费;张小泉75%产品贴牌代工...
缓存中间件技术选型Memcached、MongoDB、Redis
【微信小程序】信息管理与信息系统专业社会实习制作项目--垃圾指纹
Flutter教程大全合集(2022年版)
Neck modules of the yolo series
ShanDong Multi-University Training #4 A、B、C、G
systemdd-dev病毒杀
电源测试之输出动态响应(Output Dynamic Response Test)
Motion Rule (16)-Union Check Basic Questions-Grid Game
yolo系列的head模块
云原生Devops 的实现方法