QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#693852 | #7895. Graph Partitioning 2 | ttao | RE | 0ms | 3716kb | C++14 | 2.7kb | 2024-10-31 16:50:42 | 2024-10-31 16:50:43 |
Judging History
answer
#include <bits/stdc++.h>
#pragma optimize(2)
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
const int P = 998244353;
void add(ll& a, ll b) {
a = a + b;
if (a >= P) a -= P;
}
void solve() {
int n, k;
cin >> n >> k;
vector<vector<int>> G(n + 5);
vector<int> fa(n + 5);
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
vector<vector<pll>> dp(n + 5);
vector<ll> f(k + 5);
vector<int> stk;
function<void(int)> dfs = [&](int u) {
if (k == 1) {
dp[u].push_back({0, 1});
dp[u].push_back({1, 1});
} else {
dp[u].push_back({1, 1});
}
for (auto v : G[u]) {
if (v == fa[u]) continue;
fa[v] = u;
dfs(v);
for (auto [a, b] : dp[u]) {
for (auto [c, d] : dp[v]) {
if (a != 0) {
if (a + c == k + 1) {
add(f[0], b * d % P);
stk.push_back(0);
} else if (a + c == k) {
if (c != 0) {
add(f[0], b * d % P);
stk.push_back(0);
}
add(f[k], b * d % P);
stk.push_back(k);
} else if (a + c < k) {
add(f[(a + c)], b * d % P);
stk.push_back(a + c);
}
} else if (a == 0 && c == 0) {
add(f[0], b * d % P);
stk.push_back(0);
}
}
}
sort(stk.begin(), stk.end());
stk.erase(unique(stk.begin(), stk.end()), stk.end());
vector<pll> g;
while (!stk.empty()) {
auto x = stk.back();
stk.pop_back();
g.push_back({x, f[x]});
// cout << u << ' ' << x << ' ' << f[x] << '\n';
f[x] = 0;
}
dp[u] = move(g);
}
};
dfs(1);
auto p = *dp[1].rbegin();
cout << (p.first == 0 ? p.second : 0) << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
double prev = clock();
// freopen("data.txt", "r", stdin);
int T;
cin >> T;
while (T--)
solve();
double cur = clock();
// cout << "time=" << (cur - prev) / CLOCKS_PER_SEC << '\n';
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3716kb
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
Runtime Error
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 ...