{"id":310,"date":"2010-07-19T14:03:00","date_gmt":"2010-07-19T06:03:00","guid":{"rendered":"http:\/\/localhost\/?p=310"},"modified":"2010-07-19T14:03:00","modified_gmt":"2010-07-19T06:03:00","slug":"8th_in_oj_spoj_3974_3974_another_tree_problem","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/wjmzbmr\/?p=310","title":{"rendered":"Spoj 3974 3974. Another Tree Problem \u516b\u4e2dOJ"},"content":{"rendered":"\n<p>Spoj 3974 3974. Another  Tree Problem<\/p>\n<p>Time Limit:1000MS&#160; Memory Limit:65536K<br \/>Total Submit:13 Accepted:2<\/p>\n<p><strong>Description <\/strong><\/p>\n<p> As  you  are  bound  to  know  by  now,  a  tree  is  a  connected   graph <br \/>consisting  of N  vertices  and N&#8722;1 edges. Trees  also  have  the   property <br \/>of  there  being  exactly  a  single  unique path  between  any   pair  of  <br \/>vertices.  <br \/>You will be given a tree in which every edge is assigned a weight \u2013  <br \/>a non negative integer. The weight of a path is the product of the <br \/>weights of all edges on the path. The weight of the tree is the sum  of  <br \/>the  weights  of  all  paths  in  the  tree.  Paths  going  in   opposite <br \/>directions  (A  to  B  and  B  to  A)  are considered the same and,  when  <br \/>calculating the weight of a tree, are counted only once.  <br \/>Write a program that, given a tree, calculates its weight modulo  1000000007.  <\/p>\n<p><strong>Input <\/strong><\/p>\n<p> The first line contains the integer N (2 \u2264 N \u2264 100 000), the number of  vertices <br \/>in the tree. The vertices are numbered 1 to N. Each  of  the   following N&#8722;1 <br \/>contains  three  integers A,  B  and W  (1 \u2264 A,  B \u2264 N,  0 \u2264 W \u2264   1000)  <br \/>describing one edge. The edge connects vertices A and B, and its  weight is W. <\/p>\n<p><strong>Output <\/strong><\/p>\n<p> Output the weight of the tree, modulo 1000000007.  <\/p>\n<p><strong>Sample Input <\/strong><\/p>\n<p>3 <br \/>3 2 100 <br \/>2 1 100<\/p>\n<p><strong>Sample Output <\/strong><\/p>\n<p>10200<\/p>\n<p><strong>Hint <\/strong><\/p>\n<p> The weight of the path from 1 to 2 is 100  <br \/>The weight of the path from 2 to 3 is 100  <br \/>The weight of the path from 1 to 3 is 100 * 100 = 10000  <br \/>So the weight of the tree is 10000 + 100 + 100 = 10200 <\/p>\n<p><strong>Source<br \/>\u9760\u3002\u3002\u7531\u4e8e\u6211\u6beb\u65e0\u667a\u5546\u3002\u3002\u3002\u8fd9\u4e2a\u9898\u76ee\u5f88\u663e\u7136Dfs\u4e00\u4e0b\u5c31\u53ef\u4ee5\u89e3\u51b3\u4e86\u3002\u3002\u4f46\u6211\u4ee5\u524d\u5c45\u7136\u5199\u4e2a\u6811\u7684\u5206\u6cbb\u53bb\u505a\u3002\u3002\u6211\u771f\u662f\u8111\u7f3a\u554a\u56e7\u3002\u3002\u3002\u3002<br \/>\u662f\u5728\u592a\u9119\u89c6\u81ea\u5df1\u4e86\u3002\u3002<br \/>Code\uff1a<br \/><\/strong><\/p>\n<p>#include &lt;vector&gt;<br \/>#include &lt;algorithm&gt;<br \/>#include &lt;utility&gt;<br \/>#include &lt;iostream&gt;<br \/>#include &lt;cstdio&gt;<br \/>#include &lt;cmath&gt;<br \/>#include &lt;cstdlib&gt;<br \/>#include &lt;set&gt;<br \/>#include &lt;map&gt;<br \/>#include &lt;cstring&gt;<br \/>#include &lt;time.h&gt;<br \/>#define rep(i,n) for(int i=0;i&lt;n;i++)<br \/>#define pb push_back<br \/>#define Debug(x) cout&lt;&lt;#x&lt;&lt;&quot;=&quot;&lt;&lt;x&lt;&lt;endl;<br \/>#define For(i,l,r) for(int i=l;i&lt;=r;i++)<br \/>#define tr(e,x) for(eit e=x.begin();e!=x.end();e++)<br \/>const int inf=~0U&gt;&gt;1,maxn=100000,mod=1000000007;<br \/>using namespace std;<br \/>typedef long long ll;<br \/>int n;<br \/>struct Edge<br \/>{<br \/>    int t,c;<br \/>    Edge(int _t,int _c):t(_t),c(_c){}<br \/>};<br \/>vector&lt;Edge&gt; E[maxn];<br \/>typedef vector&lt;Edge&gt;::iterator eit;<br \/>void InsEdge(int s,int t,int c)<br \/>{<br \/>    E[s].pb(Edge(t,c));E[t].pb(Edge(s,c));<br \/>}<br \/>ll pow(int x,int e)<br \/>{<br \/>    if(!e)return 1;<br \/>    ll tmp=pow(x,e\/2);tmp*=tmp;tmp%=mod;<br \/>    if(e&amp;1)tmp*=x,tmp%=mod;<br \/>    return tmp;<br \/>}<br \/>ll Sum[maxn],P,ans=0;<br \/>int Q[maxn],F[maxn],h,t;<br \/>void BFS(int vs)<br \/>{<br \/>    h=t=0;<br \/>    for(Q[t++]=vs,F[vs]=-1;h&lt;t;h++)<br \/>    {<br \/>        int x=Q[h];<br \/>        tr(e,E[x])if(e-&gt;t!=F[x])<br \/>            F[e-&gt;t]=x,Q[t++]=e-&gt;t;<br \/>    }<br \/>    for(int i=h-1;i&gt;=0;i&#8211;)<br \/>    {<br \/>        int x=Q[i];Sum[x]=1;<br \/>        ll all,tmp,ret=0;<br \/>        tr(e,E[x])if(e-&gt;t!=F[x])<br \/>            Sum[x]+=Sum[e-&gt;t]*e-&gt;c,Sum[x]%=mod;<br \/>        all=Sum[x]+1;<br \/>        tr(e,E[x])if(e-&gt;t!=F[x])<br \/>            tmp=Sum[e-&gt;t]*e-&gt;c,tmp%=mod,ret+=(all-tmp)*tmp,ret%=mod;<br \/>        ret*=P;ret%=mod;if(ret&lt;0)ret+=mod;ans+=ret;ans%=mod;<br \/>    }<br \/>}<br \/>int main()<br \/>{<br \/>    \/\/freopen(&quot;in&quot;,&quot;r&quot;,stdin);<br \/>    cin&gt;&gt;n;int s,t,c;<br \/>    rep(i,n-1)<br \/>    {<br \/>        scanf(&quot;%d%d%d&quot;,&amp;s,&amp;t,&amp;c);&#8211;s;&#8211;t;<br \/>        InsEdge(s,t,c);<br \/>    }<br \/>    P=pow(2,mod-2);<br \/>    BFS(0);<br \/>    cout&lt;&lt;ans&lt;&lt;endl;<br \/>}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spoj 3974 3974. Another Tree Problem Time Limit:1000MS&#160; Memory Limit:65536KTotal Submit:13 Accepted:2 Description As you are bound to know by now, a tree is a connected graph consisting of N vertices and N&#8722;1 edges. Trees also have the property of there being exactly a single unique path between any pair of vertices. You will be [&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\/310"}],"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=310"}],"version-history":[{"count":0,"href":"https:\/\/www.shuizilong.com\/wjmzbmr\/index.php?rest_route=\/wp\/v2\/posts\/310\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/wjmzbmr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/wjmzbmr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/wjmzbmr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}