【2018 CCPC网络赛】1009 - 树
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6446
题目给出的数据为一棵树,dfs扫描每条边,假设去掉某条边,则左边 x 个点,右边 n-x 个点,则经过该条边共有 x*(n-x) 种组合,又因为 1~n 全排列有 n! 种,故 a~b,包含 b~a 这条边的贡献为 (n-x)*x*2*(n-1)!*w;
#include<iostream>
#include<cstdio>
#include<vector>
using namespace std; #define LL long long
const int MOD = 1e9+;
const int N = 1e5+;
struct EDGE {
LL to, length;
};
vector<EDGE> edge[N];
LL n, factorial[N], lchild[N], w[N]; int dfs(int k, int pre) {
int size = edge[k].size(), ans=;
for(int i=; i<size; i++) {
if(edge[k][i].to == pre)
w[k] = edge[k][i].length;
else
ans += dfs(edge[k][i].to, k);
}
return lchild[k] = ans;
} int main()
{
factorial[] = ;
for(int i=; i<N; i++)
factorial[i] = factorial[i-]*i%MOD; while(~scanf("%d",&n))
{
LL ans=;
for(int i=; i<=n; i++) {
edge[i].clear();
}
for(int x,y,v,i=; i<n; i++) {
scanf("%d%d%d", &x, &y, &v);
edge[x].push_back({y,v});
edge[y].push_back({x,v});
} dfs(, -);
for(int i=; i<=n; i++) {
LL tmp=;
tmp = lchild[i]*(n-lchild[i])%MOD;
tmp = *factorial[n-]%MOD*w[i]%MOD*tmp%MOD;
ans = (ans+tmp)%MOD;
}
cout<<ans<<endl;
}
return ;
}
【2018 CCPC网络赛】1009 - 树的更多相关文章
- 2018 CCPC网络赛
2018 CCPC网络赛 Buy and Resell 题目描述:有一种物品,在\(n\)个地点的价格为\(a_i\),现在一次经过这\(n\)个地点,在每个地点可以买一个这样的物品,也可以卖出一个物 ...
- 2018 CCPC网络赛 1010 hdu 6447 ( 树状数组优化dp)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=6447 思路:很容易推得dp转移公式:dp[i][j] = max(dp[i][j-1],dp[i-1][j ...
- HDU 6444 Neko's loop ( 2018 CCPC 网络赛 && 裴蜀定理 && 线段树 )
题目链接 题意 : 给出一个 n 个元素的环.可以任意选择起点.选完起点后.可以行走 m 步.每次前进 k 个单位.所走到的点将产生正或负贡献.问你一开始得准备多少才能使得初始资金加上在环上获取最大利 ...
- 2018 CCPC 网络赛
The Power Cube is used as a stash of Exotic Power. There are n cities numbered 1,2,…,n where allowed ...
- 2018 CCPC网络赛 几道数学题
1002 Congruence equation 题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=6439 题解 : https://www.zyb ...
- 2018 CCPC网络赛 hdu6444 Neko's loop
题目描述: Neko has a loop of size n.The loop has a happy value ai on the i−th(0≤i≤n−1) grid. Neko likes ...
- 2018 CCPC 网络赛 Buy and Resell
The Power Cube is used as a stash of Exotic Power. There are n cities numbered 1,2,…,n where allowed ...
- 【2018 CCPC网络赛 1004】Find Integer(勾股数+费马大定理)
Problem Description people in USSS love math very much, and there is a famous math problem . give yo ...
- 【2018 CCPC网络赛】1001 - 优先队列&贪心
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6438 获得最大的利润,将元素依次入栈,期中只要碰到比队顶元素大的,就吧队顶元素卖出去,答案加上他们期中 ...
随机推荐
- hdoj5402 【模拟/构造】
题意: 给你一个矩阵,每个值都是非负,然后让你从左上角走到右下角,每个点只能走一次,求到终点的最大值,还要输出一条路径 思路: 一开始拿到还以为搜索之类的,但是发现神特么暴力+麻烦(因为路径这个东西. ...
- Codeforces Round #364 (Div. 2)【A,C】
啊啊啊啊啊啊啊啊啊,目睹A->CⅠA全过,最终fstwaA,C;23333333 A题: 题意: 就是分成相等的m堆,每堆有两个位置上的值相加. 思路: fst在sum可能不是偶数,先*2/n; ...
- Mybatis 未设置主键映射报错;Cause: java.sql.SQLSyntaxErrorException: Unknown column 'system_id' in 'field list'
使用MyBatis的时候,主键的字段建议绑定在Bean的属性上面, import javax.persistence.*; public class User { @Id @Column(name = ...
- 简述网站、B/S架构与C/S架构
一.什么是网站? 定义:网站是指在因特网上根据一定的规则,使用HTML等工具制作的用于展示特定内容相关网页的集合. 简单地说,网站是一种沟通工具(或者说是一种软件——建设网站也是软件开发的一种),我们 ...
- hdu1102 Constructing Roads 基础最小生成树
//克鲁斯卡尔(最小生成树) #include<cstdio> #include<iostream> #include<algorithm> using names ...
- [POI2005]Sza-Template
Description Byteasar 想在墙上涂一段很长的字符,他为了做这件事从字符的前面一段中截取了一段作为模版. 然后将模版重复喷涂到相应的位置后就得到了他想要的字符序列.一个字符可以被喷涂很 ...
- AtCoder Grand Contest 012 A
A - AtCoder Group Contest Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statem ...
- 题解报告:hdu 1520 Anniversary party(树形dp入门)
Problem Description There is going to be a party to celebrate the 80-th Anniversary of the Ural Stat ...
- Contextual Action bar(2) 简介,启动,各函数介绍
一.Context Action Bar简介 它是一个ActionBar,有各种操作项,但它不是始终显示的ActionBar,它需要上下文才显示.样式如下: 二.Context Action Bar的 ...
- RHEL 6.5 ----Postfix邮件服务器
主机名 IP 服务 master 192.168.30.130 slave 192.168.30.131 软件包介绍 包名 介绍 postfix-2.6.6-2.2.el6_1.x86 ...