QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#697985#9530. A Game On TreeEmertystWA 3ms6336kbC++141.5kb2024-11-01 16:43:322024-11-01 16:43:32

Judging History

This is the latest submission verdict.

  • [2024-11-01 16:43:32]
  • Judged
  • Verdict: WA
  • Time: 3ms
  • Memory: 6336kb
  • [2024-11-01 16:43:32]
  • Submitted

answer

#include <iostream>
#include <vector>
using namespace std;
const int N = 1e5 + 5, MOD = 998244353;
int inc(int a, int b) { return a + b >= MOD ? a + b - MOD : a + b; }
int dec(int a, int b) { return a < b ? a - b + MOD : a - b; }
int mul(int a, int b) { return 1ll * a * b % MOD; }
int power(int a, int b) {
    int ans = 1;
    for (; b; b >>= 1, a = mul(a, a))
        if (b & 1)
            ans = mul(ans, a);
    return ans;
}
int inv(int a) { return power(a, MOD - 2); }
int t, n, ans, siz[N], sum[N];
vector<int> edg[N];
void dfs(int x, int f) {
    siz[x] = 1, sum[x] = 0;
    for (int e : edg[x])
        if (e != f)
            dfs(e, x), siz[x] += siz[e], sum[x] = inc(sum[x], sum[e]);
    int cnt = 0;
    for (int e : edg[x])
        if (e != f) {
            ans = inc(ans, mul(mul(siz[e], siz[e]), mul(n - siz[x], n - siz[x])));
            ans = inc(ans, mul(mul(n - siz[e], n - siz[e]), sum[e]));
            ans = inc(ans, mul(2, mul(cnt, sum[e]))), cnt = inc(cnt, sum[e]);
        }
    sum[x] = inc(sum[x], mul(siz[x], siz[x]));
}
int main() {
    ios::sync_with_stdio(false);
    for (cin >> t; t; --t) {
        cin >> n, ans = 0;
        for (int i = 1; i <= n; ++i)
            edg[i].clear();
        for (int i = 1, u, v; i < n; ++i)
            cin >> u >> v, edg[u].push_back(v), edg[v].push_back(u);
        dfs(1, 0);
        cout << mul(ans, inv(mul(1ll * n * (n - 1) / 2 % MOD, 1ll * n * (n - 1) / 2 % MOD))) << "\n";
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: -100
Wrong Answer
time: 3ms
memory: 6088kb

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:

405183084
48353896
465847367
89841993
19099065
106954755
776412276
838525258
46062242
44674518
242628838
986690601
643166364
907724034
472748810
783806679
246352775
584006902
654651114
394368141
622413149
82762607
248020589
743291514
229596203
208558822
441199358
86582420
310564911
9859205
713251754...

result:

wrong answer 1st lines differ - expected: '948445317', found: '405183084'