UVA 796 Critical Links(Tarjan求無向圖中的橋)

橋:割邊,即去掉之後原本連通的分支不再連通
求法:和割點差不多
只要 dfn[i]小於low[j],則ij就是割邊
注意這題的輸出格式,格式錯誤導致的wa了好幾次,gg…..輸出格式只要複製就可以了啊…….
還要按第一個輸出點升序排序。。。。

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 figure 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 finds all critical links of a given computer network.
Input
The program reads sets of data from a text file. Each data set specifies 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 first 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,
specifies 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
first data set from sample input below corresponds to the network in figure 1, while the second data
set specifies 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 first 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

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#define maxn 110
using namespace std;
int vis[maxn];
int dfn[maxn];
int low[maxn];
int s[maxn];
int pre[maxn];
int dfncnt=0;
int top=0;
int cnt=0;
int col[maxn];
int colcnt=0;
int head[maxn];
int ans=0;
int siz[maxn];
vector< pair<int,int> > oute;
struct node
{
    int to,next;
}e[20010];
void tarjan(int x,int fx)
{
    dfn[x]=++dfncnt;
    low[x]=dfn[x];
    pre[x]=fx;
    int flag=0;
    for(int i=head[x];i!=-1;i=e[i].next)
    {
        int to=e[i].to;
        if(!dfn[to])
        {
            tarjan(to,x);
            low[x]=min(low[x],low[to]);
        }
        else if(to!=fx)low[x]=min(low[x],dfn[to]);
    }
}
void add(int a,int b)
{
    e[cnt].to=b;
    e[cnt].next=head[a];
    head[a]=cnt++;
}
void init(int n)
{
    memset(vis,0,sizeof(vis));
    memset(s,0,sizeof(s));
    memset(head,-1,sizeof(head));
    memset(col,0,sizeof(col));
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    cnt=0;
    top=0;
    dfncnt=0;
    colcnt=0;
    ans=0;
}
int main()
{
    int m,n;
    int a,b;
    char tmp;
    while(scanf("%d",&n)==1)
    {
        init(n);
        for(int i=0;i<n;i++)
        {
            scanf("%d (%d)",&a,&m);
            for(int j=0;j<m;j++)
            {
                scanf("%d",&b);
                add(a+1,b+1);
            }
        }
        oute.clear();
        bool flag=1;
        for(int i=1;i<=n;i++)
        {
            if(!dfn[i])
                tarjan(i,-1);
        }
        for(int i=1;i<=n;i++)
        {
            if(pre[i]==-1)continue;
            if(dfn[pre[i]]<low[i])
            {
                ans++;
                a=pre[i]-1;
                b=i-1;
                if(b<a)swap(a,b);
                oute.push_back(make_pair(a,b));
            }
        }
        printf("%d critical links\n",ans);
        sort(oute.begin(),oute.end());
        for(int i=0;i<oute.size();i++)
        {
            printf("%d - %d\n",oute[i].first,oute[i].second);
        }
        printf("\n");
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章