题面

题解

点分治大火题。。。

设白边数量为$a$,黑边为$b$,则$2min(a,b)\geq max(a,b)$

即$2a\geq b\;\&\&2b\geq a$

考虑点分治时如何统计答案:

$2(a_1 +a_2) \geq b_1 + b_2$

$\therefore 2a_1-b_1\geq b_2-2a_2$

另外一边同理

于是我们可以愉快地用$sort+BIT$统计答案了

但是路径有可能重复计算,可以套一个$CDQ$分治什么的来搞一下

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#define RG register
#define clear(x, y) memset(x, y, sizeof(x)); namespace IO
{
const int BUFSIZE = 1 << 20;
char ibuf[BUFSIZE], *is = ibuf, *it = ibuf;
inline char getchar() { if (is == it) it = (is = ibuf) + fread(ibuf, 1, BUFSIZE, stdin); return *is++; }
} inline int read()
{
int data = 0, w = 1;
char ch = IO::getchar();
while(ch != '-' && (ch < '0' || ch > '9')) ch = IO::getchar();
if(ch == '-') w = -1, ch = IO::getchar();
while(ch >= '0' && ch <= '9') data = data * 10 + (ch ^ 48), ch = IO::getchar();
return data*w;
} const int maxn(1e5 + 10), Mod(1e9 + 7);
inline int fastpow(int x, int y)
{
int ans = 1;
while(y)
{
if(y & 1) ans = 1ll * ans * x % Mod;
x = 1ll * x * x % Mod, y >>= 1;
}
return ans;
} struct edge { int next, to, val, col; } e[maxn << 1];
int n, head[maxn], e_num, size[maxn], Size, min, root;
struct qry { int r, b, v, o; } stk[maxn], q[maxn << 2];
int top, cntq, cntup, csum[maxn << 2], cmul[maxn << 2], vis[maxn];
inline bool cmpr(const qry &a, const qry &b)
{ return a.r == b.r ? a.b < b.b : a.r < b.r; } void clean(int x)
{
while(x <= (n << 2 | 1)) csum[x] = 0, cmul[x] = 1, x += x & -x;
} void add(int x, int v)
{
while(x <= (n << 2 | 1))
++csum[x], cmul[x] = 1ll * cmul[x] * v % Mod, x += x & -x;
} int query_sum(int x)
{
int ans = 0;
while(x) ans += csum[x], x -= x & -x;
return ans;
} int query_mul(int x)
{
int ans = 1;
while(x) ans = 1ll * ans * cmul[x] % Mod, x -= x & -x;
return ans;
} inline void add_edge(int from, int to, int val, int col)
{
e[++e_num] = (edge) {head[from], to, val, col};
head[from] = e_num;
} void getRoot(int x, int fa)
{
size[x] = 1; int tot = 0;
for(RG int i = head[x]; i; i = e[i].next)
{
int to = e[i].to; if(vis[to] || to == fa) continue;
getRoot(to, x); tot = std::max(tot, size[to]);
size[x] += size[to];
}
tot = std::max(tot, Size - size[x]);
if(tot < min) min = tot, root = x;
} void getVal(int x, int fa, int r, int b, int v)
{
stk[++top] = (qry) {r, b, v, 0};
for(RG int i = head[x]; i; i = e[i].next)
{
int to = e[i].to; if(vis[to] || to == fa) continue;
getVal(to, x, r + (!e[i].col), b + e[i].col, 1ll * v * e[i].val % Mod);
}
} int ans = 1;
void CDQ(int l, int r)
{
if(l >= r) return;
int mid = (l + r) >> 1;
CDQ(l, mid); CDQ(mid + 1, r);
int j = l;
for(RG int i = mid + 1; i <= r; i++)
{
if(!q[i].o) continue;
while(q[j].r <= q[i].r && j <= mid)
{
if(!q[j].o) add(q[j].b, q[j].v);
++j;
}
ans = 1ll * ans * query_mul(q[i].b) % Mod *
fastpow(q[i].v, query_sum(q[i].b)) % Mod;
}
for(RG int i = l; i < j; i++) if(!q[i].o) clean(q[i].b);
std::inplace_merge(q + l, q + mid + 1, q + r + 1, cmpr);
} void calc(int x)
{
int PLUS = n << 1 | 1;
cntq = cntup = 0;
for(RG int i = head[x]; i; i = e[i].next)
{
int to = e[i].to; if(vis[to]) continue;
top = 0; getVal(to, x, !e[i].col, e[i].col, e[i].val);
for(RG int j = 1; j <= top; j++)
{
int a = stk[j].r, b = stk[j].b, c = 2 * a - b, d = 2 * b - a;
q[++cntq] = (qry) {c + PLUS, d + PLUS, stk[j].v, 1};
}
for(RG int j = 1; j <= top; j++)
{
int a = stk[j].r, b = stk[j].b, c = 2 * a - b, d = 2 * b - a;
q[++cntq] = (qry) {-c + PLUS, -d + PLUS, stk[j].v, 0};
if(2 * std::min(a, b) >= std::max(a, b))
ans = 1ll * ans * stk[j].v % Mod;
}
}
CDQ(1, cntq);
} void dfs(int x)
{
vis[x] = 1; calc(x);
for(RG int i = head[x]; i; i = e[i].next)
{
int to = e[i].to; if(vis[to]) continue;
Size = min = size[to]; getRoot(to, x); dfs(root);
}
} int main()
{
Size = min = n = read();
for(int i = 1, a, b, c, d; i < n; i++)
a = read(), b = read(), c = read(), d = read(),
add_edge(a, b, c, d), add_edge(b, a, c, d);
for(RG int i = 1; i <= (n << 2 | 1); i++) csum[i] = 0, cmul[i] = 1;
getRoot(1, 0); dfs(root);
printf("%d\n", ans);
return 0;
}

CF833D Red-Black Cobweb的更多相关文章

  1. 【CF833D】Red-Black Cobweb(点分治)

    [CF833D]Red-Black Cobweb(点分治) 题面 CF 有一棵树,每条边有一个颜色(黑白)和一个权值,定义一条路径是好的,当且仅当这条路径上所有边的黑白颜色个数a,b满足2min(a, ...

  2. 【CF833D】Red-Black Cobweb

    [CF833D]Red-Black Cobweb 题面 洛谷 题解 看到这种统计路径的题目当然是淀粉质啦. 考虑转化一下信息设一条路径上有红点\(a\)个,黑点\(b\)个 则\(2min(a,b)\ ...

  3. CF833D Red-Black Cobweb 点分治、树状数组

    传送门 统计所有路径的边权乘积的乘积,不难想到点分治求解. 边权颜色比例在\([\frac{1}{2},2]\)之间,等价于\(2B \geq R , 2R \geq B\)(\(R,B\)表示红色和 ...

  4. 题解 CF833D Red-Black Cobweb

    题目传送门 题目大意 给出一个 \(n\) 个点的树,每条边有边权和颜色 \(0,1\) ,定义一条链合法当且仅当 \(0,1\) 颜色的边数之比小于等于 \(2\) ,求所有合法的链的边权之积的积. ...

  5. Codeforces 833D Red-black Cobweb【树分治】

    D. Red-black Cobweb time limit per test:6 seconds memory limit per test:256 megabytes input:standard ...

  6. 使用Red Gate Sql Data Compare 数据库同步工具进行SQL Server的两个数据库的数据比较、同步

    Sql Data Compare 是比较两个数据库的数据是否相同.生成同步sql的工具. 这一款工具由Red Gate公司出品,我们熟悉的.NET Reflector就是这个公司推出的,它的SQLTo ...

  7. 使用Red Gate Sql Compare 数据库同步工具进行SQL Server的两个数据库的结构比较、同步

    将测试版的项目同步(部署)到正式版的时候,两个数据库的结构比较与同步时,如果修改数据库的时候没有记录好修改了那些表,很难将两个数据库进行同步 RedGate Sql Compare使用简介说明: 1. ...

  8. 新年抢红包效果(New Year Red Packet)

    新年抢红包效果(New Year Red Packet) 晓娜的文章(微信公众号:migufe) 2016即将过去,我们将迎来新的一年2017,这里小编提前祝大家新年快乐!万事如意!那我们新年最开心的 ...

  9. KALI Linux problems & Study Red Hat | Ubuntu

    Problem When you ask some website with https head.you may met the problem  secure connection failed ...

随机推荐

  1. 网络Socket编程(简易qq实现之C/S通信1)

    1. 目标:实现两个用户之间的通信,利用的是简单的Socket知识以及简略界面 2. 界面:分为客户端与服务器端(如下图) 3. 基本功能:客户端先向服务器端发送一个消息,这样就可以让客户端与服务器端 ...

  2. 第04章-VTK基础(7)

    [译者:这个系列教程是以Kitware公司出版的<VTK User's Guide -11th edition>一书作的中文翻译(出版时间2010年.ISBN: 978-1-930934- ...

  3. 使用Qt框架开发http服务器问题的记录

    最近需求需要开发一款 HTTP ,然后由于先前接触过Qt,就直接用Qt写HTTP服务器了,也是为了当作练手,要不然是直接上HTTP框架的. 后端用C++ Qt框架 前端为了练手 当然是纯生的 js h ...

  4. 死磕salt系列-salt入门

    saltstack简介 SaltStack是一种新型的基础设施管理软件,简单易部署,可伸缩的足以管理成千上万的服务器,和足够快的速度控制,与他们交流,以毫秒为单位.SaltStack提供了一个动态基础 ...

  5. maven 编译替换占位符

    首先开启资源配置的插件,由此插件替换占位符 <plugin> <groupId>org.apache.maven.plugins</groupId> <art ...

  6. 程序员职业规划(一篇来自"阿里Java工程师"对工作3年左右程序员的职业建议和应该掌握的职业技能)

    程序员的三个阶段(转载) 第一阶段:三年 我认为三年对于程序员来说是第一个门槛,这个阶段将会淘汰掉一批不适合写代码的人. 这一阶段,我们走出校园,迈入社会,成为一名程序员,正式从书本上的内容迈向真正的 ...

  7. Python 有用的 map() deduce() filter() 函数

    #!/usr/bin/python#5!+4!+3!+2!+1! #give 3 return 3*2*1def jiechen(n): N = map(lambda x:x+1,range(n)) ...

  8. Gradle Goodness: Excluding Tasks for Execution

    In Gradle we can create dependencies between tasks. But we can also exclude certain tasks from those ...

  9. G1 GC日志:Application time: 0.8766273 seconds

    启动日志一直循环: 1.159: Application time: 0.8766273 seconds 1.160: Total time for which application threads ...

  10. oracle 子查询的几个种类

    1.where型子查询: select cat_id,good_id,good_name from goods where good_id in (selct max(good_id) from go ...