1155. Can I Post the letter(有向图遍历判断两点是否可到达)
2013-01-02 19:56
453 查看
/* 1155. Can I Post the letter(有向图遍历判断两点是否可到达) 题意:寻找可否到达另一个点 思路:每个节点都持有下一个点的下标和持有的下标数destNum 开始时从0开始dfs,发现超时了。从后面n-1往上找。 */ #include<iostream> #include<stdlib.h> #include<vector> #include<algorithm> using namespace std; struct city { int destNum; int dest[200]; }; city cc[200]; int destNum; bool visit[200]; bool dfs(int index){ if(index == 0){ cout << "I can post the letter" << endl; return true; } visit[index] = true; for(int i=0; i<cc[index].destNum; i++){ int tempEnd = cc[index].dest[i]; if(!visit[tempEnd]) { if(dfs(tempEnd)) return true; } } visit[index] = false; return false; } int main() { int n, m; while(cin >> n && n != 0){ cin >> m; destNum = n-1; for(int i=0; i<200; i++){ cc[i].destNum=0; visit[i] = false; } for(int i=1; i<=m; i++){ int start,end; cin >> start >> end; cc[end].dest[cc[end].destNum++] = start; } if(!dfs(n-1)){ cout << "I can't post the letter" << endl; } } system("pause"); return 0; }
相关文章推荐
- Sicily 1155 Can I Post the letter (图的遍历 BFS)
- sicily:1155.Can I Post the letter
- sicily 1155 Can I Post the letter
- Sicily 1155. Can I Post the letter
- sicily 1155. Can I Post the lette
- sicily 1155. Can I Post the lette
- [sicily online]1155. Can I Post the lette(图的深度优先搜索)
- Sicily 1155. Can I Post the lette
- 1155. Can I Post the lette
- sicily 1155. Can I Post the lette
- Sicily 1155. Can I Post the lette
- <OJ_Sicily>Can I Post the letter
- 算法学习【12】—— 1155. Can I Post the lette
- 【遍历二叉树】09判断二叉树是否关于自己镜像对称【Symmetric Tree】
- HDU 1841 Find the Shortest Common Superstring(KMP 理解 应用 求组合串的next值判断是否匹配)
- 输入一个数列,判断是否为某一个二叉搜索树的后序遍历序列
- 数组遍历判断两个输入框之间是否有输入重复的值!
- poj 2699 The Maximum Number of Strong Kings 【二分 + 竞赛图建模判断是否满流】
- 判断二叉搜索树后序遍历结果是否正确
- POJ 1679 The Unique MST (prim判断最小生成树是否唯一)