Problem B: Thrall's Dream
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 17 Solved: 3
[Submit][Status][Web Board]
Description
Problem Description
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
题意:
有 T 个 case,每个 case 都给出 N(0 ~ 2001),M(0 ~ 10001)代表有 N 个节点 M 条单向边。问是否任意两点间 a 和 b ,要么存在 a 到达 b,要么存在 b 到达 a。如果满足条件则输出 Kalimdor is just ahead,不能则输出 The Burning Shadow consume us all。
思路:
强连通分量 + 拓扑排序。要么 a 能到达 b,要么 b 能达 a,即存在一条直路从起点到终点,这条直路是没有分岔的,如果有分岔的话就不能满足题意了。除此之外,这个图还可能有环,有环则可以用强连通分量缩点表示成一个点,这个点可以存在在拓扑序列中。强连通分量缩点后,再对强连通分量建图,最后判断是否存在唯一确定的拓扑序即可。
AC:
#include <cstdio> #include <cstring> #include <algorithm> #include <stack> using namespace std; const int NMAX = 2005; const int EMAX = 10005; int n, m, ind; int v[EMAX], fir[NMAX], Next[EMAX]; int scc_cnt, dfs_clock; int pre[NMAX], low[NMAX], cmp[NMAX]; int deg[NMAX], nscc[NMAX][NMAX]; stack<int> s; void add_edge(int f, int t) { v[ind] = t; ++deg[t]; Next[ind] = fir[f]; fir[f] = ind; ++ind; } void dfs(int u) { low[u] = pre[u] = ++dfs_clock; s.push(u); for (int e = fir[u]; e != -1; e = Next[e]) { if (!pre[ v[e] ]) { dfs ( v[e] ); low[u] = min(low[u], low[ v[e] ]); } else if (!cmp[ v[e] ]) { low[u] = min(low[u], pre[ v[e] ]); } } if (low[u] == pre[u]) { ++scc_cnt; for (;;) { int x = s.top(); s.pop(); cmp[x] = scc_cnt; if (x == u) break; } } } void scc() { dfs_clock = scc_cnt = 0; memset(low, 0, sizeof(low)); memset(pre, 0, sizeof(pre)); memset(cmp, 0, sizeof(cmp)); for (int u = 1; u <= n; ++u) { if (!pre[u]) dfs(u); } } void make_scc() { memset(deg, 0, sizeof(deg)); memset(nscc, 0, sizeof(nscc)); for (int u = 1; u <= n; ++u) { int f = u; for (int e = fir[u]; e != -1; e = Next[e]) { int t = v[e]; if (cmp[f] != cmp[t] && !nscc[ cmp[f] ][ cmp[t] ]) { nscc[ cmp[f] ][ cmp[t] ] = 1; ++deg[ cmp[t] ]; } } } } bool topo() { for (int i = 1; i <= scc_cnt; ++i) { int ans = 0; for (int j = 1; j <= scc_cnt; ++j) { if (!deg[j]) { ++ans; deg[j] = 1; for (int t = 1; t <= scc_cnt; ++t) { if (nscc[j][t]) --deg[t]; } } } if (ans > 1) return false; } return true; } int main() { int t; scanf("%d", &t); for (int tt = 1; tt <= t; ++tt) { ind = 0; memset(fir, -1, sizeof(fir)); scanf("%d%d", &n, &m); while (m--) { int f, t; scanf("%d%d", &f, &t); add_edge(f, t); } scc(); make_scc(); printf("Case %d: ", tt); if( topo() ) printf("Kalimdor is just ahead\n"); else printf("The Burning Shadow consume us all\n"); } return 0; }
相关推荐
python学习资源
jfinal-undertow 用于开发、部署由 jfinal 开发的 web 项目
基于Andorid的音乐播放器项目设计(国外开源)实现源码,主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。
python学习资源
python学习资源
python学习一些项目和资源
【毕业设计】java-springboot+vue家具销售平台实现源码(完整前后端+mysql+说明文档+LunW).zip
HTML+CSS+JavaScarip开发的前端网页源代码
python学习资源
【毕业设计】java-springboot-vue健身房信息管理系统源码(完整前后端+mysql+说明文档+LunW).zip
成绩管理系统C/Go。大学生期末小作业,指针实现,C语言版本(ANSI C)和Go语言版本
1_基于大数据的智能菜品个性化推荐与点餐系统的设计与实现.docx
【毕业设计】java-springboot-vue交流互动平台实现源码(完整前后端+mysql+说明文档+LunW).zip
内容概要:本文主要探讨了在高并发情况下如何设计并优化火车票秒杀系统,确保系统的高性能与稳定性。通过对比分析三种库存管理模式(下单减库存、支付减库存、预扣库存),强调了预扣库存结合本地缓存及远程Redis统一库存的优势,同时介绍了如何利用Nginx的加权轮询策略、MQ消息队列异步处理等方式降低系统压力,保障交易完整性和数据一致性,防止超卖现象。 适用人群:具有一定互联网应用开发经验的研发人员和技术管理人员。 使用场景及目标:适用于电商、票务等行业需要处理大量瞬时并发请求的业务场景。其目标在于通过合理的架构规划,实现在高峰期保持平台的稳定运行,保证用户体验的同时最大化销售额。 其他说明:文中提及的技术细节如Epoll I/O多路复用模型以及分布式系统中的容错措施等内容,对于深入理解大规模并发系统的构建有着重要指导意义。
基于 OpenCV 和 PyTorch 的深度车牌识别
【毕业设计-java】springboot-vue教学资料管理系统实现源码(完整前后端+mysql+说明文档+LunW).zip
此数据集包含有关出租车行程的详细信息,包括乘客人数、行程距离、付款类型、车费金额和行程时长。它可用于各种数据分析和机器学习应用程序,例如票价预测和乘车模式分析。
把代码放到Word中,通过开发工具——Visual Basic——插入模块,粘贴在里在,把在硅基流动中申请的API放到VBA代码中。在Word中,选择一个问题,运行这个DeepSeekV3的宏就可以实现在线问答
【毕业设计】java-springboot+vue机动车号牌管理系统实现源码(完整前后端+mysql+说明文档+LunW).zip
【毕业设计】java-springboot-vue交通管理在线服务系统的开发源码(完整前后端+mysql+说明文档+LunW).zip