【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 获得最大的利润,将元素依次入栈,期中只要碰到比队顶元素大的,就吧队顶元素卖出去,答案加上他们期中 ...
随机推荐
- 【Codeforces Round #411 (Div. 1)】Codeforces 804C Ice cream coloring (DFS)
传送门 分析 这道题做了好长时间,题意就很难理解. 我们注意到这句话Vertices which have the i-th (1 ≤ i ≤ m) type of ice cream form a ...
- poj1837【背包】
题意: 有一根杆子,给出一些杆子上的位置,位置上能放重物,再给出一些重物的重量. 重物都需要被使用,但是位置不一定都要用到. 问你能有多少种方法让这个杆子平衡. 思路: 在位置上是0/1背包思想,取或 ...
- ErrorObject OpenAsync(Action<ErrorObject>arg_fnRet)
ErrorObject OpenAsync(Action<ErrorObject>arg_fnRet) public static ErrorObject Open(this ReadWr ...
- poj 3710 Christmas Game【博弈论+SG】
也就是转换到树形删边游戏,详见 https://wenku.baidu.com/view/25540742a8956bec0975e3a8.html #include<iostream> ...
- P5167 xtq的神笔
传送门 题解 倍增也好二分也好,果然复杂度只要和\(\log\)插上关系就没我啥事了-- 首先由一个显而易见然而我完全没有发现的结论,设\(calc(l,r)\)表示区间\([l,r]\)的\(or\ ...
- IT兄弟连 JavaWeb教程 请求转发案例
CheckServlet类与OutputServlet类之间为请求转发关系.在web.xml文件中,为CheckServlet映射的URL为"/check",为OutputServ ...
- Beta版本冲刺第一天!
该作业所属课程:https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass2 作业地址:https://edu.cnblogs.com/c ...
- Educational Codeforces Round 46 (Rated for Div. 2) E. We Need More Bosses
Bryce1010模板 http://codeforces.com/contest/1000/problem/E 题意: 给一个无向图,求图的最长直径. 思路:对无向图缩点以后,求图的最长直径 #in ...
- VS Code 自用插件备份
自用插件备份 Auto Close Tag 自动闭合标签 Atuo Rename Tag 更改前面标签的时候, 自动更改后面的闭合标签 Guides 对齐线 open-in-browser 在浏览器中 ...
- 486 Predict the Winner 预测赢家
给定一个表示分数的非负整数数组. 玩家1从数组任意一端拿取一个分数,随后玩家2继续从剩余数组任意一端拿取分数,然后玩家1拿,…….每次一个玩家只能拿取一个分数,分数被拿取之后不再可取.直到没有剩余分数 ...