QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#356443 | #7895. Graph Partitioning 2 | ucup-team1198# | ML | 1ms | 3652kb | C++20 | 3.8kb | 2024-03-17 19:43:36 | 2024-03-17 19:43:37 |
Judging History
answer
#include <map>
#include <set>
#include <array>
#include <cmath>
#include <deque>
#include <bitset>
#include <random>
#include <string>
#include <vector>
#include <cassert>
#include <complex>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
using namespace std;
const int MOD = 998244353;
int add(int a, int b) {
if (a + b < MOD)
return a + b;
return a + b - MOD;
}
int sub(int a, int b) {
if (a >= b)
return a - b;
return a + MOD - b;
}
int mul(int a, int b) {
return a * 1ll * b % MOD;
}
struct SuperMap {
map<int, int> dp;
int to_add;
void insert(int x, int delta) {
x -= to_add;
dp[x] = add(dp[x], delta);
}
void add_all(int x) {
to_add += x;
}
void clear() {
dp.clear();
to_add = 0;
}
void swap_other(SuperMap& other) {
swap(dp, other.dp);
swap(to_add, other.to_add);
}
};
int k;
const int MAXN = 100100;
vector<int> G[MAXN];
int sz[MAXN];
int cnt[MAXN];
void dfs_sz(int v, int p) {
sz[v] = 1;
for (int u : G[v]) {
if (u == p)
continue;
dfs_sz(u, v);
sz[v] += sz[u];
}
cnt[v] = 1;
for (int i = 0; i < G[v].size(); ++i) {
if (G[v][i] == p) {
swap(G[v][i], G[v].back());
G[v].pop_back();
--i;
} else if (sz[G[v][i]] < k) {
cnt[v] += sz[G[v][i]];
swap(G[v][i], G[v].back());
G[v].pop_back();
--i;
}
}
sort(G[v].begin(), G[v].end(), [](int a, int b) {
return sz[a] > sz[b];
});
}
const int K = 20;
SuperMap maps[K + 1];
void dfs_dp(int v, int w) {
maps[w].clear();
if (G[v].empty()) {
maps[w].insert(cnt[v], 1);
} else {
dfs_dp(G[v][0], w);
maps[w].add_all(cnt[v]);
for (int i = 1; i < G[v].size(); ++i) {
maps[K].clear();
dfs_dp(G[v][i], w + 1);
for (auto [x, cnt1] : maps[w].dp) {
for (auto [y, cnt2] : maps[w + 1].dp) {
int total_cnt = x + y + maps[w].to_add + maps[w + 1].to_add;
if (total_cnt <= k + 1)
maps[K].insert(total_cnt, mul(cnt1, cnt2));
}
}
maps[w].swap_other(maps[K]);
}
}
auto& cur_dp = maps[w];
int new_zero = 0;
auto tmp = cur_dp.dp.find(k + 1 - cur_dp.to_add);
if (tmp != cur_dp.dp.end()) {
new_zero = add(new_zero, tmp->second);
}
tmp = cur_dp.dp.find(k - cur_dp.to_add);
if (tmp != cur_dp.dp.end()) {
new_zero = add(new_zero, tmp->second);
}
if (new_zero)
cur_dp.insert(0, new_zero);
while (!cur_dp.dp.empty()) {
auto tmp = --cur_dp.dp.end();
if (tmp->first + cur_dp.to_add > k)
cur_dp.dp.erase(tmp);
else
break;
}
}
void solve() {
int n;
cin >> n >> k;
for (int i = 1; i < n; ++i) {
int a, b;
cin >> a >> b;
//a = i + 1;
//b = rand() % i + 1;
--a;
--b;
G[a].emplace_back(b);
G[b].emplace_back(a);
}
dfs_sz(0, -1);
for (int i = 0; i < n; ++i) {
if (cnt[i] > k + 1) {
cout << 0 << '\n';
return;
}
}
dfs_dp(0, 0);
auto& res = maps[0];
cout << res.dp[-res.to_add] << '\n';
for (int i = 0; i < n; ++i)
G[i].clear();
}
int main() {
ios::sync_with_stdio(false);
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: 1ms
memory: 3652kb
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
Memory 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 ...