QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#381882#8570. Idola-Treeucup-team004#WA 1813ms41416kbC++202.4kb2024-04-07 21:12:232024-04-07 21:12:24

Judging History

你现在查看的是最新测评结果

  • [2024-04-07 21:12:24]
  • 评测
  • 测评结果:WA
  • 用时:1813ms
  • 内存:41416kb
  • [2024-04-07 21:12:23]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;

constexpr int P = 998244353;

constexpr int N = 1 << 19;

int que[N];

void solve() {
    int n, C;
    std::cin >> n >> C;

    C -= n - 1;

    std::vector<std::vector<int>> adj(n);
    for (int i = 1; i < n; i++) {
        int u, v;
        std::cin >> u >> v;
        u--, v--;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    std::vector<std::array<i64, 3>> dp(n);
    auto dfs = [&](auto self, int x, int p = -1) -> void {
        dp[x][0] = 1;
        for (auto y : adj[x]) {
            if (y == p) {
                continue;
            }
            self(self, y, x);
            dp[x][0] += dp[y][0];
            dp[x][1] += dp[y][1] + dp[y][0];
            dp[x][2] = (dp[x][2] + dp[y][2] + 2 * dp[y][1] + dp[y][0]) % P;
        }
    };
    dfs(dfs, 0);

    auto dfs1 = [&](auto self, int x, int p = -1) -> void {
        for (auto y : adj[x]) {
            if (y == p) {
                continue;
            }
            i64 v0 = dp[x][0] - dp[y][0];
            i64 v1 = dp[x][1] - dp[y][1] - dp[y][0];
            i64 v2 = ((dp[x][2] - dp[y][2] - 2 * dp[y][1] - dp[y][0]) % P + P) % P;
            dp[y][0] += v0;
            dp[y][1] += v1 + v0;
            dp[y][2] = (dp[y][2] + v2 + 2 * v1 + v0) % P;
            self(self, y, x);
        }
    };
    dfs1(dfs1, 0);

    int res = 0;
    for (int x = 0; x < n; x++) {
        res = (res + dp[x][2]) % P;
    }
    res = 1LL * res * (P + 1) / 2 % P;
    std::vector<i64> a;
    for (int x = 0; x < n; x++) {
        if (adj[x].size() == 1) {
            a.push_back(2 * dp[x][1] + (n - 2));
        }
    }

    int ans = 0;
    std::sort(a.begin(), a.end());
    int i = 0;
    int l = 0, r = 0;
    for (int c = 0; c <= C; c++) {
        int v = (res + 1LL * c * c) % P;
        ans = (ans + 1LL * v * v % P * v) % P;
        i64 x;
        if (l != r && (i == a.size() || que[l] < a[i])) {
            x = que[l++];
            if (l == N) {
                l = 0;
            }
        } else {
            x = a[i++];
        }
        res = (res + x) % P;
        que[r++] = x + 2 * (n - 2);
        if (r == N) {
            r = 0;
        }
    }
    std::cout << ans << "\n";
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int t;
    std::cin >> t;

    while (t--) {
        solve();
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

3375
25327

result:

ok 2 tokens

Test #2:

score: 0
Accepted
time: 0ms
memory: 3632kb

input:

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

output:

3375
25327
54872
249984

result:

ok 4 tokens

Test #3:

score: 0
Accepted
time: 1753ms
memory: 41416kb

input:

4
300000 50000000
216838 200677
44849 12926
125086 157230
26195 29767
241694 21336
21336 24457
105861 84565
184655 45583
175336 97945
286044 30927
295273 249694
109469 1566
193560 251229
176229 288707
206166 13532
166218 264413
299977 95587
159105 48116
57912 82606
97296 193689
115029 121192
68068 1...

output:

968050473
546482457
9595680
694101802

result:

ok 4 tokens

Test #4:

score: -100
Wrong Answer
time: 1813ms
memory: 38848kb

input:

4
300000 49999999
260729 131757
51432 46938
257303 178692
218606 108144
299084 259822
82091 231405
101255 218808
287507 101249
29597 151244
43164 198032
122336 162072
177969 62812
277824 35758
182087 258797
235155 33806
188695 76915
289006 141702
39100 183183
224949 156588
9827 173233
27349 286822
1...

output:

283884918
594098158
12901428
951761296

result:

wrong answer 2nd words differ - expected: '837489229', found: '594098158'