QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#716920#7509. 01treefzxTL 25ms46760kbC++142.9kb2024-11-06 16:22:402024-11-06 16:22:41

Judging History

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

  • [2024-11-06 16:22:41]
  • 评测
  • 测评结果:TL
  • 用时:25ms
  • 内存:46760kb
  • [2024-11-06 16:22:40]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long 
#define pb push_back
using namespace std;
const int Mod=1e9+7;
const int INF=1e6+5;
int n,col[INF],fav[INF],ifav[INF];string s,t;
vector <int> e[INF];
int ksm(int x,int y) {
    int ba=x%Mod,ans=1;
    while (y) {
        if (y&1) ans=(ans*ba)%Mod;
        ba=(ba*ba)%Mod;y>>=1;
    }
    return ans;
}
void init() {
    fav[0]=1;int N=1e6;
    for (int i=1;i<=N;i++) fav[i]=fav[i-1]*i%Mod;
    ifav[N]=ksm(fav[N],Mod-2);
    for (int i=N-1;~i;i--) ifav[i]=ifav[i+1]*(i+1)%Mod;
}
int C(int x,int y) {
    if (x<y) return 0;
    return fav[x]*ifav[y]%Mod*ifav[x-y]%Mod;
}
void DFS(int x,int fa) {
    for (int v:e[x]) {
        if (v==fa) continue;
        col[v]=col[x]^1;
        DFS(v,x);
    }
}
int A[5],B[5];
int sz1[INF][5],sz2[INF][5],res;
void DFS2(int x,int fa) {
    for (int w=0;w<3;w++) sz1[x][w]=sz2[x][w]=0;
    if (s[x]=='?') sz1[x][0]++;
    else if (s[x]=='0') sz1[x][1]++;
    else sz1[x][2]++;

    if (t[x]=='?') sz2[x][0]++;
    else if (t[x]=='0') sz2[x][1]++;
    else sz2[x][2]++;
    // cerr<<x<<" "<<sz1[x][0]<<" "<<sz2[x][0]<<" qoijrt\n";
    for (int v:e[x]) {
        if (v==fa) continue;
        DFS2(v,x);
        
        for (int A1=0;A1<=sz1[v][0];A1++) {
            for (int B1=0;B1<=sz2[v][0];B1++) {
                for (int A11=0;A11<=A[0]-sz1[v][0];A11++) {
                    for (int B11=0;B11<=B[0]-sz2[v][0];B11++) {
                        // 0 的个数是 A11+A1+A[1] =  B11+B1+B[1]
                        // cerr<<sz1[v][0]<<" "<<sz2[v][0]<<" \n";
                        if (A11+A1+A[1] ==  B11+B1+B[1]) {
                            res+=C(sz1[v][0],A1)*C(sz2[v][0],B1)%Mod
                                *C(A[0]-sz1[v][0],A11)%Mod*C(B[0]-sz2[v][0],B11)%Mod*abs(A1-B1+sz1[v][1]-sz2[v][1]);
                            res%=Mod;
                        }
                    }
                }
            }
        }

        for (int i=0;i<3;i++)
            sz1[x][i]+=sz1[v][i],
            sz2[x][i]+=sz2[v][i];
    }
}
void solve() {
    for (int w=0;w<=n+3;w++) e[w].clear();
    cin>>n;res=0;
    for (int i=1;i<n;i++) {
        int x=0,y=0;
        cin>>x>>y;
        e[x].pb(y);
        e[y].pb(x);
    }
    for (int w=0;w<3;w++) A[w]=B[w]=0;
    DFS(1,0);cin>>s>>t;s=" "+s;t=" "+t;
    for (int i=1;i<=n;i++) {
        if (s[i]=='?') {A[0]++;continue;}
        if (col[i]==1) {
            if (s[i]=='0') s[i]='1';
            else s[i]='0';
        }
        if (s[i]=='0') A[1]++;
        else A[2]++;
    }
    for (int i=1;i<=n;i++) {
        if (t[i]=='?') {B[0]++;continue;}
        if (col[i]==1) {
            if (t[i]=='0') t[i]='1';
            else t[i]='0';
        }
        if (t[i]=='0') B[1]++;
        else B[2]++;
    }
    DFS2(1,0);
    cout<<res<<"\n";
}
signed main()
{
    ios::sync_with_stdio(false);
    int t=0;cin>>t;init();
    while (t--) solve();
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 19ms
memory: 46760kb

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: 25ms
memory: 46544kb

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: