Codeforces 833D Red-black Cobweb【树分治】
D. Red-black Cobweb
Slastyona likes to watch life of nearby grove's dwellers. This time she watches a strange red-black spider sitting at the center of a huge cobweb.
The cobweb is a set of n nodes connected by threads, each of the treads is either red of black. Using these threads, the spider can move between nodes. No thread connects a node to itself, and between any two nodes there is a unique sequence of threads connecting them.
Slastyona decided to study some special qualities of the cobweb. She noticed that each of the threads has a value of clamminess x.
However, Slastyona is mostly interested in jelliness of the cobweb. Consider those of the shortest paths between each pair of nodes on which the numbers of red and black threads differ at most twice. For each such path compute the product of the clamminess of threads on the path.The jelliness of the cobweb is the product of all obtained values among all paths. Those paths that differ by direction only are counted only once.
Of course, this number can be huge, so Slastyona asks you to compute the jelliness of the given cobweb and print the answer modulo 109 + 7.
The first line contains the number of nodes n (2 ≤ n ≤ 105).
The next n - 1 lines contain four integers each, denoting the i-th thread of the cobweb: the nodes it connects ui, vi (1 ≤ ui ≤ n, 1 ≤ vi ≤ n), the clamminess of the thread xi (1 ≤ x ≤ 109 + 6), and the color of the thread ci (
). The red color is denoted by 0, and the black color is denoted by 1.
Print single integer the jelliness of the cobweb modulo 109 + 7. If there are no paths such that the numbers of red and black threads differ at most twice, print 1.
5
1 2 9 0
2 3 5 1
2 4 5 0
2 5 5 1
1265625
8
1 2 7 1
2 3 4 1
3 4 19 1
5 1 2 0
6 2 3 0
7 3 3 0
8 4 4 0
452841614
In the first example there are 4 pairs of nodes such that the numbers of threads of both colors on them differ at most twice. There pairs are (1, 3) with product of clamminess equal to 45, (1, 5) with product of clamminess equal to 45, (3, 4) with product of clamminess equal to 25 and (4, 5) with product of clamminess equal to 25. The jelliness of the cobweb is equal to 1265625.
题目链接:http://codeforces.com/contest/833/problem/D
官方题解:

下面给出AC代码:
#include <cstdio>
#include <cstring>
#include <utility>
#include <vector> const int N = ;
const int MOD = (int)1e9 + ; struct Edge { int v, x, c; };
struct Sum { int c, p; }; Sum& operator += (Sum& a, const Sum& b)
{
a.c += b.c;
a.p = (long long)a.p * b.p % MOD;
} int n, m, result, size[N], imbalance[N], w[];
bool resolved[N];
Sum sum[N << ];
std::vector<int> vertices;
std::vector<std::pair<int, int>> todos;
std::vector<Edge> tree[N]; int pow(int a, int n)
{
int result = ;
while (n) {
if (n & ) {
result = (long long)result * a % MOD;
}
a = (long long)a * a % MOD;
n >>= ;
}
return result;
} int prepare(int p, int u)
{
int size = ;
for (auto&& iterator : tree[u]) {
auto v = iterator.v;
if (v != p) {
int s = prepare(u, v);
result = (long long)result * pow(iterator.x, (long long)s * (n - s) % (MOD - )) % MOD;
size += s;
}
}
return size;
} int prepare2(int p, int u)
{
vertices.push_back(u);
size[u] = , imbalance[u] = ;
for (auto&& iterator : tree[u]) {
auto&& v = iterator.v;
if (v != p && !resolved[v]) {
prepare2(u, v);
size[u] += size[v];
imbalance[u] = std::max(imbalance[u], size[v]);
}
}
} void add(int k, const Sum& v)
{
for (; k < m << ; k += ~k & k + ) {
sum[k] += v;
}
} void dfs(int p, int u, int offset, int product)
{
todos.emplace_back(offset, product);
Sum s {, };
for (int k = offset - ; k >= ; k -= ~k & k + ) {
s += sum[k];
}
result = (long long)result * pow((long long)pow(product, s.c) * s.p % MOD, MOD - ) % MOD;
for (auto&& iterator : tree[u]) {
auto&& v = iterator.v;
if (v != p && !resolved[v]) {
dfs(u, v, offset + w[iterator.c], (long long)product * iterator.x % MOD);
}
}
} void divide(int root)
{
vertices.clear();
prepare2(-, root);
m = size[root];
for (auto&& u : vertices) {
imbalance[u] = std::max(imbalance[u], m - size[u]);
}
for (auto&& u : vertices) {
if (imbalance[u] < imbalance[root]) {
root = u;
}
}
for (int t = ; t < ; ++ t) {
w[t] = , w[t ^ ] = -;
for (int i = ; i < m << ; ++ i) {
sum[i] = {, };
}
add(m << , {, });
for (auto&& iterator : tree[root]) {
auto&& v = iterator.v;
if (!resolved[v]) {
dfs(root, v, (m << ) + w[iterator.c], iterator.x);
for (auto&& todo : todos) {
add((m << ) - todo.first, {, todo.second});
}
todos.clear();
}
}
}
resolved[root] = true;
for (auto&& iterator : tree[root]) {
auto&& v = iterator.v;
if (!resolved[v]) {
divide(v);
}
}
} int main()
{
#ifdef LOCAL_JUDGE
freopen("D.in", "r", stdin);
#endif
while (scanf("%d", &n) == ) {
for (int i = ; i < n; ++ i) {
tree[i].clear();
}
for (int i = , a, b, x, c; i < n - ; ++ i) {
scanf("%d%d%d%d", &a, &b, &x, &c);
a --;
b --;
tree[a].push_back({b, x, c});
tree[b].push_back({a, x, c});
}
result = ;
prepare(-, );
memset(resolved, , sizeof(*resolved) * n);
divide();
printf("%d\n", result);
}
}
Codeforces 833D Red-black Cobweb【树分治】的更多相关文章
- Codeforces 437D The Child and Zoo - 树分治 - 贪心 - 并查集 - 最大生成树
Of course our child likes walking in a zoo. The zoo has n areas, that are numbered from 1 to n. The ...
- Sereja and Brackets CodeForces - 380C (线段树+分治思路)
Sereja and Brackets 题目链接: CodeForces - 380C Sereja has a bracket sequence s1, s2, ..., *s**n, or, in ...
- 算法笔记--树的直径 && 树形dp && 虚树 && 树分治 && 树上差分 && 树链剖分
树的直径: 利用了树的直径的一个性质:距某个点最远的叶子节点一定是树的某一条直径的端点. 先从任意一顶点a出发,bfs找到离它最远的一个叶子顶点b,然后再从b出发bfs找到离b最远的顶点c,那么b和c ...
- dsu+树链剖分+树分治
dsu,对于无修改子树信息查询,并且操作支持undo的问题 暴力dfs,对于每个节点,对所有轻儿子dfs下去,然后再消除轻儿子的影响 dfs重儿子,然后dfs暴力恢复轻儿子们的影响,再把当前节点影响算 ...
- hdu-5977 Garden of Eden(树分治)
题目链接: Garden of Eden Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/ ...
- 【BZOJ-1468】Tree 树分治
1468: Tree Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1025 Solved: 534[Submit][Status][Discuss] ...
- HDU 4812 D Tree 树分治+逆元处理
D Tree Problem Description There is a skyscraping tree standing on the playground of Nanjing Unive ...
- BZOJ 2152: 聪聪可可 树分治
2152: 聪聪可可 Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一 ...
- POJ 1741 Tree 树分治
Tree Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...
- UVALive 7148 LRIP【树分治+线段树】
题意就是要求一棵树上的最长不下降序列,同时不下降序列的最小值与最大值不超过D. 做法是树分治+线段树,假设树根是x,y是其当前需要处理的子树,对于子树y,需要处理出两个数组MN,MX,MN[i]表示以 ...
随机推荐
- C 函数参数 char **s与char *s[]
本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/126 先来看一个小例子 : 编写函数遍历一个整型数组的元素,数组 ...
- 浅谈传统语音通信和APP语音通信音频软件开发之不同点
本人在传统的语音通信公司做过手机和IP电话上的语音软件开发,也在移动互联网公司做过APP上的语音软件开发.现在带实时语音通信功能的APP有好多,主流的有微信语音.QQ电话.钉钉等,当然也包括我开发过的 ...
- 平方根的C语言实现(二) —— 手算平方根的原理
版权申明:本文为博主窗户(Colin Cai)原创,欢迎转帖.如要转贴,必须注明原文网址 http://www.cnblogs.com/Colin-Cai/p/7220506.html 作者:窗户 Q ...
- jsDOM编程-拖拽层
页面样式代码: <!doctype html><html><head><meta http-equiv="content-type" co ...
- userdel 命令详解
userdel 作用: 删除指定用户,以及用户相关的文件. 如不加选项,则仅删除用户账号,而不删除相关文件 选项: -f:强制删除用户,即时用户当前已登录 -r:删除用户的同时删除与用户相关的所有文 ...
- 随手记一下,VS2015卡顿问题解决。
不知道什么开始,vs2015卡顿的很,启动时加载项目很慢,调试是启动慢,停止调试时直接卡死半分钟.其他都还能忍受,最不能忍受的是点击停止调试按钮后十几秒没反应! 网上有解决方案如下几个,我试了,都不行 ...
- C# new关键字和对象类型转换(双括号、is操作符、as操作符)
一.new关键字 CLR要求所有的对象都通过new来创建,代码如下: Object obj=new Object(); 以下是new操作符做的事情 1.计算类型及其所有基类型(一直到System.Ob ...
- 通过js中的useragrent来判断设备是pc端还是移动端,跳转不同的地址
if(/AppleWebKit.*Mobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alc ...
- 搭建PHP本地服务器(XAMPP)
1.下载XAMPP集成包 https://www.apachefriends.org/download.html2.启动前修改配置文件httpd.conf的端口号,例如:Listen 80803.启动 ...
- 有关opacity或RGBA设置颜色值及元素的透明值
opacity声明来设置元素的透明值,当opacity设置元素的透明值,内部的文字及元素也会透明,通过RGBA设置的颜色值只针对当前元素,内部的文字及元素的透明值并未发生变化 opacity声明来 ...