洛谷

Codeforces


思路

看到树上路径的统计,容易想到点分治。

虽然只有一个限制,但这个限制比较麻烦,我们把它拆成两个。

设黑边有\(a\)条,白边有\(b\)条,那么有

\[2a\geq b\\
2b\geq a
\]

合并两条边时,设原有的是\((a,b)\),要加入的是\((A,B)\),那么有

\[2(a+A)\geq b+B \Leftrightarrow 2A-B\geq b-2a\\
2(b+B)\geq a+A \Leftrightarrow 2B-A\geq a-2b \Leftrightarrow A-2B\leq 2b-a
\]

但现在有两个限制,似乎还要CDQ分治,非常麻烦。

注意到一个性质:只要不满足第一条,必然满足第二条,反之亦然。

那么可以用两个树状数组维护前缀积,每次用满足第一条的除以不满足第二条的即可。

复杂度\(O(n\log ^2 n)\)?\(O(n\log ^3 n)\)?反正比那些CDQ分治的要快就对了。


代码

#include<bits/stdc++.h>
clock_t t=clock();
namespace my_std{
using namespace std;
#define pii pair<int,int>
#define fir first
#define sec second
#define MP make_pair
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
#define drep(i,x,y) for (int i=(x);i>=(y);i--)
#define go(x) for (int i=head[x];i;i=edge[i].nxt)
#define templ template<typename T>
#define sz 101100
#define mod 1000000007ll
typedef long long ll;
typedef double db;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
templ inline T rnd(T l,T r) {return uniform_int_distribution<T>(l,r)(rng);}
templ inline bool chkmax(T &x,T y){return x<y?x=y,1:0;}
templ inline bool chkmin(T &x,T y){return x>y?x=y,1:0;}
templ inline void read(T& t)
{
t=0;char f=0,ch=getchar();double d=0.1;
while(ch>'9'||ch<'0') f|=(ch=='-'),ch=getchar();
while(ch<='9'&&ch>='0') t=t*10+ch-48,ch=getchar();
if(ch=='.'){ch=getchar();while(ch<='9'&&ch>='0') t+=d*(ch^48),d*=0.1,ch=getchar();}
t=(f?-t:t);
}
template<typename T,typename... Args>inline void read(T& t,Args&... args){read(t); read(args...);}
char __sr[1<<21],__z[20];int __C=-1,__Z=0;
inline void __Ot(){fwrite(__sr,1,__C+1,stdout),__C=-1;}
inline void print(register int x)
{
if (__C>1<<20) __Ot(); if (x<0) __sr[++__C]='-',x=-x;
while (__z[++__Z]=x%10+48,x/=10);
while (__sr[++__C]=__z[__Z],--__Z);__sr[++__C]='\n';
}
void file()
{
#ifndef ONLINE_JUDGE
freopen("a.in","r",stdin);
#endif
}
inline void chktime()
{
#ifndef ONLINE_JUDGE
cout<<(clock()-t)/1000.0<<'\n';
#endif
}
#ifdef mod
ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x%mod) if (y&1) ret=ret*x%mod;return ret;}
ll inv(ll x){return ksm(x,mod-2);}
#else
ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x) if (y&1) ret=ret*x;return ret;}
#endif
// inline ll mul(ll a,ll b){ll d=(ll)(a*(double)b/mod+0.5);ll ret=a*b-d*mod;if (ret<0) ret+=mod;return ret;}
}
using namespace my_std; int n;
struct hh{int t;ll w;int nxt;int col;}edge[sz<<1];
int head[sz],ecnt;
void make_edge(int f,int t,ll w,int col)
{
edge[++ecnt]=(hh){t,w,head[f],col};
head[f]=ecnt;
edge[++ecnt]=(hh){f,w,head[t],col};
head[t]=ecnt;
} int T;
struct BIT
{
ll prod[sz<<2];
int size[sz<<2];
int mark[sz<<2];
void mul(int x,ll w)
{
x+=n*2;
while (x<=n*4)
{
if (mark[x]!=T) size[x]=0,prod[x]=1;
++size[x],prod[x]=prod[x]*w%mod;mark[x]=T;
x+=(x&(-x));
}
}
void query(int x,ll &w1,int &w2)
{
w1=1,w2=0;x+=n*2;
while (x)
{
if (mark[x]==T) w1=w1*prod[x]%mod,w2+=size[x];
x-=(x&(-x));
}
}
}T1,T2; ll ans=1;
#define v edge[i].t
bool vis[sz];
int size[sz],mn,root,tot;
void findroot(int x,int fa)
{
size[x]=1;
int S=-1;
go(x) if (v!=fa&&!vis[v])
{
findroot(v,x);
size[x]+=size[v];
chkmax(S,size[v]);
}
chkmax(S,tot-size[x]);
if (chkmin(mn,S)) root=x;
}
struct hhh{int aa,bb;ll w;};
hhh s[sz];int cnt;
void dfs(int x,int fa,int a,int b,ll w)
{
s[++cnt]=(hhh){2*a-b,2*b-a,w};
go(x) if (v!=fa&&!vis[v])
{
if (edge[i].col) dfs(v,x,a,b+1,w*edge[i].w%mod);
else dfs(v,x,a+1,b,w*edge[i].w%mod);
}
}
void calc(int x)
{
++T;T1.mul(0,1);T2.mul(0,1);
go(x) if (!vis[v])
{
cnt=0;
if (edge[i].col) dfs(v,0,0,1,edge[i].w);
else dfs(v,0,1,0,edge[i].w);
rep(i,1,cnt)
{
ll w1;int w2;
T1.query(s[i].aa,w1,w2);
ans=ans*w1%mod*ksm(s[i].w,w2)%mod;
T2.query(-s[i].bb-1,w1,w2);
ans=ans*inv(w1*ksm(s[i].w,w2)%mod)%mod;
}
rep(i,1,cnt) T1.mul(-s[i].aa,s[i].w),T2.mul(s[i].bb,s[i].w);
}
}
void solve(int x)
{
vis[x]=1;
calc(x);
int all=tot;
go(x) if (!vis[v])
{
tot=size[v];if (size[v]>size[x]) tot=all-size[x];mn=1e9;
findroot(v,0);
solve(root);
}
}
#undef v int main()
{
file();
int x,y,z,c;
read(n);
rep(i,1,n-1) read(x,y,z,c),make_edge(x,y,z,c);
tot=n;mn=1e9;findroot(1,0);
solve(root);
cout<<ans;
return 0;
}

Codeforces 833D Red-Black Cobweb [点分治]的更多相关文章

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

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

  2. CodeForces 958F3 Lightsabers (hard) 启发式合并/分治 多项式 FFT

    原文链接http://www.cnblogs.com/zhouzhendong/p/8835443.html 题目传送门 - CodeForces 958F3 题意 有$n$个球,球有$m$种颜色,分 ...

  3. Codeforces 1093E Intersection of Permutations [CDQ分治]

    洛谷 Codeforces 思路 一开始想到莫队+bitset,发现要T. 再想到分块+bitset,脑子一抽竟然直接开始写了,当然也T了. 最后发现这就是个裸的CDQ分治-- 发现\(a\)不变,可 ...

  4. codeforces 549F Yura and Developers(分治、启发式合并)

    codeforces 549F Yura and Developers 题意 给定一个数组,问有多少区间满足:去掉最大值之后,和是k的倍数. 题解 分治,对于一个区间,找出最大值之后,分成两个区间. ...

  5. codeforces 399B. Red and Blue Balls 解题报告

    题目链接:http://codeforces.com/problemset/problem/399/B 题目意思:给出 n 个只由 R 和 B 组成的字符串(由上到下排列,相当于栈),问最多可以操作多 ...

  6. codeforces 873 D. Merge Sort(分治)

    题目链接:http://codeforces.com/contest/873/problem/D 题解:这题挺简单的,除了一开始算作是调用到一次,然后每次执行操作时都会调用2次,所以最多调用几次就很好 ...

  7. Codeforces 566C - Logistical Questions(点分治)

    Codeforces 题目传送门 & 洛谷题目传送门 神仙题 %%% 首先考虑对这个奇奇怪怪的 \(t^{3/2}\) 进行一番观察.考虑构造函数 \(f(x)=ax^{3/2}+b(d-x) ...

  8. Codeforces 1442D - Sum(找性质+分治+背包)

    Codeforces 题面传送门 & 洛谷题面传送门 智商掉线/ll 本来以为是个奇怪的反悔贪心,然后便一直往反悔贪心的方向想就没想出来,看了题解才发现是个 nb 结论题. Conclusio ...

  9. Codeforces 848C Goodbye Souvenir(CDQ 分治)

    题面传送门 考虑记录每个点的前驱 \(pre_x\),显然答案为 \(\sum\limits_{i=l}^{r} i-pre_i (pre_i \geq l)\) 我们建立一个平面直角坐标系,\(x\ ...

随机推荐

  1. 对xml进行数据查询时发生NoClassDefFoundError,dom4j和jaxen

    xml可扩展标记语言,标准通用标记语言的子集,是一种用于标记电子文件使其具有结构性的标记语言. 在web中,今天我本想测试一下用xml做为数据库存储用户信息,但是在查询用户信息的时候一直发生: jav ...

  2. article2pdf (Wordpress plug-in) Multiple vulnerabilities(CVE-2019-1000031, CVE-2019-1010257)

    Product: article2pdf (Wordpress plug-in)Product Website: https://wordpress.org/plugins/article2pdf/A ...

  3. matplotlib-2D绘图库-面向对象

    个人理解:plt--画本    figure--产生画纸 子图 import numpy as np import matplotlib.pyplot as plt #解决能显示中文 plt.rcPa ...

  4. Socket与TCP,UDP

    什么是socket 简单来说是IP地址与端口的结合协议(RFC 793) 一种地址与端口的结合描述协议 TCP/IP协议的 相关API的总称:是网络api的集合实现 涵盖了:Stream Socket ...

  5. Loadrunner 如何在其他浏览器进行录制(一)

    背景: 由于lr只支持低版本的IE浏览器,当我们想使用高版本或其他浏览器进行录制时,这时,我们需要用到浏览器的代理功能. 传统的访问模式如下: 使用代理后的访问方式: 下面来总结一下具体的步骤: 1. ...

  6. bash 文件名操作 常用方法

    参考链接: http://www.jb51.net/article/51592.htm 查找文件不获取路径: find $1 -name '*.bin' -exec basename {} \;

  7. vue-axios

    vue axios全攻略   不再继续维护vue-resource,并推荐大家使用 axios 开始,axios 被越来越多的人所了解.本来想在网上找找详细攻略,突然发现,axios 的官方文档本身就 ...

  8. L3-020 至多删三个字符 (30 分)(DP)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805046946938880 学习地址: 2018CCCC-L3 ...

  9. Spring4之IOC

    或第六讲前也是讲此知识. \[www.dev1234.com]一头扎进Spring4视频教程\一头扎进Spring4源码\[www.java1234.com]<一头扎进Spring4>第六 ...

  10. 使用SpringSocial开发微信登录

    ⒈编写微信用户对应的数据结构 package cn.coreqi.social.weixin.entities; /** * 微信用户实体类 */ public class WeixinUserInf ...