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

    Nginx1.6.3安装配置 安装时关闭防火墙和selinuxservice iptables stopsed -i "s/selinux=enabled/selinux=disable/g ...

  2. 【UTR #2】[UOJ#278]题目排列顺序 [UOJ#279]题目交流通道 [UOJ#280]题目难度提升

    [UOJ#278][UTR #2]题目排列顺序 试题描述 “又要出题了.” 宇宙出题中心主任 —— 吉米多出题斯基,坐在办公桌前策划即将到来的 UOI. 这场比赛有 n 道题,吉米多出题斯基需要决定这 ...

  3. HDU 3973 线段树+字符串hash

    题目大意: 不断修改字符串中的字母,然后询问区间字符串是否处于已给定的字符串集合中 这里将原来的字符串集合保存到hash表中,当然用map,set都没有问题 修改查询都用线段树实现,自己的query函 ...

  4. [Vijos1067]Warcraft III 守望者的烦恼(DP + 矩阵优化)

    传送门 可知 f[i] = f[i - 1] + f[i - 2] + ... + f[i - k] 直接矩阵优化就好了 #include <cstdio> #include <cs ...

  5. 禁止ScrollView在子控件的布局改变时自动滚动的的方法

    重写scrollview中的如下方法,并将其返回值设为0即可. @Override  protected int computeScrollDeltaToGetChildRectOnScreen(Re ...

  6. TOYS(poj 2318)

    题意:就是给了m个点,落在n+1个区域中,问各个区域有多少个点. /* 对于每个玩具,二分其所在的区间,然后用叉积判断. 但是我觉得枚举好像时间复杂度也可以. */ #include<cstdi ...

  7. T1992 聚会 codevs

    http://codevs.cn/problem/1992/  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description 小S 想要从某地 ...

  8. INFO org.apache.hadoop.ipc.RPC: Server at master/192.168.200.128:9000 not available yet, Zzzzz...

    hadoop 启动时namenode和datanode可以启动,使用jps命令也可以看到进程,但是在浏览器中输入master:50070却没有显示datanode 查看datanode的log日志: ...

  9. 框架-弹出选择框(Jquery传递Json数组)

    给一个button按钮,执行方法 Json传值$("body").on("click", "#btnsure", function() {  ...

  10. CSS制作简单图标

    CSS制作图标包括知识点如border-width.border-style.border-color.border-radius.对元素的定位拼接.旋转等结合起来. 图标效果如下(拖动滑块可缩放图标 ...