POJ3713-Transferring Sylla
来源:互联网 发布:菜鸟网络工资 编辑:程序博客网 时间:2024/06/05 03:19
Transferring Sylla
After recapturing Sylla, the Company plans to establish a new secure system, a transferring net! The new system is designed as follows:
The Company staff choose N cities around the nation which are connected by “security tunnels” directly or indirectly. Once a week, Sylla is to be transferred to another city through the tunnels. As General ordered, the transferring net must reach a certain security level that there are at least 3 independent paths between any pair of cities a, b. When General says the paths are independent, he means that the paths share only a and b in common.
Given a design of a transferring net, your work is to inspect whether it reaches such security level.
Input
The input consists of several test cases.
For each test case, the first line contains two integers, N ≤ 500 and M ≤ 20000. indicating the number of cities and tunnels.
The following M lines each contains two integers a and b (0 ≤ a, b < N), indicating the city a and city b are connected directly by a tunnel.
The input ends by two zeroes.
Output
For each test case output “YES” if it reaches such security level, “NO” otherwise.
Sample Input
4 6
0 1
0 2
0 3
1 2
1 3
2 3
4 5
0 1
0 2
0 3
1 2
1 3
7 6
0 1
0 2
0 3
1 2
1 3
2 3
0 0
Sample Output
YES
NO
NO
参考书籍:《挑战程序设计竞赛》
题目大意:判断一个无向图是否三连通?
解题思路: 三连通是指从a到b有三条除了a和b外无重点的路径(去掉三个点就不连通)。暴力的想法就是枚举删除两个点,看图是否还连通,但这样时间复杂度是O(n^3)的,肯定超时,所以枚举删除一个点,如果存在割点,那么就不满足(因为再删掉这个点,图就不连通了,才删掉两个点,图就不连通,那么就肯定不是三连通图),Tarjan算法可以求解连通和割点,再枚举删除一个点就能达到三连通的目的。时间复杂度O(n^2)
#include <iostream>#include <vector>#include <algorithm>#include <cstring>using namespace std;#define MAXN 512vector<int> G[MAXN];int V;bool is_cut_vertex[MAXN];int status[MAXN];int lowlink[MAXN];int index[MAXN];int root;bool has_cut_vertex;void init(const int &v){ V = v; has_cut_vertex = false; for (int i = 0; i <= V; ++i) { G[i].clear(); }}void tarjan_dfs(int current, int from, int depth){ status[current] = 1; lowlink[current] = index[current] = depth; int sub_tree = 0; int v; for (vector<int>::const_iterator it = G[current].begin(), end = G[current].end(); it != end; ++it) { v = *it; if (v != from && status[v] == 1) { lowlink[current] = min(lowlink[current], index[v]); } if (0 == status[v]) { tarjan_dfs(v, current, depth + 1); ++sub_tree; lowlink[current] = min(lowlink[current], lowlink[v]); if ((current == root && sub_tree > 1) || (current != root && lowlink[v] >= index[current])) { is_cut_vertex[current] = 1; has_cut_vertex = true; } } } status[current] = 2;}void calc(int del){ memset(is_cut_vertex, 0, sizeof(is_cut_vertex)); memset(status, 0, sizeof(status)); memset(lowlink, 0, sizeof(lowlink)); memset(index, 0, sizeof(index)); status[del] = 2; root = 0; if (del == 0) { root = 1; } tarjan_dfs(root, -1, 1);}bool solve(){ for (int i = 0; i < V; ++i) { calc(i); for (int j = 0; j < V; ++j) { if (0 == status[j]) { has_cut_vertex = true; break; } } if (has_cut_vertex) { break; } } return !has_cut_vertex;}int main(){ int N, M; while (cin>>N>>M) { if(N==0&&M==0) break; init(N); for (int i = 0; i < M; ++i) { int from, to; cin>>from>>to; G[from].push_back(to); G[to].push_back(from); } if(solve()) cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0;}
- POJ3713-Transferring Sylla
- POJ3713: Transferring Sylla 题解
- POJ3713-Transferring Sylla
- poj3713 Transferring Sylla 枚举+tarjan判割点
- POJ-3713-Transferring Sylla
- Transferring Sylla POJ, 3713
- POJ,3713 Transferring Sylla
- Poj 3713 Transferring Sylla 3-连通
- POJ 3713 Transferring Sylla 已翻译
- POJ 3713 Transferring Sylla(图的三联通,tarjan算法
- [POJ 3713] Transferring Sylla (枚举删点+Tarjan求割点)
- poj Transferring Sylla(如何快速的判断一个图是否是3—连通图,求割点,割边)
- Transferring @ Changi airport singapore
- Transferring Jobs 工作调动
- Transferring Files With FTP
- Just only transferring registration
- Converting and transferring data
- rsync Usages For Transferring Files
- Btrace1.3.9用户手册
- RHEL7 系统ISCSI存储环境搭建
- 嵌入式学习6(C语言运算符及控制语句)
- Android:dagger2让你爱不释手-基础依赖注入框架篇
- 剑指offer笔试面试题之——位运算
- POJ3713-Transferring Sylla
- Java异常: "2 字节的 UTF-8 序列的字节 2 无效。"的解决方案
- (1)php框架开发---路由转发
- Android 获取屏幕尺寸和密度
- Android “java.lang.RuntimeException: Parcelable encounteredClassNotFoundException reading a Serializ
- Android Stuido启动提示"No JVM installation found.Please install a 32-bit JDK...."
- oracle官网如何下载早期版本的jdk
- C语言———42道自整理面试题
- HTML5之列表