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. linux下查看某个文件属于哪个包

    1.centos/redhat下查看某个文件或命令属于哪个rpm包: $ yum provides /etc/passwd 或者 $ rpm -qf /etc/passwd 2.ubuntu及衍生版: ...

  2. Python 编码为什么那么蛋疼?

    据说,每个做 Python 开发的都被字符编码的问题搞晕过,最常见的错误就是 UnicodeEncodeError.UnicodeDecodeError,你好像知道怎么解决,遗憾的是,错误又出现在其它 ...

  3. 关于PHP上传文件失败但是找不到原因的问题?

    确定上传文件的前后台代码都准确无误后,可以考虑是不是PHP配置文件中限制了上传文件的大小 1.找到根目录中的PHP文件夹 2.点击当前正在使用的的PHP版本 3.搜索php.ini 4.打开 phpF ...

  4. Linux学习总结(二) 网络配置-NAT方式静态IP配置篇

    一:DHCP 服务验证 1: 之前我们在里面已经装好了centos,当时我们选择的组网方式为NAT方式,那么我们怎么样如何快速实现虚拟机系统访问外网呢?能不能直接上网呢,来我们试一下ping baid ...

  5. #20145238荆玉茗《网络对抗》-逆向及Bof进阶实践

    20145238荆玉茗<网络对抗>-逆向及Bof进阶实践 实践目的:注入shellcode 准备一段shellcode代码 Shellcode实际是一段代码(也可以是填充数据),是用来发送 ...

  6. PAT——1010. 一元多项式求导

    设计函数求一元多项式的导数.(注:xn(n为整数)的一阶导数为n*xn-1.) 输入格式:以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000的整数).数字间以空格分隔. 输出格式:以与 ...

  7. 算法 - 给出一个字符串str,输出包含两个字符串str的最短字符串,如str为abca时,输出则为abcabca

    今天碰到一个算法题觉得比较有意思,研究后自己实现了出来,代码比较简单,如发现什么问题请指正.思路和代码如下: 基本思路:从左开始取str的最大子字符串,判断子字符串是否为str的后缀,如果是则返回st ...

  8. QLViewController在iOS7下的自定义

    原文来自:QLViewController在iOS7下的自定义 原先的项目使用了quicklook framework,用于在iPhone上浏览各类文件,除了txt文本会有乱码的问题,其他文件的显示都 ...

  9. Spring的入门学习笔记 (AOP概念及操作+AspectJ)

    AOP概念 1.aop:面向切面(方面)编程,扩展功能不通过源代码实现 2.采用横向抽取机制,取代了传统的纵向继承重复代码 AOP原理 假设现有 public class User{ //添加用户方法 ...

  10. 简单的mongo小工具 python

    #!/bin/python #coding=utf-8 ### eg : mgotool.py -i 127.0.0.1 -p 10001 -a xxxxx -u root -rc #import s ...