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. Python初学者第十一天 文件处理_batch

    11day 文件的操作分为读.写.修改 1.读: f = open(file='D:\新建文本文档.txt',mode='r',encoding='gbk') data = f.read() prin ...

  2. Anaconda套件,精簡版miniconda

    雖然Anaconda會預先安裝豐富的套件模組,尤其是在數據科學領域方面,有非常豐富的寶藏, 大多範例或教學或許為了節省後續的麻煩,不解釋為什麼,直接就安裝Anaconda 就對了: 但是大部份的模組套 ...

  3. windows server 2003安装Oracle webtier 32位因环境变量原因报错

    在服务中启动Oracle processer manager时报错:错误1053:服务没有及时响应启动或控制请求 原因是本系统还安装过BI和Oracle数据库等产品 解决方法:删除和本次安装无关的环境 ...

  4. 安装Kali Linux 后需要做的 20 件事 - 51CTO.COM

    我在本文中整理出了安装一份全新的Kali Linux后总是要做的若干件事情.由于我有多台笔记本电脑和工作站,所以尽量扩大下列操作步骤的适用范围,以满足每个人的需求.这是我在安装Kali Linux后做 ...

  5. TB5上正常使用msfconsole

    在TB上使用系统自带的msfconsole,给出以下错误 [-] Failed to connect to the database: could not connect to server: Con ...

  6. Django 数据模型的字段列表整理

    一个模型最重要也是唯一必需的部分,是它定义的数据库字段. 字段名称限制: 1.一个字段名不能是一个Python保留字,因为那样会导致一个Python语法错误. 2.一个字段名不能包含连续的一个以上的下 ...

  7. 牛客网多校训练第一场 F - Sum of Maximum(容斥原理 + 拉格朗日插值法)

    链接: https://www.nowcoder.com/acm/contest/139/F 题意: 分析: 转载自:http://tokitsukaze.live/2018/07/19/2018ni ...

  8. windows8安装msi软件提示2503错误的解决办法

    windows8以后的版本安装msi软件(比如nodejs.msi.Git.msi.python.msi.T ortoiseSVN.msi)的时候老师出现2503.2502的错误,究其原因还是系统权限 ...

  9. HDU 1698 【线段树,区间修改 + 维护区间和】

    题目链接 HDU 1698 Problem Description: In the game of DotA, Pudge’s meat hook is actually the most horri ...

  10. 新一代的IT实验室长啥样?

    这个时代 “互联网”江湖门派众多 “互联网+”现代农业.“互联网+”制造业. “互联网+”信息技术服务.“互联网+”文化创意服务. “互联网+”社会服务 各种“互联网+” 在不断的改变着我们的生活 这 ...