QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#683325#9530. A Game On TreeLogingWA 719ms13384kbC++144.3kb2024-10-27 20:13:272024-10-27 20:13:35

Judging History

This is the latest submission verdict.

  • [2024-10-27 20:13:35]
  • Judged
  • Verdict: WA
  • Time: 719ms
  • Memory: 13384kb
  • [2024-10-27 20:13:27]
  • Submitted

answer

#include<cstdio>
#include<algorithm>
#include<cstring>
#define M 100005
#define P 998244353
using namespace std;
long long Mul(long long a,int b){
    long long res=1;
    while(b){
        if(b&1)res=1ll*res*a%P;
        a=1ll*a*a%P;
        b>>=1;
    }
    return res;
}
int n;
struct E{
    int to,nx;
}edge[M<<1];
int tot,head[M];
void Addedge(int a,int b){
    edge[++tot].to=b;
    edge[tot].nx=head[a];
    head[a]=tot;
}
int Root,tot_sz;
int sz[M],mx_sz[M];
bool mark[M];
void FindRoot(int now,int fa){
    sz[now]=1;mx_sz[now]=0;
    for(int i=head[now];i;i=edge[i].nx){
        int nxt=edge[i].to;
        if(nxt==fa||mark[nxt])continue;
        FindRoot(nxt,now);
        sz[now]+=sz[nxt];
        mx_sz[now]=max(mx_sz[now],sz[nxt]);
    }
    mx_sz[now]=max(mx_sz[now],tot_sz-sz[now]);
    if(mx_sz[now]<mx_sz[Root]||!Root)Root=now;
}
long long Res[2][5];
long long val[M];
void Add(long long &x,long long y){
    x=(x+y)%P;
}
long long ans;
void AddPoint(int now,long long add){
    Add(Res[1][0],1ll*now*now%P*add%P);
    Add(Res[1][1],2ll*now%P*add%P);
    Add(Res[1][2],1ll*add%P);
}
void Calc(int now,long long add){
    Add(ans,1ll*Res[0][0]*add%P);
    Add(ans,1ll*Res[0][1]*now%P*add%P);
    Add(ans,1ll*Res[0][2]*now%P*now%P*add%P);
}
int SZ[M],dep[M];
long long VAL[M];
long long VAL2[M];
void redfs(int now,int fa){
    SZ[now]=1;
    VAL[now]=n;VAL2[now]=1;
    for(int i=head[now];i;i=edge[i].nx){
        int nxt=edge[i].to;
        if(nxt==fa)continue;
        dep[nxt]=dep[now]+1;
        redfs(nxt,now);
        SZ[now]+=SZ[nxt];
        Add(VAL[now],1ll*SZ[nxt]*(n-SZ[nxt])%P);
        Add(VAL2[now],1ll*SZ[nxt]*(n-SZ[nxt])%P);
    }
    Add(VAL[now],1ll*SZ[now]*(n-SZ[now])%P);
    Add(VAL2[now],1ll*SZ[now]*(n-SZ[now])%P);
//    printf("VAL[%d]=%lld\n",now,VAL[now]);
}
void dfs2(int now,int fa,int dis){
    for(int i=head[now];i;i=edge[i].nx){
        int nxt=edge[i].to;
        if(nxt==fa||mark[nxt])continue;
        dfs2(nxt,now,dis+1);
    }
    if(dep[fa]+1==dep[now]){
		val[now]=(VAL[now]-2ll*SZ[now]*(n-SZ[now]))%P;
	}else{
		val[now]=(VAL[now]-2ll*SZ[fa]*(n-SZ[fa]))%P;
//		printf("%d(%d) %d(%d)\n",fa,dep[fa],now,dep[now]);
//		printf("%lld %lld\n",VAL[now],2ll*SZ[fa]*(n-SZ[fa]));
	}
    Calc(dis,val[now]);
    AddPoint(dis,val[now]);
//    printf("val[%d]=%lld dis=%d\n",now,val[now],dis);
//    printf("ans=%lld\n",ans);
}
void dfs(int now){
    memset(Res,0,sizeof(Res));
//    printf("Split:now=%d\n",now);
    mark[now]=true;
    val[now]=1;
    // AddPoint(0,val[now]);

//    printf("val[%d]=%lld\n",now,val[now]);
    for(int i=head[now];i;i=edge[i].nx){
        int nxt=edge[i].to;
        if(mark[nxt])continue;
        long long tmp=0;
        if(dep[now]+1==dep[nxt]){
        	tmp=(VAL[now]-2ll*SZ[nxt]*(n-SZ[nxt]))%P;
		}else{
			tmp=(VAL[now]-2ll*SZ[now]*(n-SZ[now]))%P;
		}
//        printf("nxt=%d tmp=%lld\n",nxt,tmp);
//        printf("sznow=%d nxt=%d\n",sz[now],sz[nxt]);
        AddPoint(0,tmp);
        for(int j=0;j<3;j++)Res[0][j]=Res[1][j];
        dfs2(nxt,now,1);
        // Add(val[now],2ll*sz[now]*sz[nxt]%P);
        // AddPoint(0,2ll*sz[now]*sz[nxt]%P);
        // sz[now]+=sz[nxt];
        AddPoint(0,-tmp);
        for(int j=0;j<3;j++)Res[0][j]=Res[1][j];
    }
    // Calc(0,val[now]);
    for(int i=0;i<3;i++)Res[0][i]=Res[1][i];
//    printf("ans=%lld\n",ans); 

    int tmp_sz=tot_sz;
    for(int i=head[now];i;i=edge[i].nx){
        int nxt=edge[i].to;
        if(mark[nxt])continue;
        Root=0;tot_sz=sz[nxt]>sz[now]?tmp_sz-sz[now]:sz[nxt];
        FindRoot(nxt,0);
        dfs(Root);
    }
}
int main(){
//	printf("%lld\n",918384806ll*100%P);
    int T;
    scanf("%d",&T);
    while(T--){
        ans=0;
        memset(Res,0,sizeof(Res));
        scanf("%d",&n);
        for(int i=1;i<=n;i++)head[i]=mark[i]=0;
        tot=0;
        for(int i=1;i<n;i++){
            int a,b;
            scanf("%d%d",&a,&b);
            Addedge(a,b);Addedge(b,a);
        }
        redfs(1,0);
        Root=0;tot_sz=n;
        FindRoot(1,0);
        dfs(Root);
        long long w=Mul(1ll*n*(n-1)/2,P-2);
//        w=1;
        ans=ans*w%P*w%P;
        ans=(ans+P)%P;
        printf("%lld\n",ans);
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3664kb

input:

2
3
1 2
2 3
5
1 2
1 5
3 2
4 2

output:

443664158
918384806

result:

ok 2 lines

Test #2:

score: 0
Accepted
time: 2ms
memory: 5728kb

input:

1000
7
3 6
4 3
5 3
2 6
1 4
7 1
12
5 7
10 7
2 10
11 2
1 7
8 1
4 2
9 11
6 9
12 11
3 5
6
2 5
1 2
4 5
6 4
3 6
5
2 5
1 5
4 5
3 2
8
1 8
2 8
4 2
6 1
5 6
7 6
3 8
8
3 8
7 3
4 8
6 4
2 7
5 2
1 4
4
3 1
4 3
2 1
6
5 1
6 1
2 5
3 5
4 2
12
8 11
5 11
12 8
3 12
6 12
2 3
4 6
10 11
1 5
9 5
7 5
9
6 1
7 6
4 7
8 7
5 4
9 6
...

output:

948445317
468414020
550143557
918384806
711758412
487662742
776412276
869581749
240852807
765628773
211048577
887328316
890334966
940494682
760637552
908032643
592850815
584006902
908525604
221832080
433351719
56023919
867301808
183319566
698771049
366957926
449579681
599710576
310564911
286902823
3...

result:

ok 1000 lines

Test #3:

score: 0
Accepted
time: 25ms
memory: 5748kb

input:

1000
94
59 1
33 59
73 1
6 33
83 59
4 59
20 59
61 6
39 1
76 73
71 6
44 39
9 71
24 4
87 9
57 83
2 9
81 71
82 20
90 2
85 39
12 9
30 83
66 30
53 9
47 9
36 44
43 53
29 12
31 53
64 81
38 31
84 82
77 38
23 71
93 84
78 83
58 31
68 90
42 1
55 64
13 78
70 78
62 24
19 55
92 87
14 57
10 84
65 81
63 6
75 36
91 1...

output:

508107725
996793960
201633249
335988372
842755864
460619380
342223697
207523414
429241811
391691799
542977964
786416604
454278948
685531402
25914978
440729774
228518323
679471537
82764520
554190841
432505337
143444089
189106586
337234245
61954935
905141094
532919674
703954588
185671863
942858630
692...

result:

ok 1000 lines

Test #4:

score: -100
Wrong Answer
time: 719ms
memory: 13384kb

input:

10000
8
1 4
3 1
5 1
7 3
8 4
6 8
2 7
8
2 6
4 6
5 6
8 5
7 6
3 5
1 7
8
8 5
6 5
2 5
7 2
1 6
3 1
4 8
9
8 6
9 8
3 6
1 8
5 9
2 8
4 3
7 9
8
8 6
3 6
5 8
1 6
4 3
7 6
2 6
9
9 5
7 5
2 7
8 7
4 9
3 7
6 3
1 4
8
1 4
5 1
6 5
3 4
8 4
7 8
2 5
9
1 8
6 1
2 1
3 8
5 3
9 8
7 8
4 8
9
4 9
2 9
1 2
3 4
5 2
6 9
8 3
7 2
8
1 2
8 ...

output:

49657566
56023919
387074343
97051536
701572244
211048577
711758412
308100110
761007271
711758412
178698065
285212675
80216065
43380497
267677376
818005792
53239701
765628773
970145625
387074343
436731906
422725927
479157293
977872021
436731906
925779210
487662742
705549251
267677376
711758412
526851...

result:

wrong answer 9992nd lines differ - expected: '391800050', found: '459830979'