题目链接

Warm up

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 5353    Accepted Submission(s): 1195

Problem Description
  N planets are connected by M bidirectional channels that allow instant transportation. It's always possible to travel between any two planets through these channels.
  If we can isolate some planets from others by breaking only one channel , the channel is called a bridge of the transportation system.
People don't like to be isolated. So they ask what's the minimal number of bridges they can have if they decide to build a new channel.
  Note that there could be more than one channel between two planets.
 
Input
  The input contains multiple cases.
  Each case starts with two positive integers N and M , indicating the number of planets and the number of channels.
  (2<=N<=200000, 1<=M<=1000000)
  Next M lines each contains two positive integers A and B, indicating a channel between planet A and B in the system. Planets are numbered by 1..N.
  A line with two integers '0' terminates the input.
 
Output
  For each case, output the minimal number of bridges after building a new channel in a line.
 
Sample Input
4 4
1 2
1 3
1 4
2 3
0 0
 
Sample Output
0
给一个图, 求增加一条边之后的桥的数量最少是多少。有重边
 
缩点然后找树的直径, 答案就是缩点之后的边数-直径。
tarjan的时候注意, vis数组记录访问过的边的编号而不是点。
找树的直径最好写bfs, dfs据说爆栈
详细看代码。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define fi first
#define se second
typedef pair<int, int> pll;
const int inf = ;
const int maxn = 2e5+;
const int maxe = 1e6+;
int head[maxn], head1[maxn], dis[maxn], num, num1, top, cnum, instack[maxn], st[maxn], dfn[maxn], low[maxn], s[maxn];
int maxx, pos, vis[maxe*], cnt;
struct node
{
int to, nextt;
}e[maxe*], e1[maxe*];
void add(int u, int v) {
e[num].to = v, e[num].nextt = head[u], head[u] = num++;
}
void add1(int u, int v) {
e1[num1].to = v, e1[num1].nextt = head1[u], head1[u] = num1++;
}
void init() {
num = num1 = cnt = cnum = top = ;
mem1(head);
mem(s);
mem(vis);
mem1(head1);
mem(instack);
mem(st);
mem(dfn);
mem(low);
mem(dis);
}
pll edge[maxe];
void tarjan(int u) {
instack[u] = ;
st[top++] = u;
dfn[u] = low[u] = ++cnt;
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(vis[i])
continue;
vis[i] = vis[i^] = ;
if(!dfn[v]) {
tarjan(v);
low[u] = min(low[u], low[v]);
} else if(instack[v]) {
low[u] = min(low[u], dfn[v]);
}
}
if(low[u] == dfn[u]) {
++cnum;
int x;
do {
x = st[--top];
instack[x] = ;
s[x] = cnum;
} while(x != u);
}
}
void bfs(int u) {
queue <int> q;
q.push(u);
mem2(dis);
dis[u] = ;
mem(vis);
vis[u] = ;
maxx = , pos = u;
while(!q.empty()) {
int v = q.front(); q.pop();
for(int i = head1[v]; ~i; i = e1[i].nextt) {
int ve = e1[i].to;
if(vis[ve])
continue;
vis[ve] = ;
dis[ve] = dis[v]+;
if(dis[ve]>maxx) {
maxx = dis[ve];
pos = ve;
}
q.push(ve);
}
}
}
int main()
{
int n, m, x, y;
while(cin>>n>>m) {
if(n+m==)
break;
init();
for(int i = ; i<m; i++) {
scanf("%d%d", &x, &y);
edge[i].fi = x, edge[i].se = y;
add(x, y);
add(y, x);
}
tarjan();
int edgenum = ;
for(int i = ; i<m; i++) {
int x = edge[i].fi, y = edge[i].se;
if(s[x]!=s[y]) {
add1(s[x], s[y]);
add1(s[y], s[x]);
edgenum++;
}
}
bfs(s[]);
bfs(pos);
int ans = edgenum-maxx;
printf("%d\n", ans);
}
return ;
}

最新文章

  1. [LeetCode] Nth Highest Salary 第N高薪水
  2. operating system
  3. 解决jQuery ajax跨域问题,Google、IE、Firefox亲测有效
  4. phpinfo() 中 Local Value(局部变量)Master Value(主变量) 的区别
  5. 【转载】Linux下编辑生成.mo文件
  6. SQL触发器
  7. 《linux备份与恢复之二》3.19 dump(文件系统备份)
  8. css中的盒子模型
  9. SQL判断汉字
  10. javaee学习-servlet初始化参数
  11. HDU 1180 诡异的楼梯(BFS)
  12. Net分布式系统
  13. 使用apache反向代理tomacat
  14. c/c++ 继承与多态 子类隐藏父类的同名非虚函数
  15. PVID和VID彻底研究(上) ——PVID的作用及和VID的区别
  16. python 基本模块 random、os、sys
  17. Kaldi的nnet3
  18. Idea中快捷键与小技巧的总结--&gt;持续更新
  19. Android分享到微信时点击分享无反应的问题解决(注意事项)
  20. Windows平台下在服务中添加MySQL

热门文章

  1. 动画原理——绘制正弦函数&amp;环绕运动&amp;椭圆运动
  2. 执行CMD代码
  3. Ubuntu引导修复问题
  4. C#中的线程(一)入门 转
  5. 教你wamp下多域名如何配置
  6. EC读书笔记系列之17:条款41、42、43、44、45、46
  7. centos6安装vncserver实现图形化访问
  8. MongoDB(二)
  9. 【翻译】MVC Music Store 教程-概述(三)
  10. 收藏的技术文章链接(ubuntu,python,android等)