Gym102012G Rikka with Intersections of Paths
题意
\(T\) 组数据,每组数据给定一棵 \(n\) 个点的树和 \(m\) 条路径,求选出 \(k\) 条给定路径使得至少有两条交于一点的方案数,对 \(10^9+7\) 取模。
\(\texttt{Data Range:}1\leq T\leq 200,1\leq n\leq 3\times 10^5,2\leq m\leq 3\times 10^5,2\leq k\leq m\)。
题解
这种题都不能一次 AC,而且还是犯的弱智错误,我太菜了。
考虑这样一个结论:如果两条路径交于一些点,那么这些点中的某一个肯定是这两条路径中一条的两个端点的 LCA。
我们枚举这样一个端点,然后容斥。设 \(u_x\) 为这些路径中经过 \(x\) 的数量,\(v_x\) 为这些路径中两个端点的 LCA 为 \(x\) 的数量,那么答案为
\]
然后 \(v\) 是很容易维护的,\(u\) 树上差分一下就好了。
所以我们容易看出邪王真眼是最强的!!!
代码
#include<bits/stdc++.h>
using namespace std;
typedef int ll;
typedef long long int li;
const ll MAXN=3e5+51,MOD=1e9+7;
struct Edge{
ll to,prev;
};
Edge ed[MAXN<<1];
ll test,n,m,kk,tot,from,to,lca,res;
ll last[MAXN],u[MAXN],v[MAXN],depth[MAXN],anc[MAXN][20],fact[MAXN];
ll finv[MAXN],f[MAXN],diff[MAXN];
inline ll read()
{
register ll num=0,neg=1;
register char ch=getchar();
while(!isdigit(ch)&&ch!='-')
{
ch=getchar();
}
if(ch=='-')
{
neg=-1;
ch=getchar();
}
while(isdigit(ch))
{
num=(num<<3)+(num<<1)+(ch-'0');
ch=getchar();
}
return num*neg;
}
inline ll qpow(ll base,ll exponent)
{
ll res=1;
while(exponent)
{
if(exponent&1)
{
res=(li)res*base%MOD;
}
base=(li)base*base%MOD,exponent>>=1;
}
return res;
}
inline void setup(ll cnt)
{
fact[0]=fact[1]=finv[0]=1;
for(register int i=2;i<=cnt;i++)
{
fact[i]=(li)fact[i-1]*i%MOD;
}
finv[cnt]=qpow(fact[cnt],MOD-2);
for(register int i=cnt-1;i;i--)
{
finv[i]=(li)finv[i+1]*(i+1)%MOD;
}
}
inline ll binom(ll m,ll n)
{
return m<n?0:(li)fact[m]*finv[n]%MOD*finv[m-n]%MOD;
}
inline void addEdge(ll from,ll to)
{
ed[++tot].prev=last[from];
ed[tot].to=to;
last[from]=tot;
}
inline void dfs(ll node,ll fa)
{
depth[node]=depth[anc[node][0]=fa]+1;
for(register int i=last[node];i;i=ed[i].prev)
{
ed[i].to!=fa?dfs(ed[i].to,node):(void)1;
}
}
inline void LCASetup()
{
for(register int j=1;j<20;j++)
{
for(register int i=1;i<=n;i++)
{
anc[i][j]=anc[anc[i][j-1]][j-1];
}
}
}
inline ll LCA(ll x,ll y)
{
depth[x]<depth[y]?swap(x,y):(void)1;
for(register int i=19;i>=0;i--)
{
depth[anc[x][i]]>=depth[y]?x=anc[x][i]:1;
}
for(register int i=19;i>=0;i--)
{
anc[x][i]!=anc[y][i]?x=anc[x][i],y=anc[y][i]:1;
}
return x==y?x:anc[x][0];
}
inline void dfs2(ll node,ll fa)
{
ll to;
for(register int i=last[node];i;i=ed[i].prev)
{
(to=ed[i].to)!=fa?dfs2(to,node),diff[node]+=diff[to]:1;
}
}
inline void solve()
{
n=read(),m=read(),kk=read(),tot=0,memset(last,0,sizeof(last));
for(register int i=0;i<n-1;i++)
{
from=read(),to=read(),addEdge(from,to),addEdge(to,from);
}
dfs(1,0),LCASetup(),memset(f,0,sizeof(f)),memset(diff,0,sizeof(diff));
for(register int i=1;i<=m;i++)
{
u[i]=read(),v[i]=read(),f[lca=LCA(u[i],v[i])]++;
diff[u[i]]++,diff[v[i]]++,diff[lca]--,diff[anc[lca][0]]--;
}
dfs2(1,0),res=0;
for(register int i=1;i<=n;i++)
{
res=(res+(binom(diff[i],kk)-binom(diff[i]-f[i],kk)+MOD)%MOD)%MOD;
}
printf("%d\n",res);
}
int main()
{
test=read(),setup(300010);
for(register int i=0;i<test;i++)
{
solve();
}
}
Gym102012G Rikka with Intersections of Paths的更多相关文章
- 2018-2019 ACM-ICPC, Asia Xuzhou Regional Contest Solution
A. Rikka with Minimum Spanning Trees 题意: 给出一个图,求最小生成树的个数和权值 思路: 因为数据随机,只有一个MST #include <bits/std ...
- 2018-2019 ACM-ICPC 徐州区域赛 部分题解
题目链接:2018-2019 ACM-ICPC, Asia Xuzhou Regional Contest A. Rikka with Minimum Spanning Trees 题意: 给出一个随 ...
- POJ 3177 Redundant Paths & POJ 3352 Road Construction(双连通分量)
Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numb ...
- hdu1625 Numbering Paths (floyd判环)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission ...
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- [LeetCode] Unique Paths II 不同的路径之二
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [LeetCode] Unique Paths 不同的路径
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- leetcode : Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
随机推荐
- Go 指针相关
Go指针 Go语言中的指针非常简单,没有偏移和运算,只需要记住两个符号.&取变量地址与*根据地址取值. 以下是一个简单的示例: package main import ( "fmt& ...
- Web Storage API的介绍和使用
目录 简介 浏览器的本地存储技术 Web Storage相关接口 浏览器兼容性 隐身模式 使用Web Storage API 总结 简介 Web Storage为浏览器提供了方便的key value存 ...
- C/C++ 条件编译
条件编译就是指有条件的编译,即根据条件去编译代码,在编译阶段时就对代码做出取舍,有的编译,有的不编译,这样比写成一个个判断函数更有效率,比如工程代码大部分的地方都类似,只有个别语句因为使用的硬件版本不 ...
- 使用HTML的基本结构创建网页
1. 网页的扩展名--html或htm 2. 如何新建网页? 步骤1: 在电脑的空白处,右键选择-->新建--文本文档 步骤2: 把txt的扩展名,改成html或htm, ...
- Linux就该这么学28期——开篇
2020.10.03 正式开始系统学习Linux之旅,希望能在老刘的带领下,掌握操作要领. 现将所学记录在此. 学习环境如下: VmwareWorkStation 15 --虚拟机软件 versio ...
- idea 2020.1 Mybatis log plugin破解插件
下载 链接: https://pan.baidu.com/s/1FTgtJiyzxxaNxWLyX4OgZw 密码: w7w8 idea安装本地插件
- regsvr32 bypass windows defender 新思路
原文链接:blog 在对regsvr32的用法进行了解之后,对于Casey Smith的远程js脚本执行命令的思路很感兴趣. 命令语法如下: regsvr32 /s /n /u /i:http://1 ...
- python简单实现论文查重(软工第一次项目作业)
前言 软件工程 https://edu.cnblogs.com/campus/gdgy/informationsecurity1812 作业要求 https://edu.cnblogs.com/cam ...
- 持续集成工具之Jenkins使用配置
在上一篇博客中,我们主要介绍了DevOps理念以及java环境和jenkins的安装,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/13805666.html: ...
- sessionFactory' defined in class path /mappingDirectoryLocations配置问题
问题:配置好aplicationContext.xml,启动tomcat 出现如下问题 sessionFactory无法正常建立 Context initialization failed org.s ...