UPC:2218 Thrall’s Dream(BFS)
来源:互联网 发布:品牌零食 知乎 编辑:程序博客网 时间:2024/05/21 08:36
题意:给一个有向图,问任意两点之间能否联通。
思路:由于数据量不大,可以枚举每个点直接bfs遍历整个图,但这个过程有很多重复,可以加一些优化。比如说在当前枚举点x,它可达点y。如果y点之前已经枚举过那么就不需要再遍历y了,直接将y可达的点设为x也可达就行了。
#include <iostream>#include <vector>#include <cstring>#include <cstdio>#include <algorithm>#include <queue>using namespace std;const int maxn=2005;typedef long long LL;vector<short> g[maxn];bool can[maxn][maxn];int n,m;bool judge(){ for(int i=1; i<=n; ++i) for(int j=i+1; j<=n; ++j) if(!can[i][j]&&!can[j][i]) return false; return true;}void bfs(int x){ queue<short> que; que.push(x); while(!que.empty()) { short q=que.front(); que.pop(); if(q<x) { for(int i=1; i<=n; ++i) if(can[q][i]) can[x][i]=true; } else { for(int i=0; i<g[q].size(); ++i) { int y=g[q][i]; if(!can[x][y]) { can[x][y]=true; que.push(y); } } } }}int main(){ int T,kase=0; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); for(int i=1; i<=n; ++i) g[i].clear(); memset(can,0,sizeof(can)); for(int i=1; i<=m; ++i) { int x,y; scanf("%d%d",&x,&y); g[x].push_back(y); } for(int i=1; i<=n; ++i) bfs(i); if(judge()) printf("Case %d: Kalimdor is just ahead\n",++kase); else printf("Case %d: The Burning Shadow consume us all\n",++kase); } return 0;}
0 0
- UPC:2218 Thrall’s Dream(BFS)
- sdut 2604 Thrall’s Dream bfs
- UPC2218: Thrall’s Dream
- 第四届山东省赛 Thrall’s Dream(BFS+vector)
- SDUT 2604 Thrall’s Dream (bfs||单连通)
- Thrall’s Dream BFS 实现图之间的访问性
- 第四届 山东省ACM B Thrall’s Dream(连通图 OR BFS)
- [ACM] SDUT 2604 Thrall’s Dream
- sdut 2604 Thrall’s Dream (强联通分量+缩点)
- SDUT 2604 Thrall’s Dream (单连通的判断)
- 山东省第三届ACM省赛题——Thrall’s Dream(DFS)
- sdut 2604 Thrall’s Dream 判断一个图是否单侧连通
- Hrbust 2048 Thrall’s Dream【强连通+拓扑排序+思维】好题!
- 山东省第四届ACM大学生程序设计竞赛 Thrall’s Dream(单源强连通分量)
- Thrall’s Dream(山东省第四届ACM大学生程序设计竞赛 )
- 2013年山东省第四届ACM大学生程序设计竞赛 Problem I Thrall’s Dream 图,2n遍dfs
- UPC:2541 HaHa's Morning
- yesterday`s dream
- LeetCode Sort Colors
- 44. PHP while
- 张伟平得罪巩俐:在张艺谋面前捏造其劈腿
- 蝶恋花
- 欢迎使用CSDN-markdown编辑器
- UPC:2218 Thrall’s Dream(BFS)
- ZooKeeper基础
- Video Google: A Text Retrieval Approach to Object Matching in Videos
- XSLT输出比较或运算不想被转义
- [OSGI]OSGI入门介绍
- Application, Cache, Session, Cookie, Viewstate 用法和区别
- bootstrap (前端css框架)
- Unity3D导出eclipse工程使用JNI会崩的解决方法
- 每日一得--单向认证