QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#611677#7895. Graph Partitioning 2ji_114514WA 2ms7704kbC++202.2kb2024-10-04 22:04:292024-10-04 22:04:31

Judging History

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

  • [2024-10-04 22:04:31]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:7704kb
  • [2024-10-04 22:04:29]
  • 提交

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)
{
    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 <= k; 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;
            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 = 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: 2ms
memory: 7704kb

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
0

result:

wrong answer 2nd lines differ - expected: '1', found: '0'