题目链接

https://acm.bnu.edu.cn/v3/problem_show.php?pid=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 many plans to choose from. He would 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

题意:输入n 表示由n个节点构成的树,然后输入n-1条边,n-1行每行输入ai bi c 表示ai点和bi点之间有2*c条边相连,现在求从1号点开始走完所有边且每条边只走一次的方案数?

思路:深搜,然后排列组合,举两个例子:

        

第一个图片中:定义sum=1,从1号点开始向下深搜,到达孩子2号点的时候sum*=2!  表示从1到2号点走遍边的方法数,然后继续向下深搜到4号点,sum*=2!  然后返回再到5号点,sum*=4! 再返回走到6号点,sum*=2! ,然后返回2号点,这时sum*=4!/2! 为什么呢? 因为从2号点开始走完它的直系(儿子)孩子节点时,它的儿子路径之间存在先后顺序,所以乘上路径数的阶乘,但要除去重复的部分比如从2号点到5号点时这两条路径的先后顺序在之前的4!中已经考虑了,接下来就是相同的过程了......注意的是下面的孩子节点算完了,再计算上层节点时就不需要考虑了,但是相邻两层的节点之间还是有影响的,这个图看不出来,我用下面一个图作解释。

第二个图片中:按照之前的算法为sum=4!*2!=48  其实正确的解是96  ,少乘了一个2,为什么呢? 因为上面一层的路径对下面一层的路径产生了影响,上一层有两条路径,下面一层只有一条路径,那么可以分析存在两种情况:1、  1->2->3->2->1->2->1         2、  1->2->1->2->3->2->1   怎么产生的? 其实是在走上一层的路径时,在哪一次走的这一层的路径,用挡板法,设上一层有t条路径,这一层有s条路径,那么得在t次把下面的s条路径走完,也就是C(t+s-1,t-1) 。

代码如下:

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

2016弱校联盟十一专场10.2---Around the World(深搜+组合数、逆元)的更多相关文章

  1. 2016弱校联盟十一专场10.3---Similarity of Subtrees(深搜+hash、映射)

    题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52310 problem description Define the depth of a ...

  2. 2016弱校联盟十一专场10.5---As Easy As Possible(倍增)

    题目链接 https://acm.bnu.edu.cn/v3/contest_show.php?cid=8506#problem/A problem description As we know, t ...

  3. (2016弱校联盟十一专场10.3) D Parentheses

    题目链接 把左括号看成A右括号看成B,推一下就行了.好久之前写的,推到最后发现是一个有规律的序列. #include <bits/stdc++.h> using namespace std ...

  4. (2016弱校联盟十一专场10.3) B.Help the Princess!

    题目链接 宽搜一下就行. #include <iostream> #include<cstdio> #include<cstring> #include<qu ...

  5. (2016弱校联盟十一专场10.3) A.Best Matched Pair

    题目链接 #include<cstdio> #include<cstring> #include<algorithm> #include<stack> ...

  6. 2016弱校联盟十一专场10.3---We don't wanna work!(STL--set的使用)

    题目链接 https://acm.bnu.edu.cn/v3/contest_show.php?cid=8504#problem/C 代码如下: #include <iostream> # ...

  7. (2016弱校联盟十一专场10.2) A.Nearest Neighbor Search

    题目链接 水题,算一下就行. #include <bits/stdc++.h> using namespace std; typedef long long ll; ll x[],y[], ...

  8. (2016弱校联盟十一专场10.2) E.Coins

    题目链接 很久之前写的了,好像是对拍打表过的,推一下就行了. #include <bits/stdc++.h> using namespace std; typedef long long ...

  9. (2016弱校联盟十一专场10.5) F. Fibonacci of Fibonacci

    题目链接 题目大意就是这个,先找出下标的循环节,再快速幂对20160519取余就行了. 找出下标循环节: #include <cstdio> #include <iostream&g ...

随机推荐

  1. WebApi系列~安全校验中的防篡改和防复用

    回到目录 web api越来越火,因为它的跨平台,因为它的简单,因为它支持xml,json等流行的数据协议,我们在开发基于面向服务的API时,有个问题一直在困扰着我们,那就是数据的安全,请求的安全,一 ...

  2. windows下配置nginx+php环境

    刚看到nginx这个词,我很好奇它的读法(engine x),我的直译是"引擎x",一般引"擎代"表了性能,而"x"大多出现是表示" ...

  3. atitit 研发管理 要不要自己做引擎自己实现架构?.docx

    atitit 研发管理 要不要自己做引擎自己实现架构?.docx 1.1. 目前已经有很多引擎了,还要自己做吗??1 1.2. 答案是自己做更好,利大于弊1 2. 为什么要自己做??1 2.1. 从历 ...

  4. css_04之显示、定位

    1.显示方式:display:取值:none(隐藏,不占页面空间,脱离文档流)/block(元素变为块级)/inline(元素变为行内)/inline-block(元素变为行内块): 2.显示效果:v ...

  5. springboot hessian

    注意把hessian的依赖换成4.0.38或者把git文件里的4.0.37放到maven私服中去,推荐使用4.0.37版本.38版本存在序列化bigdecimal的问题. <dependency ...

  6. JSP开发环境配置问题解答

    有过JSP开发经验的同学对于JSP开发环境的配置一定非常的很有感触,十分的繁琐,有时因为一个小的问题导致我们配置的配置前功尽弃,本篇我将重点带领大家一起探讨一下关于JSP环境配置的一些常见问题,及解决 ...

  7. PL/SQL概念

    一. 为什么把SQL语句组合成PL/SQL语句块效率会更高? 使用PL/SQL语句块中的SQL语句更加高效,原因主要是这样做可以大幅降低网络流量,应用程序也会变得更加高效. 当客户端计算机发出一条SQ ...

  8. 邻接矩阵无向图(一)之 C语言详解

    本章介绍邻接矩阵无向图.在"图的理论基础"中已经对图进行了理论介绍,这里就不再对图的概念进行重复说明了.和以往一样,本文会先给出C语言的实现:后续再分别给出C++和Java版本的实 ...

  9. Spark入门实战系列--6.SparkSQL(下)--Spark实战应用

    [注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 .运行环境说明 1.1 硬软件环境 线程,主频2.2G,10G内存 l  虚拟软件:VMwa ...

  10. 机器学习&数据挖掘笔记_14(GMM-HMM语音识别简单理解)

    为了对GMM-HMM在语音识别上的应用有个宏观认识,花了些时间读了下HTK(用htk完成简单的孤立词识别)的部分源码,对该算法总算有了点大概认识,达到了预期我想要的.不得不说,网络上关于语音识别的通俗 ...