D. Red-black Cobweb

time limit per test:6 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

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.

Input

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.

Output

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.

Examples
Input
5
1 2 9 0
2 3 5 1
2 4 5 0
2 5 5 1
Output
1265625
Input
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
Output
452841614
Note

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【树分治】的更多相关文章

  1. 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 ...

  2. Sereja and Brackets CodeForces - 380C (线段树+分治思路)

    Sereja and Brackets 题目链接: CodeForces - 380C Sereja has a bracket sequence s1, s2, ..., *s**n, or, in ...

  3. 算法笔记--树的直径 && 树形dp && 虚树 && 树分治 && 树上差分 && 树链剖分

    树的直径: 利用了树的直径的一个性质:距某个点最远的叶子节点一定是树的某一条直径的端点. 先从任意一顶点a出发,bfs找到离它最远的一个叶子顶点b,然后再从b出发bfs找到离b最远的顶点c,那么b和c ...

  4. dsu+树链剖分+树分治

    dsu,对于无修改子树信息查询,并且操作支持undo的问题 暴力dfs,对于每个节点,对所有轻儿子dfs下去,然后再消除轻儿子的影响 dfs重儿子,然后dfs暴力恢复轻儿子们的影响,再把当前节点影响算 ...

  5. hdu-5977 Garden of Eden(树分治)

    题目链接: Garden of Eden Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/ ...

  6. 【BZOJ-1468】Tree 树分治

    1468: Tree Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1025  Solved: 534[Submit][Status][Discuss] ...

  7. HDU 4812 D Tree 树分治+逆元处理

    D Tree Problem Description   There is a skyscraping tree standing on the playground of Nanjing Unive ...

  8. BZOJ 2152: 聪聪可可 树分治

    2152: 聪聪可可 Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一 ...

  9. POJ 1741 Tree 树分治

    Tree     Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...

  10. UVALive 7148 LRIP【树分治+线段树】

    题意就是要求一棵树上的最长不下降序列,同时不下降序列的最小值与最大值不超过D. 做法是树分治+线段树,假设树根是x,y是其当前需要处理的子树,对于子树y,需要处理出两个数组MN,MX,MN[i]表示以 ...

随机推荐

  1. 【java API基本实现】ArrayList

    ArrayList: package com.tn.arraylist; public class ArrayList { Object[] objects=new Object[10]; int i ...

  2. lesson - 10 shell 基础知识

    课程大纲: 1. shell特性 命令历史 history !!  !$  !n  !字符 Tab 键可以补全文件路径或者命令 alias  a=“b”  unalias a 通配符 *匹配零个或多个 ...

  3. /sbin/nologin 和 /bin/false 的区别

    /bin/false是最严格的禁止login选项,一切服务都不能用,而/sbin/nologin只是不允许系统login,可以使用其他ftp等服务 如果想要用false在禁止login的同时允许ftp ...

  4. 10 Easy Steps to a Complete Understanding of SQL

    原文出处:http://tech.pro/tutorial/1555/10-easy-steps-to-a-complete-understanding-of-sql(已经失效,现在收集如下) Too ...

  5. 通过 备份文件 恢复/迁移 gitlab

    =============================================== 2017/10/20_第1次修改                       ccb_warlock = ...

  6. Nginx学习之配置RTMP模块搭建推流服务

    写在开始 小程序升级实时音视频录制及播放能力,开放 Wi-Fi.NFC(HCE) 等硬件连接功能.同时提供按需加载.自定义组件和更多访问层级等新特性,增强了第三方平台的能力,以满足日趋丰富的业务需求. ...

  7. 怎么为WebStorm更换主题 修改字体样式

    这篇文章主要用于帮助大家解决怎么为webstorm换theme. 首先,到选择一个自己喜欢的皮肤,Webstorm皮肤网址: http://phpstorm-themes.com/ 然后,选中你喜欢的 ...

  8. C#语言和SQL Server 数据库处理

    ---恢复内容开始--- 第七章 用表组织数据 1:数据性分类: 1>实体完整性的约束:检验每行数据是否符合要求 检验每列数据是否符合要求 2>域完整性约束:给定列输入的有效性 3> ...

  9. jq选择器汇总

    $("div") //标签 $(".box") //类 $("#box") //ID $("a[href][name]" ...

  10. 快速开发基于 HTML5 网络拓扑图应用--入门篇(一)

    计算机网络的拓扑结构是引用拓扑学中研究与大小,形状无关的点.线关系的方法.把网络中的计算机和通信设备抽象为一个点,把传输介质抽象为一条线,由点和线组成的几何图形就是计算机网络的拓扑结构.网络的拓扑结构 ...