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]表示以 ...
随机推荐
- 【ANT】运行JMeter用例的build.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Soft ...
- Delphi中的RectTracker - 原创
本文算是副产品,正品是利用FFmpeg从任意视频中生成GIF片段的小程序,写完了就发. 因为要对视频画面进行框选,再生成GIF,所以得有个框选的控件,可Delphi里没有啊,只好自己写一个了. 声明 ...
- Nginx的 HTTP 499 状态码处理
1.前言 今天在处理一个客户问题,遇到Nginx access log中出现大量的499状态码.实际场景是:客户的域名通过cname解析到我们的Nginx反向代理集群上来,客户的Web服务是由一个负载 ...
- UVALive 4850 Installations
题目大意:有若干个任务,每个任务耗时si,期限为di,同一时间只能做一个任务.对于一个任务,惩罚值为max(0,完成时间-期限).问怎么安排,使(最大惩罚值+次大惩罚值)最小,O(n^2). 如果没有 ...
- flask 分页
在我们学习的过程中会遇到这么样的问题,就是在我们学习的过程中会发现需要分页处理,这里呢,给大家介绍书上说的分页. @app.route('/',methods=['GET']) @app.route( ...
- appium 解锁九宫格
很多人在自动化的过程中,对解锁9宫格有很多麻烦,特别是app上的有些整个放在整个view中,这就给我们测试解锁九宫格带来问题了,笔者尝试了去解决,但是都没有找到一个很好的方案,那么我就试着先去通过安卓 ...
- C++ qsort
使用qsort 需要包含头文件#include<algorithm> 例子: class Wooden{ public: int weight; int length; bool flag ...
- Robot Framework学习笔记(八)------ride标签使用
一.edit标签使用 1.导入库 点击 Edit 标签页右侧的"Library"按钮,来添加库.在添加库之前,首先库已经在 Python 下进行了安装.如,添加"Sele ...
- XCopy命令实现增量备份
xcopy XCOPY是COPY的扩展,可以把指定的目录连文件和目录结构一并拷贝,但不能拷贝系统文件:使用时源盘符.源目标路径名.源文件名至少指定一个:选用/S时对源目录下及其子目录下的所有文件进行C ...
- Python 实现网页截屏、查库、发邮件
本文介绍了使用 Python(2.7版本)实现网页截屏.查库.发邮件的 demo.用到了 selenium.phantomjs.mailer.jinja2.mysqldb 还有 image,都是比较典 ...