2016 Weak School Alliance 11 session 10.2 --- round the World (Deep Search + combination, reverse yuan), 201610.2 --- round

Source: Internet
Author: User

2016 Weak School Alliance 11 session 10.2 --- round the World (Deep Search + combination, reverse yuan), 201610.2 --- round

Question Link

Https://acm.bnu.edu.cn/v3/problem_show.php? Pid = 1, 52305

 

Problem description

In ICPCCamp, there are n cities and (n−1) (bidirectional) roads between cities. the I-th road is between the ai-th and bi-th cities. it is guaranteed that cities are connected. recently, there are 2 × ci −1 new roads built between the ai-th and bi-th cities. bobo soon comes up with an idea to travel around the world! He plans to start in city 1 and returns to city 1 after traveling along every road exactly once. it is clear that Bobo has implements plans to choose from. he wowould like to find out the number of different plans, modulo (109 + 7 ). note that two plans A and B are considered different only if there exists an I where the I-th traveled road in plan A is different from the I-th road in plan B.

Input

The first line contains an integer n (2 ≤ n ≤105 ). the I-th of the following (n −1) lines contains 3 integers ai, bi, ci (1 ≤ ai, bi ≤ n, ci ≥ 1, c1 + c2 + · + cn − 1 ≤ 106 ).

Output

An integer denotes the number of plans modulo (109 + 7 ).

Examples

Standard input

3

1 2 1

2 3 1

3

1 2 1

1 3 2

Standard output

4

144

 

Input n indicates the tree composed of n nodes, n-1-1 edges. Input ai bi c in each line of n-1 indicates that there are 2 * c edges connecting the ai and bi points, now, how many solutions do I need to finish all the edges from and each edge takes only one time?

Train of Thought: search deeply and arrange and combine. Two examples are given:

In the first image: Define sum = 1, and search down from. when the child reaches, sum * = 2! Indicates the number of methods that walk through the edge from to, and then searches down to, sum * = 2! Then return to, sum * = 4! Then return to, sum * = 2! And then return the second point. sum * = 4! /2! Why? Because there is a sequence between the son paths when the child nodes of the immediate family (son) are completed from, so the factorial of the number of paths is multiplied, however, to remove the duplicates, for example, the order of the two paths from to is in the previous 4! The process is the same ...... note that when the following child nodes are completed and the upper-layer nodes are computed, they do not need to be considered, but there is still an impact between the adjacent two-layer nodes. This figure cannot be seen, I will use the following figure for explanation.

In the second image: according to the previous algorithm, sum = 4! * 2! = 48 in fact, the correct solution is 96, less than 2. Why? Because the path at the above layer has an impact on the path at the following layer, there are two paths at the previous layer, and there is only one path at the following layer, there are two situations for analysis: 1. 1-> 2-> 3-> 2-> 1-> 2-> 1 2, 1-> 2-> 1-> 2-> 3-> 2-> 1. How does one generate it? In fact, it is the path at which the layer is taken when the path is on the first layer. The baffle method is used to set t Paths on the last layer, and s paths on this layer, so we have to finish the following s path t times, that is, C (t + s-1, t-1 ).

 

The Code is as follows:

#include <iostream>#include <stdio.h>#include <algorithm>#include <string.h>typedef long long LL;const LL maxn=1e5+10;const LL mod=1e9+7;using namespace std;struct Tree{    LL to,next,c;} edge[maxn*2];LL Inv[30*maxn];LL tot,head[maxn];LL jc[maxn*30];LL sum;void Init(){    Inv[0]=1;    Inv[1] = 1;    for(LL i=2; i<25*maxn; i++)        Inv[i] = (mod-mod/i)*Inv[mod%i]%mod;    for(LL i=2; i<25*maxn; i++)        Inv[i] = (Inv[i-1]*Inv[i])%mod;}void init(){    tot=0;    memset(head,-1,sizeof(head));}void add_edge(LL u,LL v,LL c){    edge[tot].to=v;    edge[tot].c=c;    edge[tot].next=head[u];    head[u]=tot++;}void init2(){    jc[0]=1;    for(LL i=1; i<maxn*30; i++)        jc[i]=((jc[i-1]*i)%mod);}LL road[maxn];void  dfs(LL u,LL pre){    road[u]=0;    for(LL i=head[u]; i!=-1; i=edge[i].next)    {        LL v=edge[i].to;        if(v==pre) continue;        dfs(v,u);        road[u]+=edge[i].c;        sum=(((sum*jc[edge[i].c*2])%mod)*Inv[edge[i].c])%mod;        sum=((sum*  jc[(road[v]+edge[i].c-1)]%mod)*Inv[edge[i].c-1] )%mod;        sum=(sum*Inv[road[v]])%mod;    }    sum=(sum*jc[road[u]])%mod;}int main(){    Init();    init2();    LL n,u,v,c;    while(scanf("%lld",&n)!=-1)    {        sum=1;        init();        for(LL i=1; i<n; i++)        {            scanf("%lld%lld%lld",&u,&v,&c);            add_edge(u,v,c);            add_edge(v,u,c);        }        dfs(1,0);        printf("%lld\n",sum);    }    return 0;}

 

Related Article

Contact Us

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.