QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#401697#7509. 01treeGraphcityTL 49ms8508kbC++201.6kb2024-04-29 09:43:202024-04-29 09:43:22

Judging History

你现在查看的是最新测评结果

  • [2024-04-29 09:43:22]
  • 评测
  • 测评结果:TL
  • 用时:49ms
  • 内存:8508kb
  • [2024-04-29 09:43:20]
  • 提交

answer

#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rof(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
const int Maxn=2e5,Mod=1e9+7;

inline int Pow(int x,int y)
{
    int res=1;
    while(y)
    {
        if(y&1) res=1ll*res*x%Mod;
        x=1ll*x*x%Mod,y>>=1;
    }
    return res;
}

int n,ans,fac[Maxn+5],inv[Maxn+5]; char s[Maxn+5],t[Maxn+5];
int A1[Maxn+5],B1[Maxn+5],C1[Maxn+5];
inline int C(int x,int y) {return (x<y || y<0)?0:1ll*fac[x]*inv[x-y]%Mod*inv[y]%Mod;}
vector<int> v[Maxn+5];

inline void dfs(int x,int f,int d)
{
    int sx=(s[x]=='?'?2:s[x]-'0'),tx=(t[x]=='?'?2:t[x]-'0');
    if(d&1) {if(sx<=1) sx^=1; if(tx<=1) tx^=1;}
    A1[x]=(sx==0)-(tx==0),B1[x]=(sx==2),C1[x]=(tx==2);
    for(auto y:v[x]) if(y!=f) dfs(y,x,d+1),A1[x]+=A1[y],B1[x]+=B1[y],C1[x]+=C1[y];
}
inline void Solve()
{
    cin>>n; ans=0; For(i,1,n-1)
    {
        int a,b; cin>>a>>b;
        v[a].push_back(b),v[b].push_back(a);
    } scanf("%s%s",s+1,t+1); dfs(1,0,0);
    For(i,2,n)
    {
        int a=A1[i],b=B1[i],c=C1[i],d=A1[1]-A1[i],e=B1[1]-B1[i],f=C1[1]-C1[i];
        // cerr<<i<<' '<<a<<' '<<b<<' '<<c<<' '<<d<<' '<<e<<' '<<f<<endl;
        For(k,-n,n) For(p,0,b) For(q,0,e)
            ans=(ans+1ll*C(b,p)*C(c,a+p-k)%Mod*C(e,q)%Mod*C(f,d+q+k)%Mod*abs(k))%Mod;
    } cout<<ans<<endl; For(i,1,n) vector<int>().swap(v[i]);
}

int main()
{
    // freopen("1.in","r",stdin);

    int T; cin>>T; fac[0]=inv[0]=1;
    For(i,1,Maxn) fac[i]=1ll*fac[i-1]*i%Mod;
    inv[Maxn]=Pow(fac[Maxn],Mod-2)%Mod;
    Rof(i,Maxn-1,1) inv[i]=1ll*inv[i+1]*(i+1)%Mod;
    while(T--) Solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 8036kb

input:

3
2
1 2
00
11
3
1 2
2 3
???
???
3
1 2
2 3
??1
0?0

output:

1
16
1

result:

ok 3 number(s): "1 16 1"

Test #2:

score: 0
Accepted
time: 49ms
memory: 8508kb

input:

1000
23
1 2
1 3
1 4
2 5
5 6
4 7
3 8
4 9
8 10
8 11
8 12
1 13
7 14
10 15
7 16
7 17
5 18
18 19
12 20
9 21
21 22
6 23
00?10?0000??1?00111?010
011??1?10?01?110?0??101
6
1 2
1 3
1 4
4 5
3 6
000?10
1???01
25
1 2
2 3
2 4
4 5
5 6
2 7
4 8
5 9
7 10
8 11
11 12
5 13
11 14
3 15
6 16
14 17
1 18
4 19
6 20
4 21
5 22...

output:

53545
24
11724
2228
162
14
711
28
550
1680
52
2
13
988
9480
2342
626416
0
71780
1
88
39207
19448
4
37395
9602
3253496
38057200
1066
3
303732
1608
281022
11718
170
78
15
1219376
29364
9092
7
0
2
7014
1942448
170371
75845
167217
16554
64
904
564290
14822
1127090
1758504
1227646
14833316
14954550
36055...

result:

ok 1000 numbers

Test #3:

score: -100
Time Limit Exceeded

input:

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

output:


result: