当前位置:网站首页>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
边栏推荐
猜你喜欢

win10开始键点击无响应

手机键盘(模拟题)

因果学习将开启下一代AI浪潮?九章云极DataCanvas正式发布YLearn因果学习开源项目

Domestic flagship mobile phones have a serious price foam, and second-hand phones are more cost-effective than new ones, or buy iPhones

Redis集群、一主二从三哨兵的搭建

LeetCode 2335. 装满杯子需要的最短总时长

知其然,而知其所以然,JS 对象创建与继承

Aike AI frontier promotion (7.17)

The use and Simulation of stack and queue in STL

Beego框架实现文件上传+七牛云存储
随机推荐
创建虚拟机第一章(vmvare虚拟机)
vulnhub inclusiveness: 1
看一看这丑恶嘴脸 | MathWorks Account Unavailable - Technical Issue
Structure the combat battalion | module 7
Thread pool principle
LeetCode 2335. 装满杯子需要的最短总时长
[Acwing]第 60 场周赛 B- 4495. 数组操作
C # treeview tree structure recursive processing (enterprise group type hierarchical tree display)
Custom complex logic verification during adding and modifying -2022 new project
VScode+Unity3D的配置
空天地海一体化网络体系架构与网络切片技术
二叉树刷题(二)
ue4对动画蓝图的理解
分类任务中的类别不平衡问题
c# treeView 树形结构递归处理(企业集团型层次树形展示)
顺序表的基本建立,以及增删改查的相关操作(c语言描述之顺序表)
Openfoam heat flow boundary condition
SAP ECC 和 S4HANA Material 物料库存管理的模型比较
如果是用mybatics去访问达梦数据库,相当于完全一样了?因为SQL语法没变。对吧?
R language ggplot2 visualization: use the ggstripchart function of ggpubr package to visualize dot strip plot, and set the add parameter to mean_ SD add the mean standard deviation vertical line and s