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;}
原创粉丝点击