洛谷

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 1000000007lltypedef 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_JUDGEfreopen("a.in","r",stdin);#endif}inline void chktime(){#ifndef ONLINE_JUDGEcout<<(clock()-t)/1000.0<<'\n';#endif}#ifdef modll 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);}#elsell 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 vint 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;
}

转载于:https://www.cnblogs.com/p-b-p-b/p/10499952.html

Codeforces 833D Red-Black Cobweb [点分治]相关推荐

  1. Codeforces 833D Red-Black Cobweb 边分治

    题意 一颗树,有边权,和颜色(红或黑) 求,所有的路径中,满足两种颜色的个数差不超过少的颜色的两倍的路径的权值的乘积 路径的权值为经过的边的权值的乘积 题解 边分治牛逼!!! 出现了两个错误,一个是, ...

  2. Codeforces 997D Cycles in Product (点分治、DP计数)

    题目链接 https://codeforces.com/contest/997/problem/D 题解 点分治这个思路想不到== 首先这两棵树的笛卡尔积并没有什么用处,因为笛卡尔积中的环就是两棵树中 ...

  3. Educational Codeforces Round 32 G. Xor-MST 01tire + 分治 + Boruvka

    传送门 文章目录 题意: 思路: 题意: 给你一个长度为nnn序列aaa,每两个点之间的边权为ai⊕aja_i\oplus a_jai​⊕aj​,问你最小生成树的权值是多少. n≤2e5,ai< ...

  4. CodeForces - 1324D Pair of Topics (分治+排序)

    CodeForces - 1324D Pair of Topics 题目大意: 这题大意ai+aj>bi+bj全在这个式子上,就问你满足的组合有几种, 题目分析: 整理一下,得到(ai-bi)+ ...

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

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

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

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

  7. 清北学堂培训2019.4.28

    Day 1(冯哲) 今天的内容很杂但却都是基础知识 主要分为下面几个点 枚举 枚举也称作穷举,指的是从问题所有可能的解的集合中一一枚举各元素.用题目中给定的检验条件判定哪些是无用的,哪些是有用的.能使 ...

  8. codeforces 293E Close Vertices 点分治+滑窗+treap

    http://codeforces.com/contest/293/problem/E 题意:求树上合法点对的个数.合法条件为:路径长度<=W,路径边数<=L. 显然是点分治.求解的时候第 ...

  9. Codeforces 448C Painting Fence:分治

    题目链接:http://codeforces.com/problemset/problem/448/C 题意: 有n个木板竖着插成一排栅栏,第i块木板高度为a[i]. 你现在要将栅栏上所有地方刷上油漆 ...

最新文章

  1. BZOJ2588 Count on a tree 【树上主席树】
  2. .net MVC(存储过程+SQLHelper)
  3. 一个XP使用者眼中的Windows 7
  4. 基于Tensorflow的神经网络解决用户流失概率问题
  5. 家卫士扫地机器人好吗_扫地机器人哪个牌子好?市场最全智能扫地机器人品牌解析_扫地机器人...
  6. ABP官方文档翻译 5.1 Web API控制器
  7. 基于sklearn和keras的数据切分与交叉验证
  8. webx rewrite
  9. myeclipse以及tomcat乱码解决
  10. 如何处理pagefile.sys占用太多C盘空间
  11. 网页版魔兽争霸游戏策划
  12. linux 相机,linux下使用大恒相机实时运行ORB-SLAM
  13. App关键字(100字符)优化的方法
  14. NR/5G - Measurement, GAP, SFTD
  15. 数据挖掘实战(4)——聚类(Kmeans、MiniBatchKmeans、DBSCAN、AgglomerativeClustering、MeanShift)
  16. 练习- Java顺序结构综合练习二之温度换算
  17. 简易单片机制作频率计
  18. 科研工具|Ubuntu 装机那些事(更新中)☺️
  19. leetcode526 优美的排列
  20. 二分图(三)——KM算法

热门文章

  1. 长江商学院营销学李洋教授分析大数据与精准营销
  2. Debian服务器更改时区为中国
  3. 撸了一次 Js 代码
  4. 云服务厂商人才空心化隐忧
  5. HUAWEI 机试题:工厂流水线调度
  6. 央视推荐的护眼灯是哪款?分享央视曝光护眼灯品牌
  7. 如何通过学习实现人生的逆袭!
  8. 使用函数创建多个备选BOM
  9. python操作pdf——pdfplumber/PyPDF2
  10. RPC:RPC的通信流程