博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
连通分量 Aizu - ALDS1_11_D Connected Components dfs或bfs或并查集
阅读量:3905 次
发布时间:2019-05-23

本文共 1614 字,大约阅读时间需要 5 分钟。

Write a program which reads relations in a SNS (Social Network Service), and judges that given pairs of users are reachable each other through the network.

Input

In the first line, two integer nn and mm are given. nn is the number of users in the SNS and mm is the number of relations in the SNS. The users in the SNS are identified by IDs 0,1,...,n−10,1,...,n−1 .

In the following mm lines, the relations are given. Each relation is given by two integers ss and tt that represents ss and tt are friends (and reachable each other).

In the next line, the number of queries qq is given. In the following qq lines, qq queries are given respectively. Each query consists of two integers ss and tt separated by a space character.

Output

For each query, print "yes" if tt is reachable from ss through the social network, "no" otherwise.

Constraints

  • 2≤n≤100,0002≤n≤100,000
  • 0≤m≤100,0000≤m≤100,000
  • 1≤q≤10,0001≤q≤10,000

Sample Input

10 90 10 23 45 75 66 76 87 88 930 15 91 3

Sample Output

yesyesno

此题我一开始用的dfs, 然后超时, 发现需要在查询之前确定是否连通,于是乎想到了并查集。。 。做完发现并查集是真的快。 。dfs写了一遍, 但是不如并查集快。 。。

dfs:

#include 
#include
#include
#include
#include
using namespace std;const int maxn=100005;int n,m,q;int num;vector
ve[maxn];int col[maxn];void init(){ num=0; memset (col,0,sizeof(col));}void dfs(int x,int nm){ col[x]=nm; for (int i=0;i

并查集:

#include 
#include
#include
#include
#include
using namespace std;const int maxn=100005;int n,m,q;int a[maxn];void init(){ for (int i=0;i

 

 

转载地址:http://uvaen.baihongyu.com/

你可能感兴趣的文章
Linux 任务控制的几个技巧( &, [ctrl]-z, jobs, fg, bg, kill)
查看>>
慧眼云:基于云计算和大数据分析的主动防御实践
查看>>
58集团监控业务实践:将网站运行信息透明化
查看>>
给Django用户的SQLAlchemy介绍
查看>>
consul http api
查看>>
如何定位问题
查看>>
使用火焰图分析CPU性能回退问题
查看>>
openresty lua zlib整合安装 让lua支持解压服务端压缩过的数据
查看>>
Nginx与Gzip请求
查看>>
最佳日志实践(v2.0)
查看>>
logstash日志分析的配置和使用
查看>>
Nginx问题定位之监控进程异常退出
查看>>
https://imququ.com/post/content-encoding-header-in-http.html
查看>>
如何监控 Nginx?
查看>>
字符编码的前世今生
查看>>
视频笔记:Go 抓包、分析、注入 - John Leon
查看>>
matplotlib 画图
查看>>
linux下模拟丢包,延时命令总结
查看>>
java的字符流简单介绍
查看>>
初识java的xml
查看>>