HDOJ4612 Warm up
来源:互联网 发布:win10解压缩软件 编辑:程序博客网 时间:2024/05/29 03:08
思路:缩点,求最长链。重点在于重边的判断。我遇到了Runtime Error(STACK_OVERFLOW)的错误,手动扩栈即解决。
Warm up
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 2543 Accepted Submission(s): 595
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.
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.
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 41 21 31 42 30 0
Sample Output
0
#include <cstdio>#include <algorithm>#include <cstring>#pragma comment(linker,"/STACK:102400000,102400000")//手动扩栈using namespace std;const int N = 200005;int n, m;int head[N], dfn[N], low[N], belong[N];int cnt, scc, index;int cnt2, head2[N];int dis[N];int sta[4000005], top;struct Edge{ int v, next;}edge[2000005], edge2[2000005];void addedge(int u, int v){ edge[cnt].v = v; edge[cnt].next = head[u]; head[u] = cnt++;}void addedge2(int u, int v){ edge2[cnt2].v = v; edge2[cnt2].next = head2[u]; head2[u] = cnt2++;}void tarjan(int u, int fa){ dfn[u] = low[u] = ++index; sta[++top] = u; int multi = 0;//标记重边 for(int i = head[u]; i != -1; i = edge[i].next){ int v = edge[i].v; if(!dfn[v]){ tarjan(v, u); low[u] = min(low[u], low[v]); } else if(fa == v){ if(multi) low[u] = min(low[u], dfn[v]); multi++; } else low[u] = min(low[u],dfn[v]); } if(dfn[u]==low[u]) { int x; scc++; do { x = sta[top--]; belong[x] = scc; }while(x!=u); }}void DFS(int u){ for(int i = head2[u]; i != -1; i = edge2[i].next){ int v = edge2[i].v; if(dis[v] == -1){ dis[v] = dis[u]+1; DFS(v); } }}int main(){ while(scanf("%d %d", &n, &m), (m||n)){ top = 0; cnt = scc = index = 0; memset(head, -1, sizeof(head)); memset(dfn, 0, sizeof(dfn)); memset(low, 0, sizeof(low)); memset(belong, 0, sizeof(belong)); for(int i = 1; i <= m; i++){ int a, b; scanf("%d %d", &a, &b); if(a == b) continue; addedge(a, b); addedge(b, a); } tarjan(1, -1); //建新图 cnt2 = 0; memset(head2, -1, sizeof(head2)); for(int u = 1; u <= n; u++){ for(int i = head[u]; i != -1; i = edge[i].next){ int v = edge[i].v; if(belong[u] != belong[v]){ addedge2(belong[u], belong[v]); } } } memset(dis, -1, sizeof(dis)); dis[1] = 0; DFS(1); int tmp = 0; int s; for(int i = 1; i <= scc; i++){ if(tmp < dis[i]){ tmp = dis[i]; s = i; } } memset(dis, -1, sizeof(dis)); dis[s] = 0; DFS(s); int len = 0; for(int i = 1; i <= scc; i++){ if(len < dis[i]){ len = dis[i]; } } printf("%d\n", scc - len - 1); } return 0;}
- HDOJ4612 Warm up
- [Warm-up] Homework
- [Introduction and Warm-up]
- hdu 4612 Warm up
- HDU 4612 Warm up
- HDOJ 4612: Warm up
- hdu 4612 Warm up
- D - Warm up 2
- hdu 4612Warm up
- CSU Warm Up 2
- Warm UP for Changsha
- Warm-Up-Problem
- HDOJ 4612 Warm up
- HDU 4612 Warm up
- HDU 4612 Warm up
- Warm up HDU
- hdu 4612 Warm up
- Warm up HDU
- Category(范畴)的使用方法与介绍
- UVA 10954 Add All 全加一起 模拟+贪心+优先队列
- GBK与GB2312
- 获取加载Kernel32.dll的地址
- 如何在客户端配置ODBC来访问远程DB2 for Windows服务器
- HDOJ4612 Warm up
- java中volatile关键字的含义
- 《编程之美》1.2象棋问题
- HDU1045 Fire Net
- 第三章 AXF (ActXmlFromFramework) 活动ANDROID页面展现框架 android动态加载页面框架
- 逆波兰计算器,实现了对exp、sin、pow等的支持(TCPL 练习4-5)
- 最近链接20130830
- 墨半成霜为你深入剖析UITableView SDK
- 怎么发外链,发外链有什么技巧?