QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#625438#7009. Rikka with Intersections of Pathsucup-team5071#AC ✓3969ms79620kbC++202.5kb2024-10-09 19:19:502024-10-09 19:19:50

Judging History

This is the latest submission verdict.

  • [2024-10-09 19:19:50]
  • Judged
  • Verdict: AC
  • Time: 3969ms
  • Memory: 79620kb
  • [2024-10-09 19:19:50]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
const int maxn=3e5+10;
int fac[maxn],inv[maxn];
typedef long long ll;
void add(int &x,int y){
    if((x+=y)>=mod)x-=mod;
}
void del(int &x,int y){
    if((x-=y)<0)x+=mod;
}
int ksm(int x,int k){
    int res=1;
    while(k){
        if(k&1)res=(ll)res*x%mod;
        x=(ll)x*x%mod;
        k/=2;
    }
    return res;
}
int ny(int x){
    return ksm(x,mod-2);
}
void init()
{
    fac[0]=1;
    for(int i=1;i<maxn;i++)fac[i]=(ll)fac[i-1]*i%mod;
    inv[maxn-1]=ny(fac[maxn-1]);
    for(int i=maxn-2;i>=0;i--)inv[i]=(ll)inv[i+1]*(i+1)%mod;
}
int C(int n,int m){
    if(n<m)return 0;
    if(n==m)return 1;
    return (ll)fac[n]*inv[n-m]%mod*inv[m]%mod;
}
int solve()
{
    int n,m,k;cin>>n>>m>>k;
    vector<vector<int>> ve(n+1);
    for(int i=1;i<n;i++){
        int x,y;cin>>x>>y;
        ve[x].push_back(y);
        ve[y].push_back(x);
    }
    vector<int>dep(n+1);
    vector<vector<int>> f(21,vector<int>(n+1));
    function<void(int,int)> dfs1 = [&](int x,int h){
        dep[x]=dep[h]+1;
        for(int i=0;i<=19;i++)
            f[i+1][x]=f[i][f[i][x]];
        for(auto it:ve[x])if(it!=h){
            f[0][it]=x;
            dfs1(it,x);
        }
    };
    dfs1(1,0);
    int ans=0;
    auto lca = [&](int x,int y){
        if(dep[x]<dep[y])swap(x,y);
        for(int i=20;i>=0;i--){
            if(dep[f[i][x]]>=dep[y])x=f[i][x];
            if(x==y)return x;
        }
        for(int i=20;i>=0;i--)
            if(f[i][x]!=f[i][y])
                x=f[i][x],y=f[i][y];
        return f[0][x];
    };
    vector<int> addp(n+1),adde(n+1);
    vector<int> valp(n+1),vale(n+1);
    while(m--){
        int x,y;cin>>x>>y;
        int l=lca(x,y);
        {
            valp[l]++;
            addp[l]-=2;
            addp[x]++;addp[y]++;
        }
        {
            adde[l]-=2;
            adde[x]++;adde[y]++;
        }
    }
    function<void(int,int)> dfs = [&](int x,int h){
        for(auto it:ve[x])if(it!=h){
            dfs(it,x);
            addp[x]+=addp[it];
            adde[x]+=adde[it];
        }
        valp[x]+=addp[x];
        vale[x]+=adde[x];
        add(ans,C(valp[x],k));
        if(x!=1)del(ans,C(vale[x],k));
    };
    dfs(1,0);
    return ans;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;cin>>T;
    init();
    while(T--)cout<<solve()<<"\n";
}

/*
1
3 6 2
1 2
1 3
1 1
2 2
3 3
1 2
1 3
2 3

*/

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 6156kb

input:

1
3 6 2
1 2
1 3
1 1
2 2
3 3
1 2
1 3
2 3

output:

10

result:

ok single line: '10'

Test #2:

score: 0
Accepted
time: 3679ms
memory: 56220kb

input:

108
2000 2000 52
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
...

output:

887021006
410694359
325103243
670233279
72813801
947351730
883070706
311794222
998954559
996232156
569968667
505502006
778426774
220584560
246359125
260714580
11417533
351222718
490813635
444958907
207271238
791034394
734465853
472937949
826448869
646757384
776063725
826971663
959125943
459469914
30...

result:

ok 108 lines

Test #3:

score: 0
Accepted
time: 3969ms
memory: 79620kb

input:

6
300000 300000 43
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 5...

output:

690220121
895677607
370155943
510259168
589689421
548994023

result:

ok 6 lines