当前位置:网站首页>Connected graph (union search set)
Connected graph (union search set)
2022-07-19 10:45:00 【Like a few dollars】
int find(int x) // Find the root node ( Find the leader )
{
if(pre[x] != find(x)) pre[x] = find(pre[x]);
return pre[x];
}
Example : Union checking set
Given an undirected graph and all its edges , Determine whether all vertices of the graph are connected .
Input format
The input contains several sets of data .
The first row of each set of data contains two integers nn and mm, Represents the number of points and edges of an undirected graph .
Next mm That's ok , Each line contains two integers x,yx,y, Indication point xx Sum point yy Connected to a .
The number of the point is from 11 To nn.
There may be double edges and self rings in the graph .
Output format
Output one row per group of data , A result , If all the vertices are connected , Output
YES, Otherwise outputNO.Data range
The input can contain at most 1010 Group data .
1≤n≤10001≤n≤1000,
1≤m≤50001≤m≤5000,
1≤x,y≤n1≤x,y≤nsample input :
4 3 1 2 2 3 3 2 3 2 1 2 2 3sample output :
NO YES
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1010;
int pre[N];
int find(int x)
{
if(pre[x]!=x) pre[x]=find(pre[x]);
return pre[x];
}
int main()
{
int n,m,a,b;
while(cin>>n>>m)
{
for(int i=1;i<=n;i++) pre[i]=i;
while(m--)
{
cin>>a>>b;
pre[find(a)]=find(b);
}
bool res=true;
for(int i=2;i<=n;i++)
{
if(find(i)!=find(1))
res=false;
}
if(res) puts("YES");
else puts("NO");
}
return 0;
}2022/7/11
边栏推荐
- openfoam热流边界条件
- unity3d如何利用asset store下载一些有用的资源包
- R language uses R native functions to aggregate transforms, calculate window statistics, calculate min, and merge the generated statistical data into the original data set
- 关于hping打流测试工具
- 移植吴恩达深度学习01机器学习和神经网络第二周神经网络基础编程作业选修作业到pycharm
- 博弈论(depu)与投资(40/100)
- Autojs learning - multi function treasure chest - medium
- Leetcode ugly number problem solution
- 金鱼哥RHCA回忆录:CL210描述OPENSTACK控制平面--识别overclound控制平台服务+章节实验
- Aike AI frontier promotion (7.17)
猜你喜欢
随机推荐
R language uses the ordinal of epidisplay package or. The display function obtains the summary statistical information of the ordered logistic regression model (the odds ratio and its confidence inter
Pytorch与权重衰减(L2范数)
TS解决引入插件的类型文件不存在的问题
LeetCode 2331. 计算布尔二叉树的值(树的遍历)
unity3d如何利用asset store下载一些有用的资源包
破案了卧槽---从MQ消费的逻辑怎么改代码都不生效
二分类学习推广到多分类学习
从“被动”到“主动”,ZETA技术助力“RFID2.0”升级该如何实现?
看一看这丑恶嘴脸 | MathWorks Account Unavailable - Technical Issue
MySQL query error
mysql不能启动了?相关组件缺失?系统升级?组件不匹配?开始重装mysql
LeetCode 2315. 统计星号(字符串)
手机键盘(模拟题)
The select function of dplyr package in R language deletes the data columns in dataframe containing the specified string content (drop columns in dataframe)
追根问底:Objective-C关联属性原理分析
Attachment handling of SAP Fiori
如何使用SVG制作沿任意路径排布的文字效果
Convert excel table to word table, and keep the formula in Excel table unchanged
vSphere 下借助 vDS 或 NSX 做端口镜像的方法总结
读已提交级别下 注解事务+分布式锁结合引起的事故--活动购买机会的错乱








