【Lightoj 1026 B Critical Links】& 桥

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 red: 0 - 1, 3 - 4 and 6 - 7 in figure 2.

这里写图片描述这里写图片描述

Figure 1: Original Graph

Figure 2: The 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
Input starts with an integer T (≤ 15), denoting the number of test cases.

Each case starts with a blank line. The next line will contain n (0 ≤ n ≤ 10000) denoting the number of nodes. Each of the next n lines will contain some integers in the following format:

u (k) v1 v2 … vk

Where u is the node identifier, k is the number of adjacent nodes; v1, v2 … vk are the adjacent nodes of u. You can assume that there are at most 100000 edges in total in a case. Dataset is huge, so use faster i/o methods.

Output
For each case, print the case number first. Then you should print 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 and then second element. Since the graph is bidirectional, print a link u v if u < v.

Sample Input
3

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

2
0 (1) 1
1 (1) 0
Sample Output
Case 1:
3 critical links
0 - 1
3 - 4
6 - 7
Case 2:
0 critical links
Case 3:
1 critical links
0 - 1
Hint
Dataset is huge, use faster I/O methods.

题意 : 找“桥”的个数,桥:关键路径,去掉该边将导致原本相连的点不在联通

思路 : 据说是个模板题(QAQ)

AC代码:

#include<cstdio>
#include<cmath>
#include<deque>
#include<queue>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX = 1e5 + 10;
const int INF = 1e9 + 7;
typedef long long LL;
int nl,head[MAX * 4],n;
int ans,d[MAX],lo[MAX],pl;
struct node{
    int to,next;
}st[MAX * 4];
struct nod{
    int u,v;
}sum[MAX * 4];
void add(int x,int y){
    st[nl] = node{y,head[x]};
    head[x] = nl++;
}
void init(){
    ans = nl = 0;
    pl = 1;
    memset(head,-1,sizeof head);
    memset(d,0,sizeof d);
    memset(lo,0,sizeof lo);
}
void tr(int x,int from){
    d[x] = lo[x] = pl++;
    for(int i = head[x]; i != -1; i = st[i].next){
        int to = st[i].to;
        if(!d[to]){
            tr(to,x);
            lo[x] = min(lo[x],lo[to]);
            if(d[x] < lo[to]){
                sum[ans] = nod{x,to};
                if(x > to)
                    swap(sum[ans].u,sum[ans].v);
                ans++;
            }
        }
        else if(to != from)
            lo[x] = min(lo[x],d[to]);
    }
}
void solve(){
    for(int i = 0; i < n; i++)
        if(!d[i])
            tr(i,-1);
}
int T,x,m,a,cas = 0;
bool cmp(nod i,nod j){
    if(i.u == j.u) return i.v < j.v;
    return i.u < j.u;
}
void output(){
    if(ans) sort(sum,sum + ans,cmp);
    printf("Case %d:\n",++cas);
    printf("%d critical links\n",ans);
    for(int i = 0; i < ans; i++)
        printf("%d - %d\n",sum[i].u,sum[i].v);
}
int main()
{
    scanf("%d",&T);
    while(T--){
        init();
        scanf("%d",&n);
        for(int i = 0; i < n; i++){
            scanf("%d (%d)",&x,&m);
            while(m--){
                scanf("%d",&a);
                add(x,a);
            }
        }
        solve();
        output();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值