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. 环链表相关的题目和算法[LeetCode]

    这篇文章讨论一下与链表的环相关的题目,我目前遇到的一共有3种题目. 1.判断一个链表是否有环(LeetCode相关题目:https://leetcode.com/problems/linked-lis ...

  2. Struts2学习---基本配置,action,动态方法调用,action接收参数

    首先我们先来直接配置,然后再来讲原理: 第一步:jar包的引入: 我们可以到struts2的官网上下载: http://struts.apache.org/download.cgi#struts251 ...

  3. iOS 数据加密方案

    iOS安全攻防(二十三):Objective-C代码混淆 提交用户的隐私数据 一定要使用POST请求提交用户的隐私数据GET请求的所有参数都直接暴露在URL中请求的URL一般会记录在服务器的访问日志中 ...

  4. java I/O---复制文本文件

    利用FileInputStream 和FileOutputStream 复制文本 1 public class CopyTextByBuffer { 2 3 /** 4 * @param args 5 ...

  5. bzoj 1801: [Ahoi2009]chess 中国象棋

    Description 在N行M列的棋盘上,放若干个炮可以是0个,使得没有任何一个炮可以攻击另一个炮. 请问有多少种放置方法,中国像棋中炮的行走方式大家应该很清楚吧. Input 一行包含两个整数N, ...

  6. JavaScript函数(二)

    在前面我们已经对函数作了简单的介绍,比如函数的定义.函数的声明.函数的调用和函数的传参等.本节将进一步介绍函数的应用,深度理解函数的各种使用. 函数是一个对象,每个函数时Function类型的一个实例 ...

  7. 删除SVN版本信息的两种方式

    一.在linux下删除SVN版本信息 删除这些目录是很简单的,命令如下 find . -type d -name ".svn"|xargs rm -rf 或者 find . -ty ...

  8. 使用 mysql PDO 防止sql注入

    技巧: 1. php升级到5.3.6+,生产环境强烈建议升级到php 5.3.9+ php 5.4+,php 5.3.8存在致命的hash碰撞漏洞. 2. 若使用php 5.3.6+, 请在在PDO的 ...

  9. SQL 多列合并一列

    select rtrim(姓)+ rtrim(名) as 姓名 from tb

  10. 一个超级简单的demo带你走进redux的大坑

    先上代码 import React, { Component } from 'react'; import ReactDOM from 'react-dom'; import { createStor ...