题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3394
Railway
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2780 Accepted Submission(s): 953
Problem Description
There are some locations in a park, and some of them are connected by roads. The park manger needs to build some railways along the roads, and he would like to arrange tourist routes to each circuit. If a railway belongs to more than one tourist routes, there might be clash on it, and if a railway belongs to none tourist route, it doesn’t need to build.
Now we know the plan, and can you tell us how many railways are no need to build and how many railways where clash might happen.
Now we know the plan, and can you tell us how many railways are no need to build and how many railways where clash might happen.
Input
The Input consists of multiple test cases. The first line of each test case contains two integers, n (0 < n <= 10000), m (0 <= m <= 100000), which are the number of locations and the number of the railways. The next m lines, each line contains two integers, u, v (0 <= u, v < n), which means the manger plans to build a railway on the road between u and v.
You can assume that there is no loop and no multiple edges.
The last test case is followed by two zeros on a single line, which means the end of the input.
You can assume that there is no loop and no multiple edges.
The last test case is followed by two zeros on a single line, which means the end of the input.
Output
Output the number of railways that are no need to build, and the number of railways where clash might happen. Please follow the format as the sample.
Sample Input
8 10 0 1 1 2 2 3 3 0 3 4 4 5 5 6 6 7 7 4 5 7 0 0
Sample Output
1 5
Author
momodi@whu
Source
题解:
此题作为学习点双联通分量的模板题。
代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+7;
const int MAXN = 1e4+10;
struct Edge
{
int from, to, next;
}edge[MAXN*20];
int head[MAXN], tot;
int index, dfn[MAXN], low[MAXN], Stack[MAXN*20];
int bridge, conflict, top;
set<int>Set;
void add(int u, int v)
{
edge[tot].from = u;
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
}
void Tarjan(int u, int pre)
{
dfn[u] = low[u] = ++index;
for(int i = head[u]; i!=-1; i = edge[i].next)
{
int v = edge[i].to;
if(v==pre) continue;
if(!dfn[v])
{
Stack[top++] = i;
Tarjan(v, u);
low[u] = min(low[u], low[v]);
if(low[v]>=dfn[u]) //割点
{
int cnt = 0;
Set.clear();
int id;
do
{
id = Stack[--top];
Set.insert(edge[id].from);
Set.insert(edge[id].to);
cnt++;
}while(edge[id].from!=u || edge[id].to!=v);
if(cnt>Set.size())
conflict += cnt;
}
if(low[v]>dfn[u]) //桥
bridge++;
}
else
{
low[u] = min(low[u], dfn[v]);
if(dfn[v]<dfn[u]) //如果遇到祖先,则把此边压入栈(如果遇到子孙,则不用,因为之前在子孙时已经入栈)
Stack[top++] = i;
}
}
}
void init()
{
memset(head, -1, sizeof(head));
memset(dfn, 0, sizeof(dfn));
memset(low, 0, sizeof(low));
index = top = tot = 0;
bridge = conflict = 0;
}
int main()
{
int n, m;
while(scanf("%d%d",&n,&m) && (n||m) )
{
init();
for(int i = 1; i<=m; i++)
{
int u, v;
scanf("%d%d",&u,&v);
add(u, v);
add(v, u);
}
for(int i = 0; i<n; i++)
if(!dfn[i])
Tarjan(i, -1);
printf("%d %d\n", bridge, conflict);
}
}