现场赛大佬打印的代码,观摩了一哈。

写了注释,贴一下,好好学习。%%%PKU

代码:

 //树上差分(LCA)
#include<bits/stdc++.h> #define For(i,x,y) for (int i=x;i<y;i++)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define lf else if #define dprintf(...) fprintf(stderr,__VA_ARGS__)
using namespace std; typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
typedef vector<int> Vi; int IN(){//读入挂
int c,f,x;
while (!isdigit(c=getchar())&&c!='-');c=='-'?(f=,x=):(f=,x=c-'');
while (isdigit(c=getchar())) x=(x<<)+(x<<)+c-'';return !f?x:-x;
} const int p=1e9+;
const int N=3e5+; int Pow(int a,int b){//快速幂
int res=;
for (;b;b>>=,a=1ll*a*a%p) if (b&) res=1ll*res*a%p;
return res;
} struct Edge{
int y,nxt;
} E[N*];
int fac[N],inv[N];
int las[N],fa[][N],A[N],B[N],dep[N];
int n,m,cnt,x,y,z,ans,k; int C(int n,int m){//组合数
if (n<m) return ;
return 1ll*fac[n]*inv[m]%p*inv[n-m]%p;
} void Link(int x,int y){//链式前向星存图
E[cnt]=(Edge){y,las[x]};las[x]=cnt++;
E[cnt]=(Edge){x,las[y]};las[y]=cnt++;
} void dfs(int x){//LCA的dfs
for (int i=las[x],y;~i;i=E[i].nxt)
if ((y=E[i].y)!=fa[][x]){
fa[][y]=x;
dep[y]=dep[x]+;
dfs(y);
}
} int LCA(int x,int y){//LCA(ST)
if (dep[x]>dep[y]) swap(x,y);
for (int i=dep[y]-dep[x],k=;i;i>>=,k++) if(i&) y=fa[k][y];
if(x==y) return x;
for (int i=;~i;i--) if (fa[i][x]!=fa[i][y]) x=fa[i][x],y=fa[i][y];
return fa[][x];
} void Dfs(int x){//树上差分的dfs,从根节点深搜,回溯时将其本身的权值加上所有子节点的权值
for (int i=las[x],y;~i;i=E[i].nxt)
if ((y=E[i].y)!=fa[][x]){//筛掉父节点
Dfs(y);
A[x]+=A[y];//累加权值和
B[x]+=B[y];
}
} void Main(){
n=IN(),m=IN(),k=IN();
For(i,,n+) las[i]=-,A[i]=B[i]=;
cnt=;
For(i,,n) Link(IN(),IN());
dfs();
For(i,,) For(x,,n+) fa[i][x]=fa[i-][fa[i-][x]];
For(i,,m+){
x=IN(),y=IN();
z=LCA(x,y);
A[x]++,A[y]++,A[z]--,A[fa[][z]]--;
B[x]++,B[y]++,B[z]-=;//起点终点权值+1,lca权值-2
}
Dfs();
ans=;
// cout<<"--------"<<endl;
// for(int i=1;i<=n;i++)
// cout<<i<<" "<<A[i]<<endl;
// cout<<"--------"<<endl;
// for(int i=2;i<=n;i++)
// cout<<i<<" "<<B[i]<<endl;
// cout<<"--------"<<endl;
For(i,,n+){
ans=(ans+C(A[i],k))%p;
}
For(i,,n+){
ans=(ans-C(B[i],k)+p)%p;
}
printf("%d\n",ans);
} int main(){
fac[]=;
For(i,,N) fac[i]=1ll*fac[i-]*i%p;
inv[N-]=Pow(fac[N-],p-);
for(int i=N-;i;i--) inv[i-]=1ll*inv[i]*i%p;
for(int T=IN();T--;) Main();
} /*
1
3 6 2
1 2
1 3
1 1
2 2
3 3
1 2
1 3
2 3
*/

OK.

最新文章

  1. ASP.NET 发布到IIS后支撑woff 的解决方案
  2. html、url、http、servlet&amp;jsp之间千丝万缕的联系
  3. CSS控制背景
  4. UVa 10562 (特殊的输入处理方式) Undraw the Trees
  5. Es6 之箭头函数 初学
  6. 从PHP程序员到RAW开发~
  7. 基本属性 - iOS中的本地通知
  8. mac 下mysql
  9. linux静态ip的设置
  10. VGG19模型训练+读取
  11. MyBatis实操进阶版(一)
  12. ubuntu环境下编译linux内核问题解决备忘
  13. 华为交换机MSTP+VRRP配置实例说明文档
  14. php三种无限分类
  15. SQL提交数据三种类型
  16. 通过 CeSi + Supervisor 可视化集中管理服务器节点进程
  17. sublime text2 注册码
  18. python中__name__属性的使用
  19. oracle中如何把结果集插入临时表中
  20. DDD学习笔记(一)

热门文章

  1. ListBox, ListView, GridView
  2. swiper 、css3制作移动端网站,折叠导航
  3. 数据结构:K-D树
  4. \(\rm LightOJ 1371 - Energetic Pandas 简单计数+组合\)
  5. python实现备份gitlab版本库并更改文件名
  6. Item 4 ----通过私有构造器强化不可实例化的能力
  7. 【BZOJ】3502 PA2012 Tanie linie
  8. 使用TSQL语句操作MySQL数据库
  9. MongoDB 数据库(2)
  10. a标签的嵌套