# Bzoj 3363: [Usaco2004 feb]cow Marathon Cow Marathon

Source: Internet
Author: User

Description

Give you a picture, two points at most one path, the longest path. \ (n \leqslant 4\times 10^4\)

Sol

Dfs? Dp?

This is a tree, the direction of anything is useless ...

Then record the maximum and sub-large values to the point to update the answer.

Code
/************************************************************** problem:3363 User:beiyu language:c++ Result : Accepted time:116 ms memory:5572 kb****************************************************************/#include <c stdio> #include <vector> #include <utility> #include <iostream>using namespace std; #define MPR make_pairtypedef pair< int,int > pr;const int N = 40005; int n,m,ans;vector<pr> G[n];int Mx1[n],mx2[n],b[n];    inline int in (int x=0,char Ch=getchar ()) {while (ch> ' 9 ' | | ch< ' 0 ') Ch=getchar (); while (ch>= ' 0 ' && ch<= ' 9 ') x= (x<<3) + (x<<1) +ch-' 0 ', Ch=getchar (); return x;    }void DFS (int u,int fa) {b[u]=1;        for (int i=0,v,d;i<g[u].size (); i++) if ((V=g[u][i].first)!=fa) {DFS (v,u), D=g[u][i].second;        if (Mx1[v]+d>mx1[u]) mx2[u]=mx1[u],mx1[u]=mx1[v]+d;     else if (Mx1[v]+d>mx2[u]) mx2[u]=mx1[v]+d; }ans=max (Ans,mx1[u]+mx2[u]);}    int main () {n=in (), M=in (); for (int i=1,u,v,w;i<=m;i++) {u=in (), V=in (), W=in ();    G[u].push_back (MPR (V,W)), G[v].push_back (MPR (U,W));    } for (int i=1;i<=n;i++) if (!b[i]) DFS (i,-1);    cout<<ans<<endl; return 0;}

Bzoj 3363: [Usaco2004 feb]cow Marathon Cow Marathon

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.