{"id":104,"date":"2010-02-13T01:47:00","date_gmt":"2010-02-12T17:47:00","guid":{"rendered":"http:\/\/localhost\/?p=104"},"modified":"2010-02-13T01:47:00","modified_gmt":"2010-02-12T17:47:00","slug":"sgu_149","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/wjmzbmr\/?p=104","title":{"rendered":"SGU 149"},"content":{"rendered":"<p> \u6c42\u51fa\u4e00\u68f5\u6811\u4e2d\u79bb\u6bcf\u4e2a\u70b9\u6700\u8fdc\u7684\u70b9\u3002\u3002 \u5178\u578b\u7684\u6811\u5f62DP\u3002\u3002\u8001\u9898\u4e86\u3002\u3002\u770b\u7a0b\u5e8f\u5427\u3002\u3002 Java\u592a\u96be\u5199\u8fd9\u79cd\u9898\u76ee\u4e86\u3002\u3002\u53ea\u597d\u7528c++\u4e86\u3002\u3002<br \/>#include&lt;cstdio&gt;<br \/>#include&lt;vector&gt;<br \/>#include&lt;utility&gt;<br \/>#include&lt;algorithm&gt;<br \/>using namespace std;<br \/>typedef pair&lt;int,int&gt; pi;<br \/>struct edge<br \/>{<br \/>        int t,c;<br \/>        edge(int _t,int _c):t(_t),c(_c){}<br \/>};<br \/>const int maxn=10000;<br \/>pi ch[maxn];<br \/>int f[maxn];<br \/>vector&lt;edge&gt; E[maxn];<br \/>typedef vector&lt;edge&gt;::iterator it;<br \/>void add_edge(int s,int t,int c)<br \/>{<br \/>        E[s].push_back(edge(t,c));<br \/>        E[t].push_back(edge(s,c));<br \/>}<br \/>void Renew(pi&amp;o,int x)<br \/>{<br \/>        if(o.second&lt;x)<br \/>        {<br \/>                o.second=x;<br \/>                if(o.first&lt;o.second)<br \/>                swap(o.first,o.second);<br \/>        }<br \/>}<br \/>void Renew(int&amp;x,int c)<br \/>{<br \/>        x=max(x,c);<br \/>}<br \/>void dfsch(int x,int fa)<br \/>{<br \/>        ch[x]=pi(0,0);<br \/>        for(it i=E[x].begin();i!=E[x].end();i++)<br \/>        if(i-&gt;t!=fa)<br \/>        {<br \/>                dfsch(i-&gt;t,x);<br \/>                Renew(ch[x],ch[i-&gt;t].first+i-&gt;c);<br \/>        }<br \/>}<br \/>void dfsf(int x,int fa)<br \/>{<br \/>        for(it i=E[x].begin();i!=E[x].end();i++)<br \/>        if(i-&gt;t!=fa)<br \/>        {<br \/>                Renew(f[i-&gt;t],f[x]+i-&gt;c);<br \/>                if(ch[x].first==ch[i-&gt;t].first+i-&gt;c)<br \/>                        Renew(f[i-&gt;t],ch[x].second+i-&gt;c);<br \/>                else<br \/>                        Renew(f[i-&gt;t],ch[x].first+i-&gt;c);<br \/>                dfsf(i-&gt;t,x);<br \/>        }<br \/>}<br \/>int n;<br \/>void init()<br \/>{<br \/>        scanf(&quot;%d&quot;,&amp;n);<br \/> for(int s=1,t,c;s&lt;n;s++)<br \/> {<br \/>                scanf(&quot;%d %d&quot;,&amp;t,&amp;c);<br \/>                add_edge(s,t-1,c);<br \/>        }<br \/>}<br \/>int main()<br \/>{<br \/>        \/\/freopen(&quot;in&quot;,&quot;r&quot;,stdin);<br \/>        \/\/freopen(&quot;out&quot;,&quot;w&quot;,stdout);<br \/>        init();f[0]=0;<br \/>        dfsch(0,-1);<br \/>        dfsf(0,-1);<br \/>        for(int i=0;i&lt;n;i++)<br \/> <a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\">printf<\/a>(&quot;%dn&quot;,max(ch[i].first,f[i]));<br \/>} <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6c42\u51fa\u4e00\u68f5\u6811\u4e2d\u79bb\u6bcf\u4e2a\u70b9\u6700\u8fdc\u7684\u70b9\u3002\u3002 \u5178\u578b\u7684\u6811\u5f62DP\u3002\u3002\u8001\u9898\u4e86\u3002\u3002\u770b\u7a0b\u5e8f\u5427\u3002\u3002 Java\u592a\u96be\u5199\u8fd9\u79cd\u9898\u76ee\u4e86\u3002\u3002\u53ea\u597d\u7528c++\u4e86\u3002\u3002#include&lt;cstdio&gt;#include&lt;vector&gt;#include&lt;utility&gt;#include&lt;algorithm&gt;using namespace std;typedef pair&lt;int,int&gt; pi;struct edge{ int t,c; edge(int _t,int _c):t(_t),c(_c){}};const int maxn=10000;pi ch[maxn];int f[maxn];vector&lt;edge&gt; E[maxn];typedef vector&lt;edge&gt;::iterator it;void add_edge(int s,int t,int c){ E[s].push_back(edge(t,c)); E[t].push_back(edge(s,c));}void Renew(pi&amp;o,int x){ if(o.second&lt;x) { o.second=x; if(o.first&lt;o.second) swap(o.first,o.second); }}void Renew(int&amp;x,int c){ x=max(x,c);}void dfsch(int x,int fa){ ch[x]=pi(0,0); for(it i=E[x].begin();i!=E[x].end();i++) if(i-&gt;t!=fa) { dfsch(i-&gt;t,x); Renew(ch[x],ch[i-&gt;t].first+i-&gt;c); }}void dfsf(int x,int fa){ for(it i=E[x].begin();i!=E[x].end();i++) if(i-&gt;t!=fa) { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[10],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/wjmzbmr\/index.php?rest_route=\/wp\/v2\/posts\/104"}],"collection":[{"href":"https:\/\/www.shuizilong.com\/wjmzbmr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shuizilong.com\/wjmzbmr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/wjmzbmr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/wjmzbmr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=104"}],"version-history":[{"count":0,"href":"https:\/\/www.shuizilong.com\/wjmzbmr\/index.php?rest_route=\/wp\/v2\/posts\/104\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/wjmzbmr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/wjmzbmr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/wjmzbmr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}