洛谷

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. IIS 常见异常及解决办法

    Ø  简介 IIS 是我们平常接触比较多的服务端软件,用于站点发布等,本文主要记录 IIS 常见的异常及解决办法.主要包括: 1.   Visual Studio 启动 Web 项目提示"无 ...

  2. c# cbo控件

    c#  cbo控件 ,要获取选中的值 最好使用 ((强转的类型)selectitem).属性

  3. html页面设置<span>的高度和宽度

    <span>标签属于行内元素(inline),所以无法设置高度和宽度:如果需要改变其宽高,就需要将其转变为块体元素(block)或行内块体元素(inle-block): 1 span{di ...

  4. JavaScript面试技巧(一):基础知识

    1.变量类型和计算 变量类型:值类型.引用类型.typeof运算符. 变量计算:字符串拼接.==运算符.if语句.逻辑运算符 2.原型和原型链 构造函数 5个原型规则 3.作用域和闭包-执行上下文 4 ...

  5. python后端将svc文件数据读入数据库具体实现

    如何用python将svc文件的数据读入到MySQL数据库里,在此直接上代码了,感兴趣的朋友可以贴代码测试: import pandas as pd import os from sqlalchemy ...

  6. MySql数据库学习笔记(2)

    DELETE 语法:delete from 表名 [where condition] delete from grade; TRUNCATE 用于完全清空表数据,但表结构.索引.约束不变: 语法: t ...

  7. undefined symbol

    参考链接:  https://blog.csdn.net/shatterheart/article/details/52440149

  8. Android逆向基础----Dalvik字节码

    参考此微博,更多详细内容可以到这里查看 http://blog.csdn.net/dd864140130/article/details/52076515 Dalvik字节码 1.寄存器位32位,64 ...

  9. python - 条件语句/循环语句/迭代器

    条件测试:if 条件表达式python 的比较操作        所有的python对象都支持比较操作            可用于测试相等性.相对大小等            如果是复合对象,pyt ...

  10. TensorFlow学习笔记之--[tf.app.flags使用方法]

    很多时候在运行python代码的时候我们需要从外部定义参数,从而避免每次都需要改动代码.所以一般我们都会使用 argparse 这个库.其实TensorFlow也提供了这个功能,那就是 tf.app. ...