【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 获得最大的利润,将元素依次入栈,期中只要碰到比队顶元素大的,就吧队顶元素卖出去,答案加上他们期中 ...
随机推荐
- 利用ffmpeg0.6.1把.h264纯码流打包成.mp4 .avi等格式 (转载)
转自:http://cache2.weidaohang.org/h/index.php?q=aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemh1cWluZ183MzkvYXJ0aWNsZS ...
- shell初级-----数据呈现方式
输入与输出 Linux系统将每个对象当作文件处理,这包括输入和输出进程.Linux用文件描述符来标识每个文件对象.文件描述符是一个非负整数,可以唯一标识会话中打开的文件.每个进程一次多可以有九个文件描 ...
- codforces 1C Ancient Berland Circus(几何)
题意 给出正多边形上三个点的坐标,求正多边形的最小面积 分析 先用三边长求出外接圆半径(海伦公式),再求出三边长对应的角度,再求出三个角度的gcd,最后答案即为\(S*2π/gcd\),S为gcd对应 ...
- 使用PDO操作数据库的好处
PDO一是PHP数据对象(PHP Data Object)的缩写. 并不能使用PDO扩展本身执行任何数据库操作,必须使用一个database-specific PDO driver(针对特定数据库的P ...
- 左耳朵耗子:我对 GitLab 误删除数据库事件的几点思考
参考链接:https://www.infoq.cn/article/some-thoughts-on-gitlab-accidentally-deleting-database 太平洋时间 2017 ...
- the little schemer 笔记(7)
第七章 Friends and Relations 这是一个set集合吗 (apple peaches apple plum) 不是,apple出现了不止一次 (set? lat) 是真还是假,其中l ...
- Tcp实现省略编码
import socket class My_socket(socket.socket): def __init__(self, encoding='utf-8'): self.encoding = ...
- [SDOI2016]模式字符串
Description 给出n个结点的树结构T,其中每一个结点上有一个字符,这里我们所说的字符只考虑大写字母A到Z,再给出长度为m的模式串s,其中每一位仍然是A到z的大写字母.Alice希望知道,有多 ...
- Lucas+中国剩余定理 HDOJ 5446 Unknown Treasure
题目传送门 题意:很裸,就是求C (n, m) % (p1 * p2 * p3 * .... * pk) 分析:首先n,m<= 1e18, 要用到Lucas定理求大组合数取模,当然p[]的乘积& ...
- 递推DP URAL 1244 Gentlemen
题目传送门 /* 题意:给出少了若干卡片后的总和,和原来所有卡片,问少了哪几张 DP:转化为少了的总和是否能有若干张卡片相加得到,dp[j+a[i]] += dp[j]; 记录一次路径,当第一次更新的 ...