Technology Sharing

AcWing 3587: Connected graph ← dfs (adjacency matrix or chained forward star)

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

【Source of the topic】
https://www.acwing.com/problem/content/3590/

【Topic Description】
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 line of each data set contains two integers n and m, representing the number of vertices and edges in the undirected graph.
The next m lines each contain two integers x and y, indicating that point x is connected to point y.
The points are numbered from 1 to n.
The picture may exist
Heavy EdgeandSelf-loop

Output format
Output one line and one result for each set of data. If all vertices are connected, output YES, otherwise output NO.

【data range】
The input contains at most 10 sets of data.
1≤n≤1000,
1≤m≤5000,
1≤x,y≤n

[Input example]
4 3
1 2
2 3
3 2
3 2
1 2
2 3

Output sample
NO
YES

【Analysis of Algorithms】
● The “
Union Search"For code implementation, see:https://blog.csdn.net/hnjzsyjyj/article/details/126455868
● The principle of using dfs to determine the connected graph in this question is "
DFS must be able to traverse all points in the connected graphIf a point is not traversed, it means it is not connected.

[Algorithm code: dfs+chain forward star]
● dfs algorithm template:
https://blog.csdn.net/hnjzsyjyj/article/details/118736059
● Chain forward star details:https://blog.csdn.net/hnjzsyjyj/article/details/139369904

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N=1e3+5;
  4. const int M=5e3+5;
  5. int e[M<<1],ne[M<<1],h[N],idx;
  6. bool st[N];
  7. int n,m;
  8. void add(int a,int b) {
  9. e[idx]=b,ne[idx]=h[a],h[a]=idx++;
  10. }
  11. void dfs(int u) {
  12. st[u]=1;
  13. for(int i=h[u]; i!=-1; i=ne[i]) {
  14. int j=e[i];
  15. if(!st[j]) dfs(j);
  16. }
  17. }
  18. int main() {
  19. while(cin>>n>>m) {
  20. memset(st,false,sizeof st);
  21. memset(h,-1,sizeof h);
  22. idx=0;
  23. while(m--) {
  24. int a,b;
  25. cin>>a>>b;
  26. add(a,b),add(b,a);
  27. }
  28. dfs(1);
  29. bool flag=true;
  30. for(int i=1; i<=n; i++)
  31. if(!st[i]) {
  32. flag=false;
  33. break;
  34. }
  35. if(flag) cout<<"YES"<<endl;
  36. else cout<<"NO"<<endl;
  37. }
  38. return 0;
  39. }
  40. /*
  41. in:
  42. 4 3
  43. 1 2
  44. 2 3
  45. 3 2
  46. 3 2
  47. 1 2
  48. 2 3
  49. out:
  50. NO
  51. YES
  52. */

[Algorithm code: dfs+adjacency matrix]
● dfs algorithm template:
https://blog.csdn.net/hnjzsyjyj/article/details/118736059
● Adjacency matrix implementation of undirected unweighted graph:https://blog.csdn.net/hnjzsyjyj/article/details/116245897

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N=1010;
  4. int g[N][N];
  5. bool st[N];
  6. int n,m;
  7. void dfs(int u) {
  8. st[u]=true;
  9. for(int i=1; i<=n; i++)
  10. if(!st[i] && g[u][i]!=0) dfs(i);
  11. }
  12. int main() {
  13. while(cin>>n>>m) {
  14. memset(g,0,sizeof g);
  15. memset(st,false,sizeof st);
  16. int x,y;
  17. while(m--) {
  18. cin>>x>>y;
  19. g[x][y]=g[y][x]=1;
  20. }
  21. dfs(1);
  22. int i;
  23. for(i=1; i<=n; i++) {
  24. if(!st[i]) break;
  25. }
  26. if(i<=n) cout<<"NO"<<endl;
  27. else cout<<"YES"<<endl;
  28. }
  29. return 0;
  30. }
  31. /*
  32. in:
  33. 4 3
  34. 1 2
  35. 2 3
  36. 3 2
  37. 3 2
  38. 1 2
  39. 2 3
  40. out:
  41. NO
  42. YES
  43. */



【references】
https://www.acwing.com/solution/content/124095/

https://blog.csdn.net/hnjzsyjyj/article/details/118736059
https://blog.csdn.net/hnjzsyjyj/article/details/139369904