当前位置:网站首页>3587. 连通图(吉林大学考研机试题)
3587. 连通图(吉林大学考研机试题)
2022-07-24 08:25:00 【Ray.C.L】

思路:求连通性可用并查集或者dfs,bfs
代码:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1010;
int p[N];
int n, m;
int find(int x){
if(x != p[x]) p[x] = find(p[x]);
return p[x];
}
int main()
{
while(cin >> n >> m){
for(int i = 1; i <= n; i ++) p[i] = i;
while(m --){
int a, b;
cin >> a >> b;
p[find(a)] = find(b);
}
bool res = true;
for(int i = 2; i <= n; i ++)
if(find(i) != find(1)){
res = false;
break;
}
if(res) puts("YES");
else puts("NO");
}
return 0;
}
边栏推荐
- Recognition and storage of Graphs
- 2022.7.11全盘题解
- Kotlin学习笔记1——变量、函数
- POJ3278抓住那头牛题解
- [Yum] configuration and use of Yum source
- "Solution" friend of Vulcan
- Dao race track is booming. What are the advantages of m-dao?
- P1305 new binary tree solution
- [multithreading] five communication modes between multithreads
- [Google play access] payment server token acquisition
猜你喜欢

Wechat applet host environment, applet architecture, concise operation structure

My six months at Microsoft

Figure New Earth: how the RVT format BIM model modeled by Revit can accurately match the map with texture

Install SQL Server database

Introduction to wechat authorized login third-party app applet method

Encryption market ushers in a new historical cycle. Look at jpex's "stability" and "health"

Error reported by Nacos: error Nacos failed to start, please see d:\nacos\logs\nacos log for more details.

SVG 从入门到后悔,怎么不早点学起来(图解版)

Vidar-Team战队专访:AS WE DO, AS YOU KNOW.

The beta version of move protocol is stable, and it is temporarily decided to expand the scale of the prize pool
随机推荐
[database] complete SQL statement
Learn - use do... While loop according to the formula e=1+1/1+ 1/2!+ 1/3!+…+ 1/n! Calculate the value of E (accuracy is 1e-6)
[golang from introduction to practice] student achievement management system
Code=6 'The connection has timed out unexpectedly
Error lnk2019: unresolved external symbol [email protected]
Use of animation expert motionlayout layout
Stack / heap / queue question brushing (Part 2)
Digital collections "chaos", 100 billion market changes are coming?
Go: Gin basicauth Middleware
Summary of points management system project
[tools] a few lines of code can realize complex excel import and export tool classes, which is really strong!!!
Avoid pitfalls and stay away from PUA in the workplace. You need to know the common routines and scripts of PUA!
Several development frameworks based on openresty
WXS syntax reference -wxs module
What is the difference between domestic "rocket heart" artificial heart and different artificial heart?
Svg from entry to regret, why not learn it earlier (graphic version)
From starfish OS' continued deflationary consumption of SFO, the value of SFO in the long run
[Google play access] payment server token acquisition
[MySQL] 08: aggregate function
MySQL日期格式化