Gym102012 - 2018徐州 - G - Rikka with Intersections of Paths

        这题我一开始的思路是,对于点相交的集合,直接统计即可。对于边相交的集合。不妨算作为这条边深度最大的点的贡献。

尼玛,这明显有重啊,我是智障吗。。。所以正解是对于边相交的集合。算作为这条边深度最小的点的贡献。

等下,又坑: (x  - y) % mod是错误的,正确应写成 ( x-y+mod )%mod

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef int lint;
const lint maxn = 300005;
const lint maxm = 600005;
lint tot,he[maxn],ver[maxm],ne[maxm],sum1[maxn],sum2[maxn],val1[maxn],val2[maxn];
typedef unsigned long long ll;
const LL mod = 1e9+7;
LL frac[maxn], carf[maxn];
LL inv(LL x){
    if (x==1) return 1;
    else return (mod-mod/x)*inv(mod%x)%mod;
}
void init0(){
    frac[0]=1;
    for (lint i=1;i<=300000;i++)
        frac[i]=frac[i-1]*i%mod;
    carf[300000]=inv(frac[300000]);
    for (lint i=300000;i>=1;i--)
        carf[i-1]=carf[i]*i%mod;
}
LL C(lint n, lint m){
    if (m>n) return 0;
    else return frac[n]*carf[n-m]%mod*carf[m]%mod;
}

void init(lint n){
    for( lint i = 0;i <= n;i++ ){
        he[i] = 0;val1[i] = 0;val2[i] = 0;
    }
    tot = 1;
}
void add( lint x,lint y ){
    ver[++tot] = y;
    ne[tot]  = he[x];
    he[x] = tot;
}
queue<lint> que;
lint d[maxn],f[maxn][21];
void build( lint n ){
    while( que.size() )que.pop();
    for( lint i =0;i <= n;i++ ){
        d[i] = 0;
        memset( f[i],0,sizeof(f[i]) );
    }
    que.push(1);
    d[1] = 0;
    while( que.size() ){
        lint x = que.front();
        que.pop();
        for( lint cure = he[x];cure;cure = ne[cure] ){
            lint y = ver[cure];
            if( f[x][0] == y ) continue;
            que.push(y);
            d[y] = d[x] + 1;
            f[y][0] = x;
            for( lint i = 1;(1<<i) <= d[y];i++ ){
                f[y][i] = f[ f[y][i-1] ][i-1];
            }
        }
    }
}
lint lca( lint x,lint y ){
    if( d[x] < d[y] ) swap( x,y );
    for( lint i = 20;i >= 0;i-- ){
        if( f[x][i] && d[ f[x][i] ] >= d[y] ) x = f[x][i];
    }
    if( x == y ) return x;
    for( lint  i = 20;i >= 0;i-- ){
        if( f[x][i] != f[y][i] ){
            x = f[x][i];y = f[y][i];
        }
    }
    return  f[x][0];
}
void dfs1( lint x ){
    sum1[x] = val1[x];
    sum2[x] = val2[x];
    for( lint cure = he[x];cure;cure = ne[cure] ){
        lint y = ver[cure];
        if( y == f[x][0] ) continue;
        dfs1(y);
        sum1[x] += sum1[y];
        sum2[x] += sum2[y];
    }
}
LL ans = 0,k;
void dfs2( lint x ){
    ans  = (ans + C( sum1[x],k ))%mod;
    ans = (ans - C( sum2[x],k ) + mod ) % mod;
    for( lint cure = he[x];cure;cure = ne[cure] ){
        lint y = ver[cure];
        if( y == f[x][0] ) continue;
        dfs2(y);
    }
}
int main(){
    init0();
    lint n,m,T;
    scanf("%d",&T);
    while( T-- ){
        scanf("%d%d%d",&n,&m,&k);
        init(n); ans = 0;
        for( lint x,y,i = 1;i <= n-1;i++ ){
            scanf("%d%d",&x,&y);
            add( x,y );add( y,x );
        }
        build(n);
        for( lint x,y,i = 1;i <= m;i++ ){
            scanf("%d%d",&x,&y);
            val1[x]++;val1[y]++;
            lint z = lca(x,y);
            val1[z]--;
            val1[ f[z][0] ]--;
            val2[x]++;val2[y]++;
            val2[z] -= 2;
        }
        dfs1(1);
        dfs2(1);
        printf("%lld\n",ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值