当前位置: 代码迷 >> 综合 >> Light OJ 1026 求桥
  详细解决方案

Light OJ 1026 求桥

热度:47   发布时间:2024-01-12 20:34:22.0

题目链接:

http://lightoj.com/volume_showproblem.php?problem=1026

1026 - Critical Links

    PDF (English) Statistics Forum
Time Limit: 2 second(s) Memory Limit: 32 MB

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 - 13 - 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

Output for 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

Case 1:

3 critical links

0 - 1

3 - 4

6 - 7

Case 2:

0 critical links

Case 3:

1 critical links

0 - 1

Note

Dataset is huge, use faster I/O methods.


SPECIAL THANKS: JANE ALAM JAN (MODIFIED DESCRIPTION, DATASET)

 题目大意:

给你一些边,让求桥,给的是单项边,但是给给两次,相当于 是双向边,不要以一次性的储存两次边

思路:

注意输入,然后裸的tarjan算法,注意图不一定联通,然后注意输出顺序

This is the code

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x7f7f7f7f      //2139062143
#define LL_INF 0x7f7f7f7f7f7f7f7f //9187201950435737471
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int dr[]={0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]={-1, 1, 0, 0, -1, 1, -1, 1};
const int maxn=20005;
struct Edge
{int x,y;Edge(){	}Edge(int x,int y){this->x=min(x,y);this->y=max(x,y);}
};
vector<Edge> edge;//储存桥
vector<int> G[maxn];
int dfn[maxn],low[maxn];
bool vis[maxn];
int dfs_clock;
void init(int n)
{for(int i=0;i<=n;++i){G[i].clear();dfn[i]=0;low[i]=0;vis[i]=false;//注意这个vis数组很重要}dfs_clock=0;edge.clear();
}void tarjan(int u,int fa)
{low[u]=dfn[u]=++dfs_clock;vis[u]=true;for(int i=0; i<G[u].size(); i++){int v=G[u][i];if(v==fa) //不能相等,形成环continue;if(!dfn[v]){tarjan(v,u);low[u]=min(low[u],low[v]);if(low[v]>dfn[u])edge.push_back(Edge(u,v));//储存桥}else if(vis[v])low[u] = min(low[u],dfn[v]);}
}
bool cmp(Edge a, Edge b)
{if(a.x==b.x)return a.y<b.y;return a.x<b.x;
}int main()
{int T;scanf("%d",&T);for(int t=1;t<=T;++t){int n,m;scanf("%d",&n);init(n);for(int i=0;i<n;++i){int u,v;scanf("%d (%d)",&u,&m);for(int j=0;j<m;++j){scanf("%d",&v);G[u].push_back(v);}}for(int i=1;i<=n;++i)if(!dfn[i])tarjan(i,-1);sort(edge.begin(),edge.end(),cmp);printf("Case %d:\n",t);printf("%d critical links\n",(int)edge.size());for(int i=0;i<edge.size();++i){printf("%d - %d\n",edge[i].x,edge[i].y);}
、}return 0;
}

 

 

  相关解决方案