Railway

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3655    Accepted Submission(s): 1219

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
题意: 一个无向图有n个点和m条边(可能不连通).现在要你找出冲突边和多余边的数目.其中冲突边是同时存在于多个环中的边,而多余边是不在环中的边.
思路:

1.首先多余边就是桥.冲突边只能在点双连通分量中,而什么样的点双连通分量有冲突边呢?

2.对于有n个节点和n条边(或小于n条边)的点双连通分量,这种分量只有一个大环,不存在其他任何环了,所以这种分量中的边都不是冲突边.

3.对于有n个节点和m条边(m>n)的点双连通分量来说,该分量内的所有边都是冲突边.因为边数>点数,所以该分量必有至少两个环,我们随便画个图就可知其中的任意边都至少在两个以上的环上.

综上所述,对于多余边,我们输出桥数.对于冲突边,我们输出边数>点数的点双连通分量的所有边数.

代码:
 #include"bits/stdc++.h"
#define rep(i, a, n) for (int i=a;i<n;i++)
#define per(i, a, n) for (int i=n-1;i>=a;i--)
const int N=;
const int M=;
using namespace std;
int low[N], dfn[N], st[N];
int id, top;
bool vis[N];
int a[N], cc;
int n, m;
int ans1, ans2;
struct P{
int to,next;
}e[M<<];
int head[N],cnt;
void add(int u,int v){
e[cnt].to = v;
e[cnt].next = head[u];
head[u] = cnt++;
}
void dfs(int u,int pre){
low[u]=dfn[u]=++id;
st[++top]=u;
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].to;
if(v==pre) continue;
if(!dfn[v]){
dfs(v,u);
if(low[u]>low[v]) low[u]=low[v];
if(low[v]>dfn[u]) ans1++;//桥
if(low[v]>=dfn[u]){
cc=;
memset(vis,,sizeof(vis));
int x;
do{
x=st[top--];
a[cc++]=x;//双连通分量中的点存起来
vis[x]=;//标记
}while(x!=v);
a[cc++]=u;
vis[u]=;
cal();
}
}
else if(low[u]>dfn[v]) low[u]=dfn[v];
}
} void cal(){
int sum=;
for(int i=;i<cc;++i){
int u=a[i];
for(int j=head[u];j!=-;j=e[j].next){
int v=e[j].to;
if(vis[v]) sum++;//统计双连通分量内的边数
}
}
sum/=;
if(sum>cc) ans2+=sum;//若边数大于点数,则为多个环
}
void init(){
cnt=id=top=;
ans1=ans2=;
memset(head,-,sizeof(head));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
}
int main()
{ int u,v;
while(scanf("%d%d",&n,&m)==&&n||m){
init();
while(m--){
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
for(int i=;i<n;i++) if(!dfn[i]) dfs(i,-);
printf("%d %d\n",ans1,ans2);
}
return ;
}

HDU3394 点双连通分量的更多相关文章

  1. POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]

    Knights of the Round Table Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 12439   Acce ...

  2. 【Codefoces487E/UOJ#30】Tourists Tarjan 点双连通分量 + 树链剖分

    E. Tourists time limit per test: 2 seconds memory limit per test: 256 megabytes input: standard inpu ...

  3. 【BZOJ-2730】矿场搭建 Tarjan 双连通分量

    2730: [HNOI2012]矿场搭建 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1602  Solved: 751[Submit][Statu ...

  4. hihoCoder 1184 连通性二·边的双连通分量

    #1184 : 连通性二·边的双连通分量 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在基本的网络搭建完成后,学校为了方便管理还需要对所有的服务器进行编组,网络所的老 ...

  5. HDU 5458 Stability(双连通分量+LCA+并查集+树状数组)(2015 ACM/ICPC Asia Regional Shenyang Online)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 Problem Description Given an undirected connecte ...

  6. 点/边 双连通分量---Tarjan算法

    运用Tarjan算法,求解图的点/边双连通分量. 1.点双连通分量[块] 割点可以存在多个块中,每个块包含当前节点u,分量以边的形式输出比较有意义. typedef struct{ //栈结点结构 保 ...

  7. Tarjan应用:求割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)【转】【修改】

    一.基本概念: 1.割点:若删掉某点后,原连通图分裂为多个子图,则称该点为割点. 2.割点集合:在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成 ...

  8. poj3177 && poj3352 边双连通分量缩点

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12676   Accepted: 5368 ...

  9. 【POJ 2942】Knights of the Round Table(点双连通分量,二分图染色)

    圆桌会议必须满足:奇数个人参与,相邻的不能是敌人(敌人关系是无向边). 求无论如何都不能参加会议的骑士个数.只需求哪些骑士是可以参加的. 我们求原图的补图:只要不是敌人的两个人就连边. 在补图的一个奇 ...

随机推荐

  1. calculate fraction by oracle

    QUESTION:When you meet calculate fraction in oracle SOLUTION: 1.Check out their values respectively. ...

  2. 150行JavaScript代码实现增强现实

    增强现实技术(Augmented Reality,简称 AR),是一种实时地计算摄影机影像的位置及角度并加上相应图像.视频.3D模型的技术,这种技术的目标是在屏幕上把虚拟世界套在现实世界并进行互动.这 ...

  3. 网站渗透常用到的Python小脚本

    0×00渗透的很多时候,找到的工具并不适用,自己码代码才是王道,下面三个程序都是渗透时在网络上找不到合适工具,自己辛苦开发的,短小使用,求欣赏,求好评. 0×01记录root密码小工具 root.py ...

  4. 为什么说 Java 程序员必须掌握 Spring Boot ?

    原作者https://www.cnblogs.com/ityouknow/p/9175980.html Spring Boot 2.0 的推出又激起了一阵学习 Spring Boot 热,就单从我个人 ...

  5. C#图解教程读书笔记(第8章 表达式和运算符)

    表达式 字面量 整数字面量 字符字面量 字符串字面量 求值顺序 优先级 结合性 与C和C++不同,在C#中的数字不具有布尔意义. 各种运算符的作用(过) 用户定义类型转换 class XiXiInt ...

  6. [HNOI2007]紧急疏散EVACUATE

    嘟嘟嘟 看数据范围,第一反应觉得爆搜是不是能骗点分,但发现爆搜太难写了,于是就开始想想正解…… 正解大概猜到了是网络流,但是怎么把时间这个条件加入到图的内容中,却困扰了我好半天,总是感觉把这种不同维度 ...

  7. python入门基础:文件的读写

    文件的读写操作运用广泛,无论是何种语言都会涉及到文件的输入输出. 下面简单的总结一下文件的读写: 1:open()函数 f = open('workfile', 'w') 函数 open()返回文件的 ...

  8. java 编写小工具 尝试 学习(五)

    1.今天 学习 标签 的 控件 的使用 ,学习 视频教程 参考  :http://edu.51cto.com/lesson/id-17733.html 常用控件如下截图: import javax.s ...

  9. react native基本调试技巧

    刚入坑RN,很多小坑都要摸索很久才明白.今天咱们就来填console.log()的坑. 废话不多说,开始讲步骤!! 1.在模拟器中打开 开发者菜单,选择 Debug JS Remotely,会自动在浏 ...

  10. 搭建Hadoop2.6.0+Spark1.1.0集群环境

    前几篇文章主要介绍了单机模式的hadoop和spark的安装和配置,方便开发和调试.本文主要介绍,真正集群环境下hadoop和spark的安装和使用. 1. 环境准备 集群有三台机器: master: ...