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

写了注释,贴一下,好好学习。%%%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.

2018 icpc 徐州现场赛G-树上差分+组合数学-大佬的代码的更多相关文章

  1. 2018 ICPC 徐州网络赛

    2018 ICPC 徐州网络赛 A. Hard to prepare 题目描述:\(n\)个数围成一个环,每个数是\(0\)~\(2^k-1\),相邻两个数的同或值不为零,问方案数. solution ...

  2. 2018 icpc 徐州网络赛 F Features Track

    这个题,我也没想过我这样直接就过了 #include<bits/stdc++.h> using namespace std; ; typedef pair<int,int> p ...

  3. 【2018 ICPC焦作网络赛 G】Give Candies(费马小定理+快速幂取模)

    There are N children in kindergarten. Miss Li bought them N candies. To make the process more intere ...

  4. 2018 ICPC 沈阳网络赛

    2018 ICPC 沈阳网络赛 Call of Accepted 题目描述:求一个算式的最大值与最小值. solution 按普通算式计算方法做,只不过要同时记住最大值和最小值而已. Convex H ...

  5. 2018 icpc 徐州

    A 矩阵树定理可以用于最小生成树计数,最直观的做法就是求个mst,再用矩阵树定理求最小生成树个数,但是n<=1e5,显然不是o(n^3)可以做出来的. 考虑随机数据生成器,固定1e5的边,但是边 ...

  6. 【2018 ICPC亚洲区域赛徐州站 A】Rikka with Minimum Spanning Trees(求最小生成树个数与总权值的乘积)

    Hello everyone! I am your old friend Rikka. Welcome to Xuzhou. This is the first problem, which is a ...

  7. hihoCoder #1871 : Heshen's Account Book-字符串暴力模拟 自闭(getline()函数) (ACM-ICPC Asia Beijing Regional Contest 2018 Reproduction B) 2018 ICPC 北京区域赛现场赛B

    P2 : Heshen's Account Book Time Limit:1000ms Case Time Limit:1000ms Memory Limit:512MB Description H ...

  8. hihoCoder #1870 : Jin Yong’s Wukong Ranking List-闭包传递(递归) (ACM-ICPC Asia Beijing Regional Contest 2018 Reproduction A) 2018 ICPC 北京区域赛现场赛A

    P1 : Jin Yong’s Wukong Ranking List Time Limit:1000ms Case Time Limit:1000ms Memory Limit:512MB Desc ...

  9. 2018徐州现场赛A

    题目链接:http://codeforces.com/gym/102012/problem/A 题目给出的算法跑出的数据是真的水 #include<iostream> #include&l ...

随机推荐

  1. 旋转 3d

    建议chorme浏览器浏览,有样式兼容性问题. 图片可以根据自己本地路径设置路径,js库引用jquery. 写的不好,多多建议,谢谢大家. <html onselectstart="r ...

  2. 二叉树系列 - 二叉搜索树 - [LeetCode] 中序遍历中利用 pre节点避免额外空间。题:Recover Binary Search Tree,Validate Binary Search Tree

    二叉搜索树是常用的概念,它的定义如下: The left subtree of a node contains only nodes with keys less than the node's ke ...

  3. 2017 济南综合班 Day 7

     a 两个指针L.R R开始指向恰好[R,n]有不超过k个逆序对的地方 随着L的右移,R指针只会右移 逆序对有2部分 1.L左侧与其他位置形成的逆序对 2.R右侧与其他位置形成的逆序对 用树状数组分别 ...

  4. POJ 2823 Sliding Window ST RMQ

    Description An array of size n ≤ 106 is given to you. There is a sliding window of size k which is m ...

  5. Sass 基本特性-运算 感觉满满都是坑

    Sass中的基本运算 一.加法 在 CSS 中能做运算的,到目前为止仅有 calc() 函数可行.但在 Sass 中,运算只是其基本特性之一.      sass做加法运算是可以不考虑参数带单位,但需 ...

  6. Google 字体API的基本使用

    一.链接CSS文件直接使用: 基本上你链接直接在Google.com上的CSS文件.通过网址参数,你可以选择你想要的字体,以及这些字体的变化. <link rel="styleshee ...

  7. POj 2104 K-th Number (分桶法+线段树)

    题目链接 Description You are working for Macrohard company in data structures department. After failing ...

  8. python keras YOLOv3实现目标检测

    1.连接 https://www.jianshu.com/p/3943be47fe84

  9. 复现VGG19训练自定义图像分类

    1.复现VGG训练自定义图像分类,成功了哈哈. 需要代码工程可联系博主qq号,在左边连接可找到. 核心代码: # coding:utf-8 import tensorflow as tf import ...

  10. Chrome 浏览器 autocomplete off无效

    在表单填写时突然发现autocomplete 失效了 网上搜索后得出大概意思是在某些情况下确实无效[捂脸] 解决方案 大致原因是浏览器默认为type为password的input标签自动填充密码 这样 ...