UVA796 - Critical Links(Tarjan求桥)

In a computer network a link L, which interconnects two servers, is considered critical if there are at
least two servers A and B such that all network interconnection paths between A and B pass through L.
Removing a critical link generates two disjoint sub–networks such that any two servers of a sub–network
are interconnected. For example, the network shown in ?gure 1 has three critical links that are marked
bold: 0 -1, 3 - 4 and 6 - 7.
Figure 1: Critical links
It is known that:
1. the connection links are bi–directional;
2. a server is not directly connected to itself;
3. two servers are interconnected if they are directly connected or if they are interconnected with
the same server;
4. the network can have stand–alone sub–networks.
Write a program that ?nds all critical links of a given computer network.
Input
The program reads sets of data from a text ?le. Each data set speci?es the structure of a network and
has the format:
no of servers
server0 (no of direct connections) connected server . . . connected server
. . .
serverno of servers (no of direct connections) connected server . . . connected server
The ?rst line contains a positive integer no of servers(possibly 0) which is the number of network
servers. The next no of servers lines, one for each server in the network, are randomly ordered and
show the way servers are connected. The line corresponding to serverk, 0 ≤ k ≤ no of servers − 1,
speci?es the number of direct connections of serverk and the servers which are directly connected to
serverk. Servers are represented by integers from 0 to no of servers − 1. Input data are correct. The
?rst data set from sample input below corresponds to the network in ?gure 1, while the second data
set speci?es an empty network.
Output
The result of the program is on standard output. For each data set the program prints the number of
critical links and the critical links, one link per line, starting from the beginning of the line, as shown
in the sample output below. The links are listed in ascending order according to their ?rst element.
The output for the data set is followed by an empty line.
Sample Input
8
0 (1) 1
1 (3) 2 0 3
2 (2) 1 3
3 (3) 1 2 4
4 (1) 3
7 (1) 6
6 (1) 7
5 (0)
0
Sample Output
3 critical links
0 - 1
3 - 4
6 - 7

0 critical links

 

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=737

 

给你一个图,让你求这个图中哪些是桥,并输出;

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
#include <vector>
using namespace std;
typedef long long LL;
#define N 10005
#define met(a, b) memset(a, b, sizeof(a))

int dfn[N], low[N], Time, ans;
int n, f[N];
vector<vector<int> >G;

struct node
{
    int x, y;
    bool friend operator < (node A,node B)
    {
        if(A.x == B.x)
            return A.y < B.y;
        return A.x < B.x;
    }
}a[N];

void Init()
{
    met(dfn, 0);
    met(low, 0);
    met(f, 0);
    met(a, 0);
    G.clear();
    G.resize(n+3);
    Time = 0;
}

void Tarjan(int u, int fa)
{
    low[u] = dfn[u] = ++Time;
    f[u] = fa;
    int len = G[u].size(), v;
    for(int i=0; i<len; i++)
    {
        v = G[u][i];
        if(!dfn[v])
        {
            Tarjan(v, u);
            low[u] = min(low[u], low[v]);

            if(low[v] > dfn[u])///判断是否是桥;
            {
                a[ans].x = u;
                a[ans].y = v;
                if(a[ans].x>a[ans].y)swap(a[ans].x, a[ans].y);
                ans++;
            }
        }
        else if(fa != v)
            low[u] = min(dfn[v], low[u]);
    }
}

int main()
{
    while(scanf("%d", &n) != EOF)
    {
        Init();

        int u, v, m;

        for(int i=0; i<n; i++)
        {
            scanf("%d (%d)", &u, &m);
            for(int j=0; j<m; j++)
            {
                scanf("%d", &v);
                G[u].push_back(v);
                G[v].push_back(u);
            }
        }
        ans = 0;

        for(int i=0; i<n; i++)
            if(!dfn[i])
                Tarjan(i, -1);

        sort(a, a+ans);

        printf("%d critical links\n", ans);
        for(int i=0; i<ans; i++)
            printf("%d - %d\n", a[i].x, a[i].y);
        printf("\n");
    }
    return 0;
}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
#include <vector>
using namespace std;
typedef long long LL;
#define N 10005
#define met(a, b) memset(a, b, sizeof(a))

int dfn[N], low[N], Time;
int n, f[N];
vector<vector<int> >G;

struct node
{
    int x, y;
    bool friend operator < (node A,node B)
    {
        if(A.x == B.x)
            return A.y < B.y;
        return A.x < B.x;
    }
}a[N];

void Init()
{
    met(dfn, 0);
    met(low, 0);
    met(f, 0);
    met(a, 0);
    G.clear();
    G.resize(n+3);
    Time = 0;
}

void Tarjan(int u, int fa)
{
    low[u] = dfn[u] = ++Time;
    f[u] = fa;
    int len = G[u].size(), v;
    for(int i=0; i<len; i++)
    {
        v = G[u][i];
        if(!dfn[v])
        {
            Tarjan(v, u);
            low[u] = min(low[u], low[v]);
        }
        else if(fa != v)
            low[u] = min(dfn[v], low[u]);
    }
}

int main()
{
    while(scanf("%d", &n) != EOF)
    {
        Init();

        int u, v, m;

        for(int i=0; i<n; i++)
        {
            scanf("%d (%d)", &u, &m);
            for(int j=0; j<m; j++)
            {
                scanf("%d", &v);
                G[u].push_back(v);
                G[v].push_back(u);
            }
        }

        for(int i=0; i<n; i++)
            if(!dfn[i])
                Tarjan(i, -1);

        int ans = 0;

        for(int i=0; i<n; i++)
        {
            v = f[i];
            if(v!=-1 && low[i]>dfn[v])
            {
                a[ans].x = i;
                a[ans].y = v;
                if(a[ans].x>a[ans].y)swap(a[ans].x, a[ans].y);
                ans++;
            }
        }
        sort(a, a+ans);

        printf("%d critical links\n", ans);
        for(int i=0; i<ans; i++)
            printf("%d - %d\n", a[i].x, a[i].y);
        printf("\n");
    }
    return 0;
}

时间: 2024-10-11 10:10:14

UVA796 - Critical Links(Tarjan求桥)的相关文章

UVA 796 - Critical Links【求桥】

link:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=737 题意: 求桥的数目及边,要求输出边的点的次序由小到大 代码: #include <stdio.h> #include <ctime> #include <math.h> #include <limits.h> #include

UVA 796 Critical Links(无向图求桥)

题目来源: UVa Online Judgehttps://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=737 求一个连通图中必不可少的路径: #include<stdio.h> #include<algorithm> #include<vector> #include<string.h> #define

UVA 796 - Critical Links (求桥按序输出)

tanjar求图中的桥,然后排序输出. 代码: #include<iostream> #include<cstdio> #include<string> #include<cmath> #include<queue> #include<stack> #include<map> #include<cstring> #include<algorithm> #define rep(i,a,b) for(i

HDU 4738 Caocao&#39;s Bridges tarjan求桥

Caocao's Bridges Problem Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn't give up. Caocao's army still was not good at water battles, so he came up with another idea. He built many islands in the Chan

Tarjan求桥和割点

//Tarjan 求桥和割点 Tarjan(u,fa) { DFN[u]=LoW[u]=++time; Cu=grey; for each e=(u,v) { Tarjan(v,u); if(Cv=white) { low[u]=min(low[u],low[v]); }else { low[u]=min(low[u],DFN[v]); } } }

【模板】【hdu4738】Caocao&#39;s Bridges——tarjan求桥

题目链接 题目大意: 曹操有N个岛,这些岛用M座桥连接起来,每座桥有士兵把守(也可能没有), 诸葛亮把所有炸弹都带走了,只留下一枚给周瑜(真狠). 周瑜想让这N个岛不连通,但需要派出不小于守桥士兵数的人去炸桥,因为只有一枚炸弹,因此只够炸掉一座桥. 分析: 很明显的求代价最小的桥,当然这道题有几个特殊的地方: 1.图本来就不联通,输出0: 2.无解(不存在桥),输出-1: 3.没人把守,但你还是得派一个人炸桥,输出1: 4.一般情况,输出最小代价. 剩下的就是模板了,不过需要注意的一点是,这道题

UVA796:Critical Links(输出桥)

Critical Links 题目链接:https://vjudge.net/problem/UVA-796 Description: In a computer network a link L, which interconnects two servers, is considered critical if there are at least two servers A and B such that all network interconnection paths between

[UVA796]Critical Links(割边, 桥)

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=737 求桥的数量,也就是割边的数量.输入有点小坑,左右括号外必须得有个空格才行,起初以为是转义的问题. 1 /* 2 ━━━━━┒ギリギリ♂ eye! 3 ┓┏┓┏┓┃キリキリ♂ mind! 4 ┛┗┛┗┛┃\○/ 5 ┓┏┓┏┓┃ / 6 ┛┗┛┗┛┃ノ) 7 ┓┏┓┏┓┃ 8

UVA 796 Critical Links —— (求割边(桥))

和求割点类似,只要把>=改成>即可.这里想解释一下的是,无向图没有重边,怎么可以使得low[v]=dfn[u]呢?只要它们之间再来一个点即可. 总感觉图论要很仔细地想啊- -一不小心就弄混了.. 另外从这题发现,代码还是写成模块化比较好,比如solve一个函数,init一个函数等等,这样可以避免很多东西忘记写,比方说dfn或者G的清空等等.. 代码如下: 1 #include <stdio.h> 2 #include <stack> 3 #include <alg