QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#611693 | #7895. Graph Partitioning 2 | ji_114514 | WA | 1ms | 5696kb | C++20 | 2.4kb | 2024-10-04 22:09:06 | 2024-10-04 22:09:07 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5 + 10, mod = 998244353;
vector<int>e[N];
int n, k, B;
int dp[N][510], t[510], sz[N];
void dfs(int u, int f)
{
for (int i = 0; i <= B; i++)dp[u][i] = 0;
if (k <= B)dp[u][1] = 1;
else dp[u][0] = 1;
sz[u] = 1;
for (auto v : e[u])
{
if (v == f)continue;
dfs(v, u);
if (k <= B) {
for (int i = 0; i <= min(k, sz[u]); i++)t[i] = dp[u][i], dp[u][i] = 0;
for (int i = 1; i <= min(k, sz[u]); i++)
{
for (int j = 1; j <= min(k + 1 - i, sz[v]); j++)
{
dp[u][i + j] = (dp[u][i + j] + 1ll * t[i] * dp[v][j]) % mod;
}
}
dp[u][0] = (dp[u][0] + dp[u][k + 1]) % mod, dp[u][k + 1] = 0;
for (int i = 0; i <= k; i++)dp[u][i] = (dp[u][i] + 1ll * t[i] * (dp[v][0] + dp[v][k])) % mod;
}
else {
for (int i = 0; i * k <= sz[u]; i++)t[i] = dp[u][i], dp[u][i] = 0;
for (int i = 0; i * k <= sz[u]; i++)
{
for (int j = 0; j * k <= sz[v]; j++)
{
int a = (sz[u] - i * k) % (k + 1), b = (sz[v] - j * k) % (k + 1);
if (b == 0 || b == k) {
dp[u][i] = (dp[u][i] + 1ll * t[i] * dp[v][j]) % mod;
}
if (a == 0 || b == 0 || a + b > k + 1)continue;
dp[u][i + j] = (dp[u][i + j] + 1ll * t[i] * dp[v][j]) % mod;
}
}
}
sz[u] += sz[v];
}
}
void solve()
{
cin >> n >> k;
B = sqrt(n);
for (int i = 1; i <= n; i++)e[i].clear();
for (int i = 0; i < n - 1; i++)
{
int u, v; cin >> u >> v;
e[u].push_back(v), e[v].push_back(u);
}
dfs(1, 0);
if (k <= B) {
//cout<<dp[3][1]<<endl;
cout << (dp[1][k] + dp[1][0]) % mod << '\n';
}
else {
int res = 0;
for (int i = 0; i * k <= sz[1]; i++) {
int j = (sz[1] - i * k) % (k + 1);
if (j == 0 || j == k)res = (res + dp[1][i]) % mod;
}
cout << res << '\n';
}
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t; cin >> t;
while (t--)solve();
return 0;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 5696kb
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:
4 1
result:
wrong answer 1st lines differ - expected: '2', found: '4'