QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#816308#9530. A Game On Treehxu10TL 135ms11044kbPython32.0kb2024-12-16 09:13:512024-12-16 09:13:51

Judging History

This is the latest submission verdict.

  • [2024-12-16 09:13:51]
  • Judged
  • Verdict: TL
  • Time: 135ms
  • Memory: 11044kb
  • [2024-12-16 09:13:51]
  • Submitted

answer

import io,os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
from collections import deque 



M = 998244353

def fastfrac(a,b):
    numb = pow(b,M-2,M)
    return ((a%M)*(numb%M))%M




def main():
    n = int(input())

    neigh = [[] for _ in range(n)]
    for _ in range(n-1):
        u,v = map(int,input().split())
        neigh[u-1].append(v-1)
        neigh[v-1].append(u-1)

    children = [[] for _ in range(n)]


    parent = [-2]*n
    subnum = [0]*n

    subnum2 = [0]*n
    layer = [0]*n


    parent[0] = -1



    queue = deque()

    queue.append(0)

    seq = []
    while queue:
        index = queue.popleft()
        seq.append(index)
        for nextindex in neigh[index]:
            if parent[nextindex] > -2:  continue 
            parent[nextindex] = index
            children[index].append(nextindex)
            queue.append(nextindex)
            layer[nextindex] = layer[index] + 1






    for index in seq[::-1]:
        subnum[index] = 1
        for nextindex in children[index]:
            subnum[index] += subnum[nextindex]
            subnum2[index] += subnum2[nextindex]
 
        subnum2[index] += subnum[index] ** 2
     


    
    
    base = 0
    for i in range(n):
        base += subnum[i] ** 2

    
    diff = [0]*n

#    print(subnum)

    ans1 = 0
    ans2 = 0
    for index in seq[1:]:
        p = parent[index]
        diff[index] = diff[p] + (n - subnum[index])**2  - subnum[p] ** 2
        
        end = base - subnum2[index] - subnum[p]**2 + diff[p]
        ans1 += (subnum[index] * (n - subnum[index]) % M) ** 2 % M
        ans2 +=  (subnum[index] ** 2 % M) * end % M
        ans2 += (subnum2[index] - subnum[index] ** 2) * (n - subnum[index]) ** 2 % M
        ans1 %= M
        ans2 %= M


#    print("ans12",ans1,ans2)    
       
    div = (n * (n-1) // 2) ** 2 % M



    ans = (ans1 + ans2)%M
    ans = fastfrac(ans, div)


     

   


    

        
    print(ans)
        
    






T = int(input())
t = 1
while t <= T:
    main()
    t += 1

详细

Test #1:

score: 100
Accepted
time: 13ms
memory: 10476kb

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: 28ms
memory: 10608kb

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: 135ms
memory: 11044kb

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
Time Limit Exceeded

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: