题目链接: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. 五、PL/SQL循环、游标、函数和过程

    --PL/SQL基础知识学习 --一.PL/SQL语句块,基础语法格式 DECLARE --变量声明列表 info varchar(25); --变量声明 stu_unm integer := 15; ...

  2. Lucene的例子

    lucene爬数据库中的数据无非也是查询数据.所有我们用lucene搜索数据主要有下面几个步骤:(代码紧供参考)       一  ,  从数据库中查数据 ====爬数据  ------------- ...

  3. 飞扬的小鸟(codevs 3729)

    题目描述 Flappy Bird 是一款风靡一时的休闲手机游戏.玩家需要不断控制点击手机屏幕的频率来调节小鸟的飞行高度,让小鸟顺利通过画面右方的管道缝隙.如果小鸟一不小心撞到了水管或者掉在地上的话,便 ...

  4. 9.6——string类型

    string: getline(is,s):从输入流is读入到字符串s中 s1+s2:将两个字符串连接起来 构造string一些方法: 1)string s(cp,n):将s初始化为cp所指的n个字符 ...

  5. 一个APP的由来

    之前在站酷.UI中国.优设等网站看过不少的APP教程.规范等一些东西.自认为有些规范讲的内容过于繁琐,对于像我这样的大多数设计师来说看着看着就懵逼了....          

  6. hdu——3861 The King’s Problem

    The King’s Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  7. Exception: Could not bind to 0.0.0.0:8080 after trying for 30 seconds

    swift@vincent-virtual-machine /etc/swift $ sudo swift-init main restart Signal proxy-server pid: sig ...

  8. appleid

    https://appleid.apple.com/#!&page=signin

  9. Failed to execute 'toDataURL' on 'HTMLCanvasElement,在canvas.toDataURL()执行时候报错解决方案

    添加跨域条件   crossorigin="anonymous" [Redirect at origin 'http://xxx.xx.com' has been blocked ...

  10. C语言必会面试题(3、耶稣有13个门徒,当中有一个就是出卖耶稣的叛徒,请用排除法找出这位叛徒:13人围坐一圈,从第一个開始报号:1,2,3,1,2,3...。凡是报到“3”就退出圈子,...)

    3.耶稣有13个门徒.当中有一个就是出卖耶稣的叛徒,请用排除法找出这位叛徒:13人围坐一圈,从第一个開始报号:1.2,3.1,2,3.... 凡是报到"3"就退出圈子.最后留在圈子 ...