立志用最少的代码做最高效的表达


We never paid any heed to the ancient prophecies, like fools we clung to the old hatreds, and fought as we had for generations. Until one day the sky rained fire, and a new enemy came upon us. We stand now upon the brink of destruction, for the Reign of Chaos has come at last.

Thrall, the warchief of the Orcish Horde, all along, he led his tribe live in the fringe of Lordaeron under the human control. In a downpour night, Thrall falls into sleep in a Orc hall at Arathi Highlands, at this moment he heard a voice:

“The sands of time have run out, son of Durotan. The cries of war echo upon the winds, the remnants of the past scar the land which is besieged once again by conflict. Heroes arise to challenge fate, and lead their brethren to battle. As mortal armies rush blindly towards their doom, The Burning Shadow comes to consume us all. You must rally the Horde, and lead your people to their destiny.

I will answer all of your questions in time, young warchief. For now, rally your warriors and prepare to leave this land, cross the sea to the distant land of Kalimdor. We will speak again. ”

Thrall believes the prophesy of Blood Raven Medivh. Three days later, He and Grom Hellscream’s Warsong Clan meet in the Lordaeron coast to the distant lands of Kalimdor. But the Goblin Zeppelins they take encountered storms in the middle. Thrall and Grom falling to the islands, they want to find each other and then to Kalimdor.

For the sake of simplicity, we assume that Thrall and Grom may fall into any islands x and y, only by Thrall to find Grom or by Grom to find Thrall. Give you the map of this island, please judge that Thrall and Gtom can meet?

Input
There are multiple test case in the input file, first line is a case number T. Each test case will begin with two integers N (0 <= N < 2001) and M (0 <= M < 10001), where N is the number of islands and M is number of portal. Next M lines each line contains two integers a and b, indicated there is a portal in island a that people can go from a to b by this portal. The island numbered from 1 to N.

Output
For each test case, your output should be in one line with “Kalimdor is just ahead” (without quotes, hereinafter the same) if Thrall and Grom can meet or “The Burning Shadow consume us all” otherwise as indicated in the sample output.

Sample Input
2
3 2
1 2
1 3
3 2
1 2
2 3
Sample Output
Case 1: The Burning Shadow consume us all
Case 2: Kalimdor is just ahead


分析

题意:n个点,m条路,单向路(只能从a到b)。 问任意两个点是否都连通。

分析

1、BFS
模板题,开大数组存储全部点,最后判断即可。

2、强联通分量
明天更新~


代码一:BFS解法

#include<iostream>
#include<queue>
#include<cstring>
#define maxn 2010
using namespace std;
typedef long long gg;gg n, m;
vector<gg>g[maxn];
bool has[maxn][maxn];   //连通数组
bool vis[maxn];         //每次bfs时判断是否可见
bool ok;void bfs(int s) {memset(vis, 0, sizeof(vis));       //vis置0 vis[s] = 1;queue<int>q;q.push(s);while(!q.empty()) {                 //遍历数组 int first = q.front();q.pop();int len = g[first].size();for(int i = 0; i < len; i++) {int v = g[first][i];if(vis[v]) continue;      //如果已经遍历过,则跳过has[s][v] = 1;q.push(v);vis[v] = 1; }}
}int main() {gg t, c = 1;scanf("%lld", &t);while(t--) {ok = true;scanf("%lld%lld", &n, &m);memset(has, 0, sizeof(has));   //1、初始化 for(gg i = 0; i < maxn; i++) g[i].clear();gg u, v;while(m--) {                    //2、数值存储, scanf("%lld%lld", &u, &v);g[u].push_back(v);}for(gg i = 1; i <= n; i++)       //3、逐个bfs,生成has数组 bfs(i);for(int i = 1; i <= n; i++) {    //判断是否连通 for(int j = i+1; j <= n; j++) {if(!has[i][j] && !has[j][i]) {  //只要有一个不连通 ok = false; break;}}if(ok == false) break;} if(ok)printf("Case %d: Kalimdor is just ahead\n", c++);else printf("Case %d: The Burning Shadow consume us all\n",c++);}return 0;
}

耗时

500ms

Thrall’s Dream HRBUST - 2048【BFS or 强连通分量】相关推荐

  1. 山东省第四届ACM大学生程序设计竞赛 Thrall’s Dream(单源强连通分量)

    Thrall's Dream Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 We never paid any heed to ...

  2. 2013年山东省第四届ACM大学生程序设计竞赛 Problem I Thrall’s Dream 图,2n遍dfs

    Thrall's Dream Time Limit: 1000MS Memory limit: 65536K 题目描述 We never paid any heed to the ancient pr ...

  3. 算法提高课-图论-有向图的强连通分量-AcWing 1174. 受欢迎的牛:tarjan算法求强连通分量、tarjan算法板子、强连通图

    文章目录 题目解答 题目来源 题目解答 来源:acwing 分析: 强连通图:给定一张有向图.若对于图中任意两个结点x,y,既存在从x到y的路径,也存在从y到x的路径,则称该有向图是"强连通 ...

  4. poj 2186 强连通分量

    poj 2186 强连通分量 传送门 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 33414 Acc ...

  5. 图论——强连通分量(Tarjan算法)

    文章目录 强连通分量 利用Tarjan算法求强连通分量 来一道例题练手(USACO08DEC) 图论文章汇总 强连通分量 什么是强连通图? 如果一个有向图中,存在一条回路,所有的结点至少被经过一次,这 ...

  6. 图论之tarjan真乃神人也,强连通分量,割点,桥,双连通他都会

    先来%一下Robert Tarjan前辈 %%%%%%%%%%%%%%%%%% 然后是热情感谢下列并不止这些大佬的博客: 图连通性(一):Tarjan算法求解有向图强连通分量 图连通性(二):Tarj ...

  7. 最小割+强连通分量 COGS 426 血帆海盗

    传送门 最小割定理我虽然说不太准,但大概就这个意思:对求完最大流后的残图进行tarjin,如果一条边的起点和终点不属于一个强连通分量,则这条边属于最大流. 联系一下,直接A #include<c ...

  8. 极小连通子图和极大连通子图_强连通分量与拓扑排序

    前言 由于GacUI里面开始多处用上拓扑排序,我决定把之前瞎JB搞出来的算法换掉,换成个正式的.之前我自己弄了个写起来很简单的算法,然后每一处需要用到的地方我就重新做一遍.当然这样肯定也是不行的,我觉 ...

  9. Tarjan算法应用 (割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)问题)...

    转载自:http://hi.baidu.com/lydrainbowcat/blog/item/2194090a96bbed2db1351de8.html 基本概念: 1.割点:若删掉某点后,原连通图 ...

最新文章

  1. 如何使用敏捷工具Leangoo脑图做Epic/ Theme /Story 管理
  2. Bzoj2762: [JLOI2011]不等式组
  3. 放假了,暂告一段落,迎接研究生
  4. SAP License:SAP概念辨识
  5. #CSP 201512-1 数位之和(100分)
  6. 设计模式(15)——抽象工厂模式(Abstract Factory)
  7. Ubuntu 14.04 desktop 不能安装 openssh-server解决方法
  8. arc和非arc完美支持
  9. Java实现仿QQ登陆、好友界面(可连接数据库)
  10. 工商服务代理行业解决方案
  11. 购物网站流程图(收藏)
  12. spring boot 报错:extShutdownHook ...was destroying!
  13. 开源的网络服务框架:Apache Etch 1.4.0 发布
  14. Electron--快速入门
  15. 典型相关分析、对应分析
  16. 解决Vivado implementation拥塞的策略方法(一)
  17. 支付宝小程序申请支付宝公钥遇到的坑
  18. 机器学习 --- PCA
  19. 出现Cannot resolve plugin XXX的解决办法
  20. 一年推出四款社交产品,百度社交难在哪?

热门文章

  1. python selenium 鼠标移动到指定元素,并点击对应的元素
  2. Python2与Python3之间的区别?
  3. Kafka日志清理之Log Deletion
  4. 喜提 redir contributor
  5. 来自未来团队伙伴的一封信
  6. ffplay.c学习-6-⾳视频同步基础
  7. 腾讯安全平台部专家研究员胡育辉:千亿黑产背后的破局之道
  8. ffmpeg基础库编程开发 读书笔记
  9. 互斥锁属性PTHREAD_MUTEX_RECURSIVE
  10. eclipse怎么更改tomcat的上下文访问路径