HDU 1269 迷宫城堡 (强连通分量,常规)
题意:
判断所给的有向图是否是一个强连通图。
思路:
如果连通分量大于1则必定No,如果强连通分量大于1也是No。tarjan算法求强连通分量。
![](/assets/blank.gif)
![](/assets/blank.gif)
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <vector> 5 #include <unordered_map> 6 #include <stack> 7 #include <iostream> 8 #include <bits/stdc++.h> 9 #define LL long long 10 #define pii pair<int,int> 11 using namespace std; 12 const int N=10000+5; 13 const int INF=0x7f7f7f7f; 14 vector<int> vect[N]; 15 stack<int> stac; 16 int n, m; 17 18 int lowlink[N], dfn[N], scc_no[N]; 19 int dfn_clock, scc_cnt; 20 21 void DFS(int x) 22 { 23 stac.push(x); 24 lowlink[x]=dfn[x]=++dfn_clock; 25 for(int i=0; i<vect[x].size(); i++) 26 { 27 int t=vect[x][i]; 28 if(!dfn[t]) 29 { 30 DFS(t); 31 lowlink[x]=min(lowlink[x],lowlink[t]); 32 } 33 else if(!scc_no[t]) 34 lowlink[x]=min(lowlink[x],dfn[t]); 35 } 36 if(lowlink[x]==dfn[x]) 37 { 38 scc_cnt++; 39 while(true) 40 { 41 int t=stac.top(); 42 stac.pop(); 43 scc_no[t]=scc_cnt; 44 if(x==t) break; 45 } 46 } 47 48 } 49 50 51 bool cal() 52 { 53 memset(lowlink, 0, sizeof(lowlink)); 54 memset(dfn, 0, sizeof(dfn)); 55 memset(scc_no, 0, sizeof(scc_no)); 56 57 scc_cnt=dfn_clock=0; 58 DFS(1); 59 if(scc_cnt>1) return false; //多个强连通分量 60 for(int i=1; i<=n; i++) if(!scc_no[i]) return false; //多个连通分量 61 return true; 62 } 63 64 65 int main() 66 { 67 freopen("input.txt", "r", stdin); 68 int a, b; 69 while(scanf("%d%d",&n,&m), n+m) 70 { 71 for(int i=1; i<=n; i++) vect[i].clear(); 72 for(int i=0; i<m; i++) 73 { 74 scanf("%d%d",&a,&b); 75 vect[a].push_back(b); 76 } 77 if(cal()) puts("Yes"); 78 else puts("No"); 79 } 80 return 0; 81 }
AC代码
转载于:https://www.cnblogs.com/xcw0754/p/4627310.html
HDU 1269 迷宫城堡 (强连通分量,常规)相关推荐
- HDU - 1269迷宫城堡 -强连通tanjar算法
为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000),每个通道都是单向的,就是说若称某通道连通了A房间和B房间,只说明可以 ...
- HDU 1269 迷宫城堡 -- 强连通图判断
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1269 图的强连通分量 http://baike.baidu.com/link?url=NqsmNsGC ...
- Hdu 1269.迷宫城堡
Problem Description 为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000),每个通道都是单向的,就是说若 ...
- HDU 1269 迷宫城堡(强连通图的判定)
最近<算法导论>快看完图论部分了,很多有关图的算法都彻底搞懂并加以证明了.现在主要是将理解的思想用到题目中来加强下.这个题目主要是判断一下整个图是否是强连通的,很简单,可以用tarjan也 ...
- HDU 1269 移动城堡 联通分量 Tarjan
迷宫城堡 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- hdu 1269 tarjan求强连通分量
tarjan求强连通分量的裸题复习,可当做模板. 1 #include <stack> 2 #include <cstdio> 3 #include <cstring&g ...
- [3.3训练赛]One-Dimensional(矩阵快速幂),Freda的迷宫(无向图强连通分量+并查集),一道防AK好题
文章目录 T1:One-Dimensional title solution code T2:[NOIP模拟赛]Freda的迷宫 title solution code T3:[NOIP模拟赛]一道防 ...
- HDU 5934:Boom——强连通分量+缩点
[题目描述] There are N bombs needing exploding.Each bomb has three attributes: exploding radius ri, posi ...
- 迷宫城堡 HDU - 1269 (塔尖算法求强连通分量)
为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000),每个通道都是单向的,就是说若称某通道连通了A房间和B房间,只说明可以 ...
- 【HDU 1269】迷宫城堡 (Tarjan算法)
迷宫城堡 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
最新文章
- 大型神经网络可能初具意识:OpenAI首席科学家引争议,众大佬吵成一团
- SpringMVC REST 风格静态资源访问配置
- numpy.tile 阵列
- boost::fusion::pop_back用法的测试程序
- 文章id 文章标题点击量php,WordPress如何通过文章ID获取文章标题等信息
- MATLAB灰度级数为1,图像处理 – 在matlab中更改灰度图像中的灰度级数
- linux 启动nacos报错_Spring Cloud:Alibaba 之 Nacos
- 折线图_Pyecharts数据可视化分析—折线图
- Spring+SpringMVC+MyBatis整合基础篇
- 系统动力学模型_RCR新文:基于系统动力学模型的中国煤炭产能情景预测
- CST2018/2020安装注意事项
- 最近常用容易遗忘的命令
- 教你来使用雪碧图(CSS sprite)
- mongodb添加多条数据_mongodb一次能插入多少数据
- 论文大致思路(不断更新)
- Unity工程里的那些东西
- kafka topic消息分配partition规则
- 蓝牙模块的配置(HC05):修改密码、修改模块名字、修改波特率。
- php substr 中文_PHP mb_substr():截取中文字符串
- 为什么xp系统访问不了服务器,XP系统不能访问局域网其他电脑
热门文章
- InnoDB如何实现多版本
- Hive练习求给出2017年1月的新客数
- 微信小程序 --- 音乐的播放和控制
- Java设计模式之单例模式(Singleton Pattern)
- python入门学习(五 字符串string和变量variable)
- css的布局模型(三)—层模型
- DWR入门教程(http://www.cnblogs.com/cyjch/archive/2012/02/16/2353758.html)
- Android Studio1.4.x JNI开发基础-基本环境配置
- “我来管管看”系列:如何消除报工数据与入库数据的差异?
- php echo substr('hello',1,-2);-2是什么意思 为什么结果是 el