2018 icpc 徐州现场赛G-树上差分+组合数学-大佬的代码
现场赛大佬打印的代码,观摩了一哈。
写了注释,贴一下,好好学习。%%%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-树上差分+组合数学-大佬的代码的更多相关文章
- 2018 ICPC 徐州网络赛
2018 ICPC 徐州网络赛 A. Hard to prepare 题目描述:\(n\)个数围成一个环,每个数是\(0\)~\(2^k-1\),相邻两个数的同或值不为零,问方案数. solution ...
- 2018 icpc 徐州网络赛 F Features Track
这个题,我也没想过我这样直接就过了 #include<bits/stdc++.h> using namespace std; ; typedef pair<int,int> p ...
- 【2018 ICPC焦作网络赛 G】Give Candies(费马小定理+快速幂取模)
There are N children in kindergarten. Miss Li bought them N candies. To make the process more intere ...
- 2018 ICPC 沈阳网络赛
2018 ICPC 沈阳网络赛 Call of Accepted 题目描述:求一个算式的最大值与最小值. solution 按普通算式计算方法做,只不过要同时记住最大值和最小值而已. Convex H ...
- 2018 icpc 徐州
A 矩阵树定理可以用于最小生成树计数,最直观的做法就是求个mst,再用矩阵树定理求最小生成树个数,但是n<=1e5,显然不是o(n^3)可以做出来的. 考虑随机数据生成器,固定1e5的边,但是边 ...
- 【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 ...
- 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 ...
- 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 ...
- 2018徐州现场赛A
题目链接:http://codeforces.com/gym/102012/problem/A 题目给出的算法跑出的数据是真的水 #include<iostream> #include&l ...
随机推荐
- Eclipse中安装插件的三种方式
转载自:http://www.cnblogs.com/lcgustc/archive/2013/01/03/2843496.html Eclipse也用了很久,但是都没有怎么去研究过怎么安装插件,今天 ...
- [Luogu 1160] 队列安排
Luogu 1160 队列安排 链表H2O H2O H2O模板. 太久不写链表,忘干净了,竟调了半个晚上. 保留备用. #include <cstdio> #include <cst ...
- javascript「篱式」条件判断
我们已经知道,null 没有任何的属性值,并且无法获取其实体(existence)值.所以 null.property 返回的是错误(error)而不是 undefined . 考虑下面的代码 if ...
- [LA3523/uva10195]圆桌骑士 tarjan点双连通分量+奇环定理+二分图判定
1.一个环上的各点必定在同一个点双连通分量内: 2.如果一个点双连通分量是二分图,就不可能有奇环: 最基本的二分图中的一个环: #include<cstdio> #include<c ...
- 【BZOJ3875】【AHOI2014】骑士游戏 [Spfa][DP]
骑士游戏 Time Limit: 30 Sec Memory Limit: 256 MB[Submit][Status][Discuss] Description 在这个游戏中,JYY一共有两种攻击 ...
- 【Foreign】Bumb [模拟退火]
Bumb Time Limit: 20 Sec Memory Limit: 512 MB Description Input Output Sample Input 4 1 5 1 4 Sample ...
- bzoj 1700: [Usaco2007 Jan]Problem Solving 解题 ——dp
Description 过去的日子里,农夫John的牛没有任何题目. 可是现在他们有题目,有很多的题目. 精确地说,他们有P (1 <= P <= 300) 道题目要做. 他们还离开了农场 ...
- bzoj 2121 DP
首先如果我们能处理出来i,j段能不能消掉,这样就可以直接dp转移了,设w[i]为前i为最少剩下多少,那么w[i]=w[j-1] (flag[j][i]). 现在我们来求flag[i][j],首先我们可 ...
- 概率DP入门学习QAQ
emmmm博客很多都烂尾了...但是没空写..先写一下正在学的东西好了 概率DP这东西每次考到都不会..听题解也是一脸懵逼..所以决定学习一下这个东东..毕竟NOIP考过...比什么平衡树实在多了QA ...
- 任务调度框架kunka
kunka kunka是一个任务调度框架.用户只需要在Task接口中实现自己要执行的功能,并且选择合适的执行器,放入TaskManager中,就可以了完成整个任务了. 实现细节 整个任务信息存放在内存 ...