题意

\(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\) 的数量,那么答案为

\[\sum\limits_{i=1}^{n}\binom{u_i}{k}-\binom{u_i-v_i}{k}
\]

然后 \(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的更多相关文章

  1. 2018-2019 ACM-ICPC, Asia Xuzhou Regional Contest Solution

    A. Rikka with Minimum Spanning Trees 题意: 给出一个图,求最小生成树的个数和权值 思路: 因为数据随机,只有一个MST #include <bits/std ...

  2. 2018-2019 ACM-ICPC 徐州区域赛 部分题解

    题目链接:2018-2019 ACM-ICPC, Asia Xuzhou Regional Contest A. Rikka with Minimum Spanning Trees 题意: 给出一个随 ...

  3. 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 ...

  4. hdu1625 Numbering Paths (floyd判环)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission ...

  5. [LeetCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  6. [LeetCode] Unique Paths II 不同的路径之二

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  7. [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 ...

  8. leetcode : Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  9. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

随机推荐

  1. Spring AOP系列(五)—反射

    前言 前面我们进行了代理模式.静态代理.动态代理的学习.而动态代理就是利用Java的反射技术(Java Reflection),在运行时创建一个实现某些给定接口的新类(也称"动态代理类&qu ...

  2. makefile实验三 理解make工作的基本原则

    代码简单,但测试花样多,若能回答对本博客的每个步骤的预期结果,可以说对makefile的基础掌握是扎实的. 一,当前的makefile代码 root@ubuntu:~/Makefile_Test# r ...

  3. pytorch和tensorflow的爱恨情仇之参数初始化

    pytorch和tensorflow的爱恨情仇之基本数据类型 pytorch和tensorflow的爱恨情仇之张量 pytorch和tensorflow的爱恨情仇之定义可训练的参数 pytorch版本 ...

  4. Spring Boot第五弹,WEB开发初了解~

    持续原创输出,点击上方蓝字关注我吧 目录 前言 Spring Boot 版本 前提条件(必须注意) 添加依赖 第一个接口开发 如何自定义tomcat的端口? 如何自定义项目路径? JSON格式化 日期 ...

  5. OpenCV图像加载与保存

    OpenCV中的图像加载与保存 头文件是包含的库,在GitHub上下载的 imread("图片路径",图片加载方式) 图片加载方式: IMREAD_GRAYSCALE 灰度图像 I ...

  6. rxjs入门5之创建数据流

    一 创建同步数据流 1.creat Observable.create = function (subscribe) { return new Observable(subscribe); }; 2. ...

  7. Python日志采集(详细)

    通常在前期调试代码的时候,我们会使用print在IDE控制台打印一些信息,判断运行情况.但在运行整个自动化测试项目的过程中,通过print打印信息的方式获取运行情况显然行不通. 这时就需要收集日志,每 ...

  8. coder初入职场必备:Eclipse+Tomcat8+MAVEN+SVN 工作环境搭建

    1.JDK的安装 首先下载JDK,这个从sun公司官网可以下载,根据自己的系统选择64位还是32位,安装过程就是next一路到底.安装完成之后当然要配置环境变量了. ----------------- ...

  9. 手撸ORM浅谈ORM框架之基础篇

    好奇害死猫 一直觉得ORM框架好用.功能强大集众多优点于一身,当然ORM并非完美无缺,任何事物优缺点并存!我曾一度认为以为使用了ORM框架根本不需要关注Sql语句如何执行的,更不用关心优化的问题!!! ...

  10. 【原创】有利于提高xenomai 实时性的一些配置建议

    版权声明:本文为本文为博主原创文章,转载请注明出处.如有错误,欢迎指正. @ 目录 一.影响因素 1.硬件 2.BISO(X86平台) 3.软件 4. 缓存使用策略与GPU 二.优化措施 1. BIO ...