2016弱校联盟十一专场10.2——Around the World

题目链接:Around the World

题意:

给你n个点,有n-1条边,现在这n-1条边又多增加了ci*2-1条边,问你有多少条欧拉回路

题解:

套用best定理

Best Theorem:有向图中以 i 为起点的欧拉回路个数为以 i 为根的树形图个数 ×(( 每个点 度数 −1)!)。

Matrix Tree Theorem:以 i 为根的树形图个数 = 基尔霍夫矩阵去掉第 i 行第 i 列的行列 式。

从某个点 i 出发并回到 i 的欧拉回路个数 = 以 i 为起点的欧拉回路个数 ×i 的度数。

 1 #include<cstdio>
 2 #include<cstring>
 3 #define F(i,a,b) for(int i=a;i<=b;i++)
 4 typedef long long ll;
 5
 6 const int N=1e5+7,P=1e9+7,M=2e6;
 7 long long fact[M+1];
 8 int deg[N],n;
 9
10 ll qpow(ll a,ll n){
11     ll ans=1;
12     while(n){
13         if(n&1) ans=ans*a%P;
14         a=a*a%P,n>>=1;
15     }
16     return ans;
17 }
18 ll C(int n,int m){return (fact[n]*qpow(fact[m],P-2)%P)*qpow(fact[n-m],P-2)%P;}
19
20 int main(){
21     fact[0]=1;
22     F(i,1,2000000)fact[i]=fact[i-1]*i%P;
23     scanf("%d",&n);
24     ll ans=1;
25     memset(deg,0,sizeof(deg));
26     F(i,1,n-1){
27         int a,b,c;
28         scanf("%d%d%d",&a,&b,&c);
29         deg[a]+=c,deg[b]+=c,ans=(ans*C(2*c,c)%P)*c%P;
30     }
31     F(i,1,n)ans=ans*fact[deg[i]-1]%P;
32     printf("%lld\n",ans*deg[1]%P);
33     return 0;
34 }

时间: 2024-08-08 13:56:24

2016弱校联盟十一专场10.2——Around the World的相关文章

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

题目链接 #include<cstdio> #include<cstring> #include<algorithm> #include<stack> using namespace std; int n,data[1005]; int cal(int x) { int tx=x; int pre=-1; while(tx) { if(pre!=-1&&tx%10-pre!=-1) return -1; pre = tx%10; tx/=10

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 node in a rooted tree by applying the following rules recursively: • The depth of a root node is 0. • The depths of child nodes whose parents are with

2016弱校联盟十一专场10.2---Around the World(深搜+组合数、逆元)

题目链接 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 conne

2016弱校联盟十一专场10.2部分题解

1/10 J. Matrix Transformation 1 /*zhen hao*/ 2 #include <bits/stdc++.h> 3 using namespace std; 4 5 #define lson l, m, rt*2 6 #define rson m + 1, r, rt*2+1 7 #define xx first 8 #define yy second 9 10 typedef long long LL; 11 typedef unsigned long lon

2016弱校联盟十一专场10.2——Floyd-Warshall

题目链接:Floyd-Warshall 题意: 给你n个点,m条边,100>m-n>0,现在有q个询问,问你任意两点的最短距离,题目保证每条边都被连接,每条边的距离为1 题解: 首先我们可以看到边最多只比点多100个,那么我们可以先将n-1条边生成一棵树,然后用LCA来求最短距离. 然而有可能最短路在多余的这100条边上,所以我们将这100条边的两个端点到所有点的最短路用bfs预处理出来, 然后再用来更新一下答案就行. 1 #include<cstdio> 2 #include&l

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

题目链接 宽搜一下就行. #include <iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; int n,m; char str[209][209]; int to[4][2]={1,0,-1,0,0,1,0,-1}; bool mp[209][209]; bool check(int x,int y) { if(x<0||x>=n

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

题目链接 把左括号看成A右括号看成B,推一下就行了.好久之前写的,推到最后发现是一个有规律的序列. #include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ll n; while(scanf("%lld",&n)!=EOF) { ll cnt=0,sum=0; for(int i=1;; i++) { if(sum>=n) break; cnt++; su

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

题目链接 题目大意就是这个,先找出下标的循环节,再快速幂对20160519取余就行了. 找出下标循环节: #include <cstdio> #include <iostream> using namespace std; int main() { int i=1,a=0,b=1; for(;;i++) { int t=b; b=(a+b)%20160519; a=t%20160519; if(a==0&&b==1) { printf("%d\n"

2016弱校联萌十一专场10.3 遗憾题合集

http://acm-icpc.aitea.net/index.php?2016%2FPractice%2F%E6%A8%A1%E6%93%AC%E5%9C%B0%E5%8C%BA%E4%BA%88%E9%81%B8%2F%E8%AC%9B%E8%A9%95 C.We don't wanna work! @siludose 你要的代码,做好了参考看 SB模拟,xjb模拟 #include <iostream> #include <algorithm> #include <st