无向图的联通分量NEUACM1134: Searching for TwoBee
来源:互联网 发布:激光美容知乎 编辑:程序博客网 时间:2024/04/29 17:39
题目链接:http://acm.neu.edu.cn/hustoj/problem.php?id=1134
题目描述
Zhuo deeply loves TwoBee. But one day TwoBee got lost, Zhuo got crazy searching for TwoBee all over the world. Now give you a map that contains the places that Zhuo and the lost TwoBee may be in. there are N places and M bidirectional roads in the map. Zhuo and TwoBee stay at a random place initially, and TwoBee is too scared to move any more. Does it possible for Zhuo to find TwoBee no matter where the places they stay initially?
输入
This problem contains multiple test cases. The first row contains two integers N, M (1<=N<=1000,0<=M<=10000)which has been mentioned above. In the next M lines, each line contains two integers a,b(1<=a,b<=N),which indicates there is a road between a and b.
输出
Print exactly one line with a string “YES” if Zhuo is possible to find TwoBee, otherwise output a string “NO”.
显然这题是求无向图的联通分量,如果只有一个,那么所有点可以互达,输出yes,否则输出no。求一个无向图的联通分量可以用DFS在线性时间内求出,如下代码:
void find_cc(){ current_cc=0; memset(vis,false,sizeof(vis)); for(int u=0;u<n;u++) if(!vis[u]) { current_cc++; dfs(u); }}
然而如果只需求联通分量,则可以使用并查集,不仅不会出现DFS栈溢出,而且连图都不用保存。
#include <iostream>#include <cstdio>#define MAXN 1050using namespace std; int pa[MAXN]; int findset(int x){ return pa[x]!=x?pa[x]=findset(pa[x]):x;} int main(){ int n,m,x,y; while(scanf("%d%d",&n,&m)!=EOF) { for(int i=1;i<=n;i++) pa[i]=i; int num=n; for(int i=0;i<m;i++) { scanf("%d%d",&x,&y); x=findset(x),y=findset(y); if(x!=y) { pa[x]=y; num--; } } if(num==1) printf("YES\n"); else printf("NO\n"); } return 0;} /************************************************************** Problem: 1134 Language: C++ Result: 正确 Time:18 ms Memory:1272 kb****************************************************************/
- 无向图的联通分量NEUACM1134: Searching for TwoBee
- 无向图的边双联通分量,点双联通分量
- 有向图的强联通分量
- 无向图的割顶、桥和双联通分量
- 求无向图中的桥 和 边的双联通分量
- 有向图的强联通分量 tarjan
- 无向图的连通分量
- 无向图的连通分量
- 无向图的连通分量
- 求无向图的连通分量
- 无向图的连通分量
- 无向连通图求割点(tarjan算法去掉改割点剩下的联通分量数目)
- Tarjan(无向图双联通分量)——hihoCoder 1184
- 无向图强联通分量-洛谷 P2860 [USACO06JAN]冗余路径Redundant Paths
- BZOJ2140: 稳定婚姻 Tarjan求无向图强联通分量
- UVALive3523 [Knights of the Round Table] tarjan求无向图双联通分量
- UVALive5135 [Mining Your Own Business] tarjan求无向图双联通分量
- uva 10972 添加几条变使得无向图为双联通分量
- C++的异常处理初步学习
- 多核编程(一)-- 概述
- Java泛型的好处
- 解决SQL Server 没有安装VS无法自动提示及没有折叠号
- 2013年湖南省第九届程序设计大赛 J 搞笑版费马大定理
- 无向图的联通分量NEUACM1134: Searching for TwoBee
- Ibatis基本的sql语句
- Spiral Matrix II
- 使用Eclipse中的ant功能将生成的JAR文件ftp到服务器
- 饭统网倒闭:不创新、不放权就是作死
- hdu 1171 Big Event in HDU
- 单例模式之简约美
- linux网络编程实现投票功能
- dlutoj 1263 树状数组