题解 CF833D Red-Black Cobweb
题目大意
给出一个 \(n\) 个点的树,每条边有边权和颜色 \(0,1\) ,定义一条链合法当且仅当 \(0,1\) 颜色的边数之比小于等于 \(2\) ,求所有合法的链的边权之积的积。
\(n\le 10^5\),答案对 \(10^9+7\) 取模。
思路
边分治板题,但是因为边界问题爆炸了。。。
首先先容斥一下,即总答案除以不合法答案,然后你发现总答案特别好求,不合法方案可是使用边分治解决。
时间复杂度 \(\Theta(n\log^2 n)\) 。
\(\texttt{Code}\)
#include <bits/stdc++.h>
using namespace std;
#define Int register int
#define mod 1000000007
#define MAXN 800005
template <typename T> inline void read (T &t){t = 0;char c = getchar();int f = 1;while (c < '0' || c > '9'){if (c == '-') f = -f;c = getchar();}while (c >= '0' && c <= '9'){t = (t << 3) + (t << 1) + c - '0';c = getchar();} t *= f;}
template <typename T,typename ... Args> inline void read (T &t,Args&... args){read (t);read (args...);}
template <typename T> inline void write (T x){if (x < 0){x = -x;putchar ('-');}if (x > 9) write (x / 10);putchar (x % 10 + '0');}
int n,ans = 1;
int qkpow (int a,int b){
int res = 1;for (;b;b >>= 1,a = 1ll * a * a % mod) if (b & 1) res = 1ll * res * a % mod;
return res;
}
int inv (int x){return qkpow (x,mod - 2);}
namespace Graph{
#define PII pair<int,int>
int cnt = 1,toop = 1,pres[MAXN],to[MAXN << 1],wei[MAXN << 1],col[MAXN << 1],nxt[MAXN << 1],head[MAXN],siz[MAXN];bool vis[MAXN];
void Add_Edge (int u,int v,int w,int c){
to[++ toop] = v,wei[toop] = w,col[toop] = c,nxt[toop] = head[u],head[u] = toop;
to[++ toop] = u,wei[toop] = w,col[toop] = c,nxt[toop] = head[v],head[v] = toop;
}
struct node{
int R,B,dis;
};
node *f,T1[MAXN],T2[MAXN];
void dfs (int u,int fa,int totr,int totb,int pre){
if (u <= n) f[++ cnt] = node {totr,totb,pre};
for (Int i = head[u];i;i = nxt[i]){
int v = to[i];
if (v == fa || vis[i]) continue;
dfs (v,u,totr + (col[i] == 0),totb + (col[i] == 1),1ll * pre * wei[i] % mod);
}
}
int ed,lim,Siz,lena,lenb;
void findedge (int u,int fa){//找重边
siz[u] = 1;
for (Int i = head[u];i;i = nxt[i]){
int v = to[i];
if (v == fa || vis[i]) continue;
findedge (v,u),siz[u] += siz[v];
int tmp = max (siz[v],Siz - siz[v]);
if (tmp < lim) ed = i,lim = tmp;
}
}
bool cmp1 (node a,node b){return 2 * a.B - a.R < 2 * b.B - b.R;}
bool cmp2 (node a,node b){return 2 * a.R - a.B < 2 * b.R - b.B;}
void Solve (int u,int S){
if (S <= 1) return ;
lim = Siz = S,findedge (u,0),vis[ed] = vis[ed ^ 1] = 1;
cnt = 0,f = T1,dfs (to[ed],0,0,0,1),lena = cnt;
cnt = 0,f = T2,dfs (to[ed ^ 1],0,0,0,1),lenb = cnt;
for (Int i = 1;i <= lenb;++ i) T2[i].R += (col[ed] == 0),T2[i].B += (col[ed] == 1),T2[i].dis = 1ll * T2[i].dis * wei[ed] % mod;
sort (T1 + 1,T1 + lena + 1,cmp1);
pres[0] = 1;for (Int i = 1;i <= lena;++ i) pres[i] = 1ll * pres[i - 1] * T1[i].dis % mod;
for (Int i = 1;i <= lenb;++ i){
int now = T2[i].R - 2 * T2[i].B,l = 1,r = lena,fuckans = 0;
while (l <= r){
int mid = (l + r) >> 1;
if (2 * T1[mid].B - T1[mid].R < now) fuckans = mid,l = mid + 1;
else r = mid - 1;
}
ans = 1ll * ans * qkpow (T2[i].dis,fuckans) % mod * pres[fuckans] % mod;
}
sort (T1 + 1,T1 + lena + 1,cmp2);
pres[0] = 1;for (Int i = 1;i <= lena;++ i) pres[i] = 1ll * pres[i - 1] * T1[i].dis % mod;
for (Int i = 1;i <= lenb;++ i){
int now = T2[i].B - 2 * T2[i].R,l = 1,r = lena,fuckans = 0;
while (l <= r){
int mid = (l + r) >> 1;
if (2 * T1[mid].R - T1[mid].B < now) fuckans = mid,l = mid + 1;
else r = mid - 1;
}
ans = 1ll * ans * qkpow (T2[i].dis,fuckans) % mod * pres[fuckans] % mod;
}
int tx = to[ed],ty = to[ed ^ 1];
if (siz[tx] > siz[ty]) siz[tx] = S - siz[ty];
else siz[ty] = S - siz[tx];
Solve (tx,siz[tx]),Solve (ty,siz[ty]);
}
}
int cnt,all = 1,toop = 1,to[MAXN << 1],wei[MAXN << 1],col[MAXN << 1],nxt[MAXN << 1],head[MAXN],las[MAXN],siz[MAXN];
void Add_Edge (int u,int v,int w,int c){
to[++ toop] = v,wei[toop] = w,col[toop] = c,nxt[toop] = head[u],head[u] = toop;
to[++ toop] = u,wei[toop] = w,col[toop] = c,nxt[toop] = head[v],head[v] = toop;
}
void dfs (int u,int fa){
siz[u] = 1;
for (Int i = head[u];i;i = nxt[i]){
int v = to[i],w = wei[i],c = col[i];
if (v == fa) continue;
if (!las[u]) las[u] = u,Graph::Add_Edge (u,v,w,c);
else ++ cnt,Graph::Add_Edge (las[u],cnt,1,-1),Graph::Add_Edge (las[u] = cnt,v,w,c);
dfs (v,u),siz[u] += siz[v],all = 1ll * all * qkpow (w,1ll * siz[v] * (n - siz[v]) % (mod - 1)) % mod;
}
}
signed main(){
read (n),cnt = n;
for (Int i = 2,u,v,w,c;i <= n;++ i) read (u,v,w,c),Add_Edge (u,v,w,c);
dfs (1,0),Graph::Solve (1,cnt),write (1ll * all * inv (ans) % mod),putchar ('\n');
return 0;
}
题解 CF833D Red-Black Cobweb的更多相关文章
- 【CF833D】Red-Black Cobweb(点分治)
[CF833D]Red-Black Cobweb(点分治) 题面 CF 有一棵树,每条边有一个颜色(黑白)和一个权值,定义一条路径是好的,当且仅当这条路径上所有边的黑白颜色个数a,b满足2min(a, ...
- 【CF833D】Red-Black Cobweb
[CF833D]Red-Black Cobweb 题面 洛谷 题解 看到这种统计路径的题目当然是淀粉质啦. 考虑转化一下信息设一条路径上有红点\(a\)个,黑点\(b\)个 则\(2min(a,b)\ ...
- Hdoj 1312.Red and Black 题解
Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...
- CF833D Red-Black Cobweb
题面 题解 点分治大火题... 设白边数量为$a$,黑边为$b$,则$2min(a,b)\geq max(a,b)$ 即$2a\geq b\;\&\&2b\geq a$ 考虑点分治时如 ...
- 题解报告:hdu 1312 Red and Black(简单dfs)
Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...
- poj 1979 Red and Black 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=1979 Description There is a rectangular room, covered with square tiles ...
- 【CF1425B】 Blue and Red of Our Faculty! 题解
原题链接 简要翻译: 有一个连通图,A和B同时从点1出发,沿不同的路径前进.原本,图上的每一条边都是灰色的.A将经过的边涂成红色,B将经过的边涂成蓝色的.每个回合每个人只能走灰色的边.当某个回合中不存 ...
- CF833D Red-Black Cobweb 点分治、树状数组
传送门 统计所有路径的边权乘积的乘积,不难想到点分治求解. 边权颜色比例在\([\frac{1}{2},2]\)之间,等价于\(2B \geq R , 2R \geq B\)(\(R,B\)表示红色和 ...
- 洛谷 CF399B【Red and Blue Balls】题解
n年没有更博客:我总结出了规律,当学的东西很难得时候都去学习,没有时间写博客,只有 内容对于我这种蒟蒻友好,又让我非常闲的慌时才写博客,这种博客以后也没有价值(也有些是做完一道题有成就感写的) 最近内 ...
随机推荐
- pytorch 测试 迁移学习
训练源码: 源码仓库:https://github.com/pytorch/tutorials 迁移学习测试代码:tutorials/beginner_source/transfer_learning ...
- python入门-变量与数据类型
1.命名规则 变量名只能包含字母.数字和下划线.但不能以数字打头. 变量名不能包含空格 不能与关键字冲突 变量名应尽量简短且具有描述性 2.字符串 python中引号括起的内容,其中引号可以为单引号或 ...
- 移动端ios上下滑动翻页事件失效
移动端开发过程中,在添加上下滑动事件时候,引入了最常用的移动端库zepto.js及其touch模块,有一种现象,安卓的手机没有问题,上下滑动翻页很正常 :但是到了ios上面,好啊,上下滑动会出现弹性滚 ...
- 20210805 noip31
考场 没有一眼题 T1 想到先贪心地算出最大得分,任意构造出一种方案,不断调整以增大字典序. T2 发现在 \(x_k\) 确定的情况下操作次数就是左右两边的逆序对数,\(x_i\) 互不相同时直接找 ...
- Python - 如何将 list 列表作为数据结构使用
列表作为栈使用 栈的特点 先进后出,后进先出 如何模拟栈? 先在堆栈尾部添加元素,使用 append() 然后从堆栈顶部取出一个元素,使用 pop() # 模拟栈 stack = [1, 2, 3, ...
- Spring系列之集成MongoDB的2种方法
MongoDB是最流行的NoSQL数据库,SpringBoot是使用Spring的最佳实践.今天带大家讲一讲SpringBoot集成MongoDB的两种方式,MongoDB的安装自行去官网查询,本地开 ...
- Vue跨域问题解决
项目根目录下创建vue.config.js module.exports = { devServer: { proxy: { //配置跨域 '/api': { //这里是真实的后台接口 target: ...
- Sentry 后端监控 - 最佳实践(官方教程)
系列 1 分钟快速使用 Docker 上手最新版 Sentry-CLI - 创建版本 快速使用 Docker 上手 Sentry-CLI - 30 秒上手 Source Maps Sentry For ...
- Excel怎么把两个单元格中的文字合并到一个单元格中
使用&符号,可以将字符串和单元格中的内容拼接起来
- DFS与DFS迷宫问题
一天蒜头君掉进了一个迷宫里面,蒜头君想逃出去,可怜的蒜头君连迷宫是否有能逃出去的路都不知道. 看在蒜头君这么可怜的份上,就请聪明的你告诉蒜头君是否有可以逃出去的路. 输入格式 第一行输入两个整数n 和 ...