QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#611680#7895. Graph Partitioning 2ji_114514WA 19ms10068kbC++202.3kb2024-10-04 22:05:452024-10-04 22:05:46

Judging History

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

  • [2024-10-04 22:05:46]
  • 评测
  • 测评结果:WA
  • 用时:19ms
  • 内存:10068kb
  • [2024-10-04 22:05:45]
  • 提交

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 <= 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 = 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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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
Wrong Answer
time: 19ms
memory: 10068kb

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
170
1
4
12
2
5
0
0
3
0
2
0
0
1
1
190
0
0
0
1799
2
7
3
2
8
2
0
1260
0
2
0
0
0
1
2
2
0
256
12450
5
0
5154
1
0
0
0
1145
1
3100
6773
3
220
0
1
0
68
0
0
0
2
0
2
0
1
0
0
0
1
1
1
460
0
0
0
4
0
0
0
3
0
0
1
0
1
2
0
3
0
2
2
0
0
61
0
4
0
1
1
1
1087
0
508
0
0
114
0
1
4
0
6
2
0
4
0
0
0
0
1
8
0
1
2
0
0
0
2
2
...

result:

wrong answer 3rd lines differ - expected: '112', found: '170'