QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#436114#7895. Graph Partitioning 2HiraethsoulTL 0ms9080kbC++201.3kb2024-06-08 23:17:452024-06-08 23:17:45

Judging History

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

  • [2024-06-08 23:17:45]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:9080kb
  • [2024-06-08 23:17:45]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long
const int p = 998244353;
const int N = 1e5 + 9;
std::unordered_map<int, int> dp[N];
void solve()
{
    int n, k;
    std::cin >> n >> k;
    std::vector<std::vector<int>> g(n + 1);
    for (int i = 1; i <= n; ++i)
    {
        dp[i].clear();
    }
    for (int i = 1; i < n; ++i)
    {
        int u, v;
        std::cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    auto dfs = [&](auto dfs, int u, int fa) -> void
    {
        dp[u][1] = 1;
        for (auto v : g[u])
        {
            if (v == fa)
            {
                continue;
            }
            dfs(dfs, v, u); // dp
            std::unordered_map<int, int> tmp;
            for (auto [i, val1] : dp[u])
            {
                for (auto [j, val2] : dp[v])
                {
                    tmp[i + j] = (tmp[i + j] + val1 * val2 % p) % p;
                }
            }
            dp[u].swap(tmp);
        }
        dp[u][0] = dp[u][k] + dp[u][k + 1];
        dp[u].erase(k + 1);
        dp[u][0] %= p;
    };
    dfs(dfs, 1, 0);
    std::cout << dp[1][0] << '\n';
}

signed main()
{
    std::ios::sync_with_stdio(0);
    std::cin.tie(0);
    std::cout.tie(0);
    int t;
    std::cin >> t;
    while (t--)
    {
        solve();
    }
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 9080kb

input:

2
8 2
1 2
3 1
4 6
3 5
2 4
8 5
5 7
4 3
1 2
1 3
2 4

output:

2
1

result:

ok 2 lines

Test #2:

score: -100
Time Limit Exceeded

input:

5550
13 4
10 3
9 1
10 8
3 11
8 5
10 7
9 6
13 5
9 7
2 7
5 12
4 8
8 2
4 1
3 4
7 8
2 5
6 7
4 8
2 3
11 1
11 10
1 4
9 10
8 4
3 6
5 7
6 1
10 2
11 7
11 1
17 2
14 16
13 15
17 3
15 11
1 6
13 2
13 17
4 8
14 10
8 14
14 5
9 12
14 2
12 17
17 6
15 7
14 6
2 14
2 13
2 4
8 4
3 11
7 3
14 1
11 9
13 3
5 10
6 8
3 10
14 ...

output:

0
3
112
0
1
0
1
0
0
0
1
0
1
0
0
1
0
140
0
0
0
814
1
6
1
1
2
2
0
612
0
1
0
0
0
1
1
0
0
121
4536
0
0
1718
0
0
1
0
444
1
1908
1813
3
74
0
1
0
46
0
0
0
0
0
0
0
0
0
1
0
1
1
1
239
0
0
0
1
0
0
0
1
0
1
0
0
1
1
0
0
0
1
0
0
0
48
0
2
0
0
0
1
364
0
206
0
0
76
0
1
0
0
2
0
1
2
0
0
1
0
0
4
0
1
1
0
0
1
1
1
0
0
1
1
...

result: