【树的直径】 POJ 2631 Roads in the North
2014-10-12 19:00
316 查看
转自 DISCUSS的证明
设最长链是MN->已知[1]
设由B开始DFS得到最长路为BC->已知[2]
结论[1] MN与AB有公共点.否则MN<AM+AN<=AM+AB=BM 与已知[1]矛盾
结论[2] B是最长链的一个端点.否则由结论[1] 设K是AB上距B最近且在MN上的点 则MN=MK+KN=MK+AN-AK<=MK+AB-AK=MK+BK=BM 当取等号时MB与MN等长 符合结论[2] 否则与已知[1]矛盾 [这里假定了A不在NK上.若A在NK上 只须将上面式子中MN交换位置即可 不影响结论]
结论[3] BC是一条最长链.否则由结论[2] 设经过B的最长链为BD 则BD>BC 与已知[2]矛盾
至此证毕
(数据居然可能为空。。。)
设最长链是MN->已知[1]
设由B开始DFS得到最长路为BC->已知[2]
结论[1] MN与AB有公共点.否则MN<AM+AN<=AM+AB=BM 与已知[1]矛盾
结论[2] B是最长链的一个端点.否则由结论[1] 设K是AB上距B最近且在MN上的点 则MN=MK+KN=MK+AN-AK<=MK+AB-AK=MK+BK=BM 当取等号时MB与MN等长 符合结论[2] 否则与已知[1]矛盾 [这里假定了A不在NK上.若A在NK上 只须将上面式子中MN交换位置即可 不影响结论]
结论[3] BC是一条最长链.否则由结论[2] 设经过B的最长链为BD 则BD>BC 与已知[2]矛盾
至此证毕
(数据居然可能为空。。。)
#include <cstdio> #include <cstdlib> #include <cstring> #include <climits> #include <cctype> #include <cmath> #include <string> #include <sstream> #include <iostream> #include <algorithm> #include <iomanip> using namespace std; #include <queue> #include <stack> #include <vector> #include <deque> #include <set> #include <map> typedef long long LL; typedef long double LD; #define pi acos(-1.0) #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 typedef pair<int, int> PI; typedef pair<int, PI> PP; #ifdef _WIN32 #define LLD "%I64d" #else #define LLD "%lld" #endif const int MAXN = 20100; const int INF = 999999; //LL quick(LL a, LL b){LL ans=1;while(b){if(b & 1)ans*=a;a=a*a;b>>=1;}return ans;} //inline int read(){char ch=' ';int ans=0;while(ch<'0' || ch>'9')ch=getchar();while(ch<='9' && ch>='0'){ans=ans*10+ch-'0';ch=getchar();}return ans;} //inline void print(LL x){printf(LLD, x);puts("");} //inline void read(int &x){char c = getchar();while(c < '0') c = getchar();x = c - '0'; c = getchar();while(c >= '0'){x = x * 10 + (c - '0'); c = getchar();}} struct Edge { int to,next,val; } edge[MAXN*2]; int head[MAXN],d[MAXN],tol; bool vis[MAXN]; void init() { tol=0; memset(head,-1,sizeof(head)); memset(d,0,sizeof(d)); } void addedge(int u,int v,int w) { edge[tol].to=v,edge[tol].val=w,edge[tol].next=head[u]; head[u]=tol++; edge[tol].to=u,edge[tol].val=w,edge[tol].next=head[v]; head[v]=tol++; } int bfs(int u) { int point=0,big=0; memset(vis,false,sizeof(vis)); queue<int>q; q.push(u); vis[u]=true; d[u]=0; while(!q.empty()) { u=q.front(); q.pop(); for(int i=head[u]; ~i; i=edge[i].next) { int v=edge[i].to; if(!vis[v]) { d[v]=d[u]+edge[i].val; if(d[v]>big) { big=d[v]; point=v; } vis[v]=true; q.push(v); } } } return point; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); #endif int t,a,b,c,n,m,len=0; init(); while(scanf("%d%d%d",&a,&b,&c)!=EOF) { addedge(a,b,c); } int u=bfs(1); int v=bfs(u); len=d[v]; cout<<len<<endl; return 0; }
相关文章推荐
- POJ 2631 Roads in the North 【树的直径】
- poj2631 Roads in the North(求树的直径)
- POJ 2631 Roads in the North (树的直径)
- poj 2631 Roads in the North【树的直径裸题】
- poj 2631 Roads in the North(裸树的直径)
- poj2631 Roads in the North(求树的直径裸题)
- 【POJ】-2631-Roads in the North(树的直径)
- 【树的直径】 POJ 2631 Roads in the North
- POJ 2631 -- Roads in the North【树的直径 && 裸题】
- poj--2631--Roads in the North(树的直径 裸模板)
- POJ 2631 Roads in the North(树的直径)
- 【树的直径】POJ 2631 Roads in the North
- POJ 2631 Roads in the North(村庄最大距离,树的直径,经典题目)
- POJ-2631-Roads in the North-树的直径(最长路)
- 【POJ】2631 - Roads in the North(树的直径)
- 树的直径学习小记 Poj 1985 Cow Marathon+Poj 2631 Roads in the North
- poj 2631 Roads in the North (树的直径模板题)
- poj 2631 Roads in the North 【树的直径裸题】
- POJ 2631 Roads in the North(树的直径)
- POJ 2631 Roads in the North 树的直径