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. Mybatis入门(一)之操作数据库

    Whats Mybatis 持久层框架, 替代MVC层中DAO,因为DAO 层的需求就是 :能与数据库交互的对象. 能执行SQL语句. 不同于JDBC的connection,MyBatis 中有个Sq ...

  2. python链接mysql

    1.安装MySQLdb MySQLdb 是用于Python链接Mysql数据库的接口,它实现了 Python 数据库 API 规范 V2.0,基于 MySQL C API 上建立的. 下载地址: ht ...

  3. 520. Detect Capital

      Given a word, you need to judge whether the usage of capitals in it is right or not. We define the ...

  4. 【WebGL】《WebGL编程指南》读书笔记——第3章

    一.前言 根据前面一章的内容,继续第三章的学习. 二.正文       一起绘制三个点,这里要使用到缓存了 var n = initVertexBuffers(gl); //返回绘制点的个数 n ) ...

  5. 解决NTPD漏洞,升级Ntpd版本

    关于解决漏洞的问题我就不详说了,主要就是升级版本.这里我们就直接简单记录下步骤: 1.升级 使用root用户登录系统进入到/home/guankong ,上传ntp-4.2.8p9-1.el6.x86 ...

  6. Java 包装类Integer的值比较

    对于包装类型Integer的值比较与int的值比较是不同的:   public class Java_Val_Compare { public static void main(String[] ar ...

  7. thinkphp3.2.3使用ajax 的一些坑——使用AjaxReturn()后,直接返回null,模板文件不起作用

    从接触thinkphp到今天,填完此坑,必有其他的坑有会冒出来.哎!这个填坑之路我想是没有尽头的了. 最近,需要使用ajax完成一些操作,一开始想Ajax简单啊,不过是一种提交数据的方式,不过是害苦了 ...

  8. errcode 4103 invalid page hint 小程序模板消息推送遇到的坑

    invalid page hint一直提示这个坑爹的就是,我的小程序没发布之前,也就是测试版本用这个格式是可以的 /pages/myGroup/myGroup?groupid=22***但是发布成功以 ...

  9. sql sever基本查询语句

    查询(*可代表全部)(<>代表不等于于)select 列名 from 表名(,隔开)where 查询条件order by 排序的列名+连接的数据类型必须兼容(结果为字符串数据的连接 , 如 ...

  10. 深入C#.NET数据类型

    深入C#数据类型 --不同类型的参数传递使用值传递,在方法中对参数的更改在调用后不能保留.使用ref方式传递,可以保留对参数值的更改. ---值方式参数传递和引用方式传递使用值方式(不用ref修饰)传 ...