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