QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#710553#9530. A Game On TreelvliangWA 120ms7348kbC++142.0kb2024-11-04 20:25:272024-11-04 20:25:29

Judging History

This is the latest submission verdict.

  • [2024-11-04 20:25:29]
  • Judged
  • Verdict: WA
  • Time: 120ms
  • Memory: 7348kb
  • [2024-11-04 20:25:27]
  • Submitted

answer

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
const int mod = 998244353;
typedef long long LL;

int n;
int h[N], e[N << 1], ne[N << 1], idx;
LL si[N], sum[N];
LL ans;

void add(int a, int b) {
    e[++idx] = b, ne[idx] = h[a], h[a] = idx;
}

LL dfs(int u, int father) {
    si[u] = 1;
    sum[u] = 0;
    LL pre_sum = 0;
    LL tmppre_sum = 0;
    for (int i = h[u]; i; i = ne[i]) {
        int j = e[i];
        if (j == father) continue;
        LL x = dfs(j, u);
        tmppre_sum = (tmppre_sum + pre_sum * sum[j]) % mod;
        pre_sum = (pre_sum + sum[j]) % mod; 
        
        LL tmp1 = x * x % mod;
        LL tmp2 = (n - x) * (n - x) % mod;
        ans = (ans + tmp1 * tmp2 % mod) % mod;
        ans = (ans + tmp2 * (sum[j] - tmp1) % mod * 2) % mod;
        
        si[u] += x;
        sum[u] = (sum[j] + sum[u]) % mod;
    }
    ans = (ans + tmppre_sum * 2) % mod;
    sum[u] = (sum[u] + si[u] * si[u]) % mod;
    return si[u];
}

LL quick_pow(LL base, LL power, LL mod) {
    LL ans = 1;
    while (power) {
        if (power & 1) ans = base * ans % mod;
        base = base * base % mod;
        power >>= 1;
    }
    return ans;
}

void solve(bool flag, int tt) {
    ans = 0, idx = 0;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        sum[i] = si[i] = h[i] = 0;
    }
    if (flag && tt == 9992) cout << n << " " << endl;
    for (int i = 1; i < n; i++) {
        int u, v;
        if (flag && tt == 9992) cout << u << " " << v << endl;
        scanf("%d%d", &u, &v);
        add(u, v), add(v, u);
    }
    if (flag) return;
    dfs(1, -1);
    LL y = (LL)(n * (n - 1) / 2) % mod;
    LL x = quick_pow(y * y % mod, mod - 2 , mod);
    x = (x + mod) % mod;
    printf("%lld\n", ans * x % mod);
}

int main() {
    int T;
    scanf("%d", &T);
    int i = 0;
    bool flag = false;
    if (T == 10000) flag = true;
    while (T--) {
        i++;
        solve(flag, i);
    }
    return 0;
}

詳細信息

Test #1:

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

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: 3984kb

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

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: 120ms
memory: 7348kb

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:

100000 
8 5
87726 46455
32539 46455
80955 32539
42819 46455
90659 46455
30478 46455
99385 46455
29408 46455
28173 87726
15605 28173
21192 80955
42180 28173
55974 32539
16056 29408
66213 21192
53970 21192
10224 55974
58022 32539
63584 58022
86976 10224
61166 30478
41629 42819
14013 29408
47107 42180
...

result:

wrong answer 1st lines differ - expected: '49657566', found: '100000 '