QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#694658#9530. A Game On Treefrankly6WA 90ms16832kbC++172.3kb2024-10-31 18:25:182024-10-31 18:25:19

Judging History

This is the latest submission verdict.

  • [2024-10-31 18:25:19]
  • Judged
  • Verdict: WA
  • Time: 90ms
  • Memory: 16832kb
  • [2024-10-31 18:25:18]
  • Submitted

answer

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#define int long long
using namespace std;
typedef pair<int,int> PII;
const int MX=100020;
const int inf=1e18;
const int p=998244353;

int N, M, T;
int head[MX], cnt=1;
int siz[MX], sum[MX];//sum[u] = \sum_{v \in tree(u)} siz[v]*siz[v]
int v1[MX], v2[MX];  //v1[i] = e[i] 的贡献
                     //给边的有序对 <e_i,e_j> 按照 lca 分类, v2[u]=lca 为 u 的边对贡献
                     //ans = (sumv1+sumv2) / (N*(N-1)/2)^2
int read()
{
    int r=0, f=1; char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-') f=-1; ch=getchar();}
    while(ch>='0'&&ch<='9') {r=r*10+ch-'0'; ch=getchar();}
    return r*f;
}
struct edge{int nxt, to;}e[2*MX];
inline void ae(int u, int v){e[++cnt].to=v; e[cnt].nxt=head[u]; head[u]=cnt;}
int qpow(int x, int pow)
{
    int ans=1;
    while(pow)
    {
        if(pow&1) ans=ans*x%p;
        x=x*x%p;
        pow>>=1;
    }
    return ans;
}
int inv(int x){return qpow(x,p-2);}
int ord(int &x){return (x%p+p)%p;}
void dfs(int x, int f)
{
    siz[x]=1;
    for(int i=head[x],now=0;i;i=e[i].nxt)
    {
        int y=e[i].to;
        if(y==f) continue;
        dfs(y,x);
        siz[x]+=siz[y];
        sum[x]+=sum[y]; 
        v1[i/2]=siz[y]*(N-siz[y])%p*siz[y]%p*(N-siz[y])%p;
        v2[x]=v2[x]+2*(N-siz[y])*(N-siz[y])%p*(sum[y]-siz[y]*siz[y]%p)%p; //same subtree
        v2[x]=v2[x]+2*sum[y]*now%p; //dif subtree
        now=(now+sum[y])%p;
        ord(v2[x]);
        ord(v1[i/2]);
    }
    sum[x]=(sum[x]+siz[x]*siz[x]%p)%p;
}
signed main()
{
    // freopen("testdata.in","r",stdin);
    // int tmp=qpow(5*(5-1)%p*inv(2)%p,2);
    // for(int i=1;i<=200;i++)
    //     if(i*inv(tmp)%p==918384806) cout << "i=" << i << ", ans=" << i*inv(tmp)%p << '\n';
    T=read();
    while(T--)
    {
        N=read();
        for(int i=1;i<N;i++)
        {
            int u=read(), v=read();
            ae(u,v); ae(v,u);
        }
        dfs(1,0);
        int bel=qpow(N*(N-1)%p*inv(2)%p,2);
        int fac=0;
        for(int i=1;i<=N;i++)
            fac=(fac+v1[i]+v2[i])%p;
        ord(fac);
        cout << fac*inv(bel)%p << '\n';
        for(int i=1;i<=N;i++) head[i]=v1[i]=v2[i]=siz[i]=sum[i]=0; cnt=1;
    }
    return (0-0);
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 5756kb

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: 1ms
memory: 5700kb

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: 5ms
memory: 3616kb

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: 0
Accepted
time: 88ms
memory: 16472kb

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:

ok 10000 lines

Test #5:

score: 0
Accepted
time: 90ms
memory: 16260kb

input:

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

output:

711758412
286902823
691130166
841483019
650641410
887328317
331207619
733278261
56023919
977872021
414394648
183319566
239374924
696059768
855285904
761007271
711758412
86268032
599710576
728310932
178698065
178698065
422725927
219002589
178698065
202450068
599710576
56023919
449579681
760637552
925...

result:

ok 10000 lines

Test #6:

score: 0
Accepted
time: 83ms
memory: 16656kb

input:

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

output:

211048577
354315128
178698065
705549251
285212675
138645051
449579681
286902823
925779210
294297225
519087065
368179632
422725927
603876215
539175192
867301808
977540027
669439919
211048577
701572244
977872021
138645051
267677376
855285904
977872021
286902823
925286249
705549251
219002589
331207619
...

result:

ok 10000 lines

Test #7:

score: 0
Accepted
time: 77ms
memory: 15588kb

input:

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

output:

422725927
977872021
867301808
407446676
466833287
387074343
97051536
292325385
301691628
765628773
285212675
711758412
650641410
178698065
543242114
286902823
473241769
109930120
841975980
836553418
422725927
286902823
414394648
739440264
436731906
56023919
436731906
530918109
603876215
977872021
40...

result:

ok 10000 lines

Test #8:

score: -100
Wrong Answer
time: 88ms
memory: 16832kb

input:

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

output:

409773147
306621231
836553418
760637552
519087065
304649390
97051536
742521264
387074343
855285904
874737082
358875008
733278261
698524570
908525604
387074343
970145625
449579681
286902823
239374924
650641410
691130166
765628773
603876215
839572800
977872021
742521264
908032643
874737082
299719788
7...

result:

wrong answer 9999th lines differ - expected: '608175344', found: '-390069009'