POJ2631 Roads in the North
生活随笔
收集整理的這篇文章主要介紹了
POJ2631 Roads in the North
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題面:http://poj.org/problem?id=2631
本題是求樹的直徑裸題。Code: #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<cstdlib> #include<algorithm> #include<ctime> using namespace std; const int MAXN=10005; struct Node{int v,Next,w; }Edge[MAXN*2]; int head[MAXN],maxDis,Cnt,maxInd; void Push(int u,int v,int w){++Cnt;Edge[Cnt].v=v; Edge[Cnt].w=w;Edge[Cnt].Next=head[u];head[u]=Cnt; } void dfs(int now,int fa,int dis){if(dis>maxDis){maxDis=dis;maxInd=now;}for(int i=head[now];i;i=Edge[i].Next){if(Edge[i].v!=fa){dfs(Edge[i].v,now,Edge[i].w+dis);}} } int main(){int u,v,w;while(~scanf("%d%d%d",&u,&v,&w)){Push(u,v,w);Push(v,u,w);}dfs(1,0,0);dfs(maxInd,0,0);printf("%d\n",maxDis);return 0; }轉載于:https://www.cnblogs.com/ukcxrtjr/p/11243509.html
總結
以上是生活随笔為你收集整理的POJ2631 Roads in the North的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 连接oracle数据库,新建用户登录界面
- 下一篇: POJ1655 Balancing Ac