题目链接: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);
}
}

最新文章

  1. Asp.Net Core 发布和部署(Linux + Jexus )
  2. Java 碰撞的球 MovingBall (整理)
  3. Oracle中的通配符
  4. Windows调试的基石——符号(1)
  5. poj 3253 Fence Repair (哈夫曼树 优先队列)
  6. 将sql数据库逆向生成PDM模型
  7. Cognitive Radio Emergency Networks – Requirements and Design
  8. [原创] 使用rpi + crontab + git 定时向bitbucket 推送 照片
  9. Lua 数据类型和 Redis 数据类型之间转换
  10. iOS 将NSArray、NSDictionary转换为JSON格式进行网络传输
  11. linux—find常见指令用法示例
  12. poj 1743
  13. 【BZOJ3295】动态逆序对(线段树,树状数组)
  14. 修改帝国cms栏目后,如何更新
  15. React Native笔记
  16. 13 ,CSS 入门基础,行内排版内嵌式排版和外部排版样式
  17. Webpack 的 Tree Shaking
  18. 采用boosting思想开发一个解决二分类样本不平衡的多估计器模型
  19. MenOS
  20. Hadoop集群配置(最全面总结 )(转)

热门文章

  1. Java学习关于随机数工具类--Random类
  2. luogu2869 [USACO07DEC]美食的食草动物Gourmet Grazers
  3. Java比较两个数组中的元素是否相同的最简单方法
  4. 大数据学习——linux常用命令(二)四
  5. ZOJ - 3781 Paint the Grid Reloaded 题解
  6. jsonp跨域请求实现示例
  7. 前端接收到的json的属性的首字母会自动变成小写,解决办法如下
  8. SQL SERVER 2012 第四章 连接 JOIN の INNER JOIN
  9. 前端学习之--CSS
  10. Anagrams(hash表)