Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #define N 66666
- long long n,ef[N],es[N],ev[N],first[N],next[N],c,m,dest[N];
- bool b[N];
- void add(int x,int y,int z)
- {
- next[++c]=first[x];
- first[x]=c;
- ef[c]=x;es[c]=y;ev[c]=z;
- }
- void init(void)
- {
- freopen("input.txt","r",stdin);
- freopen("output.txt","w",stdout);
- scanf("%I64d",&n);
- long long v1,v2,vv;
- c=0;
- for (int i=1;i<n;i++)
- {
- scanf("%I64d %I64d %I64d",&v1,&v2,&vv);
- add(v1,v2,vv);
- add(v2,v1,vv);
- }
- m=c;
- }
- void dfs(int v)
- {
- b[v]=true;
- long long h;
- for (h=first[v];h;h=next[h])
- {
- if (!b[es[h]])
- {
- dest[es[h]]=dest[v]+ev[h];
- dfs(es[h]);
- }
- }
- }
- void sol(void)
- {
- dest[1]=0;
- dfs(1);
- long long i,fmax=0;
- dest[0]=-1000000;
- for (i=1;i<=n;i++)
- if (dest[i]>dest[fmax])
- fmax=i;
- dest[fmax]=0;
- for (i=1;i<=n;i++)
- b[i]=false;
- dfs(fmax);
- dest[0]=-1000000;fmax=0;
- for (i=1;i<=n;i++)
- if (dest[i]>dest[fmax])
- fmax=i;
- printf("%I64d",dest[fmax]);
- }
- int main(void)
- {
- init();
- sol();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment