HDU-4612-Warm up(无向图缩点+直径)
2016-04-28 19:15
471 查看
Warm up
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 5623 Accepted Submission(s): 1268
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each case, output the minimal number of bridges after building a new channel in a line.
[align=left]Sample Input[/align]
4 4 1 2 1 3 1 4 2 3 0 0
[align=left]Sample Output[/align]
0
[align=left]Author[/align]
SYSU
[align=left]Source[/align]
2013 Multi-University Training Contest 2
题意让求 ans=缩点后的边数-直径边数。
缩点后 两遍DFS搜直径就好,第一遍DFS求出的点p为最深点或次深点,从p开始第二次DFS求出的点q为次深点或最深点,p到q长度就是直径。
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <iostream> #include <cstdio> #include <cmath> #include <vector> #include <cstring> #include <algorithm> #include <string> #include <set> #include <stack> #include <map> #include <queue> using namespace std; const int MAXM = 1e6+1e6+7; const int M= 1e6+7; const int MAXN = 200007; int head[MAXN],h[MAXN],index; struct node { int v,vis; int next; }edge[MAXM]; vector<int> e[MAXN]; void add(int u, int v) { edge[index].vis=0; edge[index].v=v; edge[index].next=head[u]; head[u]=index++; } int low[MAXN],DFN[MAXN],belong[MAXN]; int stack_[MAXN],top; bool in_stack[MAXN]; int dps,cir; void Tarjan(int u) { in_stack[u]=1; low[u]=DFN[u]=++dps; stack_[top++]=u; for(int i=head[u]; i+1; i=edge[i].next) { int v=edge[i].v; if(edge[i].vis)continue; edge[i].vis=edge[i^1].vis=1; if(!DFN[v]) { Tarjan(v); low[u]=min(low[u],low[v]); } else if(in_stack[v] && edge[i^1].v) low[u]=min(low[u],DFN[v]); } int p; if(low[u]==DFN[u]) { cir++; do{ p=stack_[--top]; in_stack[p]=0; belong[p]=cir; }while(p!=u); } } int MAX_1,MAX_2,poi_1; bool vis[MAXN]; int ans; void DFS(int u,int deep) { vis[u]=1; if(deep>MAX_1)MAX_1=deep,poi_1=u; for(int i=0; i<e[u].size(); i++) { int v=e[u][i]; if(!vis[v]) { DFS(v,deep+1); ans++; } } } int main() { int n,m; while(scanf("%d%d",&n,&m)!=EOF) { if(n==0)break; index=0; memset(head,-1,sizeof(head)); int a,b; for(int i=0; i<m; ++i) { scanf("%d%d",&a,&b); add(a,b); add(b,a); } memset(in_stack,0,sizeof(in_stack)); memset(low,0,sizeof(low)); memset(DFN,0,sizeof(DFN)); dps=top=cir=0; Tarjan(1); memset(h,-1,sizeof(h)); index=0; for(int i=1; i<=n; ++i)e[i].clear(); for(int i=1; i<=n; ++i) for(int j=head[i]; j+1; j=edge[j].next) { int v=edge[j].v; if(belong[i]!=belong[v]) { e[ belong[i] ].push_back(belong[v]); e[ belong[v] ].push_back(belong[i]); } } memset(vis,0,sizeof(vis)); ans=MAX_1=0; DFS(1,0); int temp=ans; memset(vis,0,sizeof(vis)); MAX_1=0; DFS(poi_1,0); cout<<temp-MAX_1<<endl; } return 0; } /* 8 8 1 2 2 3 3 4 1 5 5 6 1 7 7 8 2 5 7 7 1 2 2 3 3 4 1 5 5 6 1 7 7 8 */
相关文章推荐
- 查看Windows 10系统是否永久激活
- 使用springMVC实现文件上传和下载之文件下载
- 【饥荒mod制作吧第五篇】MOD下载及安装
- 如何在服务器上面执行定时程序,在指定的时候执行指定的代码
- 如果转载优酷、土豆视频等,怎么让视频自适应宽度?
- PAT-B 1015. 德才论
- mysql中IN,OR,BETWEEN性能比较
- 使用ThinkPHP框架快速开发网站(多图)
- Python web —— webbrowser + feedparser 网络爬虫刷博器
- angular框架知识积累
- INSTALL_FAILED_SHARED_USER_INCOMPATIBLE的问题解决
- 非常经典的JAVA编程题(判断101-200之间有多少个素数,并输出所有素数。)
- 【SQL Server】SQL注入
- HDU1535 Invitation Cards(最短路)
- POJ-3468-A Simple Problem with Integers
- 团队作业(五)-笔记app top5
- unity3D 彻底搞懂 Quaternion LookRotation FromToRotation SetLookRotation方法
- 0428 团队2.0
- 0428团队项目2.0
- hdu1300 Pearls 【dp】