标签:
#pragma comment(linker, "/STACK:102400000,102400000") #include <iostream> #include <cstdlib> #include <cstdio> #include <algorithm> #include <vector> #include <queue> #include <cmath> #include <stack> #include <cstring> usingnamespace std; #define INF 0xfffffff #define maxn 200005 #define min(a,b) (a<b?a:b) int m, n, Time, top, cnt; int blocks[maxn], dfn[maxn], low[maxn], Stacks[maxn], Father[maxn]; int step[maxn]; vector<vector<int> > G; vector<vector<int> > G2; void init() { memset(dfn, 0, sizeof(dfn)); memset(low, 0, sizeof(low)); memset(blocks, 0, sizeof(blocks)); Time = top = cnt = 0; G.clear(); G.resize(n+2); G2.clear(); G2.resize(n+2); } void Tarjan(int u,int fa) { dfn[u] = low[u] = ++Time; Stacks[top ++] = u; Father[u] = fa; int len = G[u].size(), k = 0, v; for(int i=0; i<len; i++) { v = G[u][i]; if( !k && fa == v) { k ++; continue; } if( !low[v] ) { Tarjan(v, u); low[u] = min(low[u], low[v]); } else low[u] = min(low[u], dfn[v]); } if(dfn[u] == low[u]) { do { v = Stacks[--top]; blocks[v] = cnt; }while(u != v); cnt ++; } } int BFS(int s,int flag) { int P, Pn; queue<int> Q; Q.push(s); memset(step, -1, sizeof(step)); step[s] = 0; while( Q.size() ) { P = Q.front(); Q.pop(); int len = G2[P].size(); for(int i=0; i<len; i++) { Pn = G2[P][i]; if( step[Pn] == -1) { step[Pn] = step[P] + 1; Q.push(Pn); } } } if(flag == 1) return P; return step[P]; } void solve() { int ans = 0, i; for(i=1; i<=n; i++) { if(!low[i]) Tarjan(i, i); } for(i=1; i<=n; i++) { int v = Father[i]; if(blocks[i] != blocks[v]) { /**重新构图*/ G2[blocks[i] ].push_back(blocks[v]); G2[blocks[v] ].push_back(blocks[i]); } } int p = BFS(0,1);///以0为起点经行一次BFS返回最远距离的编号 ans = cnt - BFS(p, 2) - 1;///返回最远距离的长度 printf("%d\n", ans); } int main() { while(scanf("%d %d",&n, &m), m+n) { init(); while(m--) { int a, b; scanf("%d %d",&a, &b); G[a].push_back(b); G[b].push_back(a); } solve(); } return0; } /* 5 4 1 2 1 3 1 4 2 5 */
HDU 4612 Warm up(手动扩栈,求树上哪两个点的距离最远)
标签:
原文地址:http://www.cnblogs.com/chenchengxun/p/4718740.html