题目链接: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.
 
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.
 
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

题解:

1.首先,我们可以知道桥即为多余的边。

2.然后,哪些是冲突的边呢?根据题目的意思,当一条边存在于多个回路中时,这条边即为冲突边。

3.我们可以继续得出结论:对于一个点双联通子图,如果边的个数等于点的个数,那么该点双联通子图刚好形成一个环;如果边的个数大于点的个数,那么该点双联通子图至少存在三个环,且每一条边都至少存在于两个环中,所以该子图的所有边都为冲突边。

4.为什么不是边双联通子图呢?有点难解释,画画图就可以看出来了。

存边(可以求出分量中的边):

 #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+;
const int MAXN = 1e4+; struct Edge
{
int from, to, next;
}edge[MAXN*];
int head[MAXN], tot; int index, dfn[MAXN], low[MAXN];
int top, Stack[MAXN*];
int bridge, conflict;
set<int>Set; void addedge(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!=-; 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 = ;
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()
{
tot = ;
memset(head, -, sizeof(head)); index = top = ;
memset(dfn, , sizeof(dfn));
memset(low, , sizeof(low)); bridge = conflict = ;
} int main()
{
int n, m;
while(scanf("%d%d",&n,&m) && (n||m) )
{
init();
for(int i = ; i<=m; i++)
{
int u, v;
scanf("%d%d",&u,&v);
addedge(u, v);
addedge(v, u);
} for(int i = ; i<n; i++)
if(!dfn[i])
Tarjan(i, i); printf("%d %d\n", bridge, conflict);
}
}

存点:

 #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+;
const int MAXN = 1e4+; struct Edge
{
int from, to, next;
}edge[MAXN*];
int head[MAXN], tot; int index, dfn[MAXN], low[MAXN];
int top, Stack[MAXN*], instack[MAXN];
int bridge, conflict;
set<int>Set; void addedge(int u, int v)
{
edge[tot].from = u;
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
} int count_edge()
{
int cnt = ;
for(set<int>::iterator it = Set.begin(); it!=Set.end(); it++)
for(int i = head[*it]; i!=-; i = edge[i].next)
if(*it<edge[i].to && Set.count(edge[i].to) )
cnt++;
return cnt;
} void Tarjan(int u, int pre)
{
dfn[u] = low[u] = ++index;
Stack[top++] = u;
instack[u] = true;
for(int i = head[u]; i!=-; i = edge[i].next)
{
int v = edge[i].to;
if(v==pre) continue;
if(!dfn[v])
{
Tarjan(v, u);
low[u] = min(low[u], low[v]);
if(low[v]>=dfn[u]) //割点
{
Set.clear();
int tmpv;
do
{
tmpv = Stack[--top];
instack[tmpv] = false;
Set.insert(tmpv);
}while(tmpv!=v);
Set.insert(u); int cnt = count_edge();
if(cnt>Set.size())
conflict += cnt;
} if(low[v]>dfn[u]) //桥
bridge++;
}
else if(instack[v])
low[u] = min(low[u], dfn[v]);
}
} void init()
{
tot = ;
memset(head, -, sizeof(head)); index = top = ;
memset(dfn, , sizeof(dfn));
memset(low, , sizeof(low));
memset(instack, false, sizeof(instack)); bridge = conflict = ;
} int main()
{
int n, m;
while(scanf("%d%d",&n,&m) && (n||m) )
{
init();
for(int i = ; i<=m; i++)
{
int u, v;
scanf("%d%d",&u,&v);
addedge(u, v);
addedge(v, u);
} for(int i = ; i<n; i++)
if(!dfn[i])
Tarjan(i, i); printf("%d %d\n", bridge, conflict);
}
}

HDU3394 Railway —— 点双联通分量 + 桥(割边)的更多相关文章

  1. [HDOJ4738]Caocao's Bridges(双联通分量,割边,tarjan)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738 给一张无向图,每一条边都有权值.找一条割边,使得删掉这条边双连通分量数量增加,求权值最小那条. ...

  2. Tarjan 强连通分量 及 双联通分量(求割点,割边)

    Tarjan 强连通分量 及 双联通分量(求割点,割边) 众所周知,Tarjan的三大算法分别为 (1)         有向图的强联通分量 (2)         无向图的双联通分量(求割点,桥) ...

  3. POJ 3177 Redundant Paths 双联通分量 割边

    http://poj.org/problem?id=3177 这个妹妹我大概也曾见过的~~~我似乎还没写过双联通分量的blog,真是智障. 最少需要添多少条边才能使这个图没有割边. 边双缩点后图变成一 ...

  4. HDU4738 Caocao's Bridges —— 边双联通分量 + 重边

    题目链接:https://vjudge.net/problem/HDU-4738 A network administrator manages a large network. The networ ...

  5. POJ 3694Network(Tarjan边双联通分量 + 缩点 + LCA并查集维护)

    [题意]: 有N个结点M条边的图,有Q次操作,每次操作在点x, y之间加一条边,加完E(x, y)后还有几个桥(割边),每次操作会累积,影响下一次操作. [思路]: 先用Tarjan求出一开始总的桥的 ...

  6. 【UVA10972】RevolC FaeLoN (求边双联通分量)

    题意: 给你一个无向图,要求把所有无向边改成有向边,并且添加最少的有向边,使得新的有向图强联通. 分析: 这题的解法还是很好想的.先用边双联通分量缩点,然后找新图中入度为0和为1的点,入度为0则ans ...

  7. lightoj 1300 边双联通分量+交叉染色求奇圈

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1300 边双连通分量首先dfs找出桥并标记,然后dfs交叉着色找奇圈上的点.这题只要求在 ...

  8. HDU5409---CRB and Graph 2015多校 双联通分量缩点

    题意:一个联通的无向图, 对于每一条边, 若删除该边后存在两点不可达,则输出这两个点, 如果存在多个则输出第一个点尽可能大,第二个点尽可能小的. 不存在输出0 0 首先 若删除某一条边后存在多个联通分 ...

  9. poj2942(双联通分量,交叉染色判二分图)

    题意:一些骑士,他们有些人之间有矛盾,现在要求选出一些骑士围成一圈,圈要满足如下条件:1.人数大于1.2.总人数为奇数.3.有仇恨的骑士不能挨着坐.问有几个骑士不能和任何人形成任何的圆圈. 思路:首先 ...

随机推荐

  1. 今年把js总结了一下,ppt格式的

    本来想梳理成html,但是时间有限. 希望能够有所帮助. http://pan.baidu.com/s/1ntGAfED http://files.cnblogs.com/danghuijian/js ...

  2. Leetcode 260.只出现一次的数字III

    只出现一次的数字III 给定一个整数数组 nums,其中恰好有两个元素只出现一次,其余所有元素均出现两次. 找出只出现一次的那两个元素. 示例 : 输入: [1,2,1,3,2,5] 输出: [3,5 ...

  3. UltraEdit-14.10.0.1024版本语法着色配置

    用了UltraEdit有段时间了,一直没做语法着色,当做普通文本编辑器使用,这也太委屈这个“神器”了. 今天就让它物尽其用吧.体验一把UltraEdit的语法高亮功能. 参考:http://www.1 ...

  4. 逆序对数列(BZOJ 2431)

    题目描述 对于一个数列{ai},如果有i<j且ai>aj,那么我们称ai与aj为一对逆序对数.若对于任意一个由1~n自然数组成的数列,可以很容易求出有多少个逆序对数.那么逆序对数为k的这样 ...

  5. 洛谷 P 1018 乘积最大 ==Codevs

    题目描述 今年是国际数学联盟确定的“2000――世界数学年”,又恰逢我国著名数学家华罗庚先生诞辰90周年.在华罗庚先生的家乡江苏金坛,组织了一场别开生面的数学智力竞赛的活动,你的一个好朋友XZ也有幸得 ...

  6. 「CodePlus 2018 3 月赛」白金元首与克劳德斯

    所有的云在此时没有重叠的面积 所有的云在此时没有重叠的面积 所有的云在此时没有重叠的面积 所有的云在此时没有重叠的面积 所有的云在此时没有重叠的面积 所有的云在此时没有重叠的面积 所有的云在此时没有重 ...

  7. jQuery根据属性模糊匹配元素

    1.查看带有指定属性的元素: [attribute] 例如: $("div[id]") 2.查看属性值是某个特定值的元素: [attribute=value] 例如: $(&quo ...

  8. msp430入门编程24

    msp430中C语言的扩展--段的使用 msp430入门学习 msp430入门编程

  9. TimePickerDialog

    package com.pingyijinren.helloworld.activity; import android.app.TimePickerDialog; import android.su ...

  10. SQL SERVER 2012 第四章 连接 JOIN の INNER JOIN

    所有JOIN语句的共同点是:将一个记录与另外一个或多个记录匹配,从而生成一个新记录,这个记录是由两个记录的合并列所产生的一个超集. 内部连接: 内部连接语法结构:SELECT <select l ...