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. HTTP(一)

    HTTP(一) http php http请求 HTTP请求:请求行.消息报头.请求正文.格式如下: Method Request-URI HTTP-Veraion CRLF 参数说明 Method ...

  2. 沉淀,再出发:Git的再次思考

    沉淀,再出发:Git的再次思考 一.前言 使用git也有很久了,后来有一段时间一直没有机会去使用,现在想来总结一下自己学习了这么长时间的一些心得感悟,我写的博客一般都是开了一个轮廓和框架,等到以后有所 ...

  3. java时间操作工具类

    import java.sql.Timestamp;import java.text.DateFormat;import java.text.ParseException;import java.te ...

  4. fastcgi c/c++ API 说明

    fastcgi c/c++ API 下载地址:https://github.com/FastCGI-Archives 先上example #include <iostream> #incl ...

  5. PHP-----CMS

    刚刚看到了一篇关于phpcms的文章自己今后比较有帮助,就把它摘了下来. 1.首先下载phpcms v9的集成安装包并安装,这里就不详细说明了. 2.本地调试建议大家使用APMserver,或者wam ...

  6. v-bind:的基本用法

    1. v-bind:class(根据需求进行选择) <style> .box{ background-color: #ff0; } .textColor{ color: #000; } . ...

  7. POJ 3216 Prime Path(打表+bfs)

    Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27132   Accepted: 14861 Desc ...

  8. 构建一个hashmap死锁的DEMO

    package threadmodle; import java.util.HashMap; import java.util.Map; import java.util.UUID; public c ...

  9. 【Node.js】新建一个NodeJS 4.X项目

    前提工作 1.安装Node.js 各种下一步就好 2.安装NPM(node package manager) 安装好Node.js之后,打开cmd,输入npm install npm -g,程序会自动 ...

  10. java从mysql读写数据乱码问题

    mysql默认使用latin(ISO-8859-1),而java默认使用unicode编码.因此在JAVA中向MYSQL数据库插入数据时,或者读取数据时,都需要先转换一下编码方式. 1.首先查看数据库 ...