QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#356441 | #7895. Graph Partitioning 2 | ucup-team1198# | ML | 2ms | 9388kb | C++20 | 3.8kb | 2024-03-17 19:36:57 | 2024-03-17 19:36:58 |
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;
}
};
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;
}
}
}
SuperMap maps[MAXN];
int map_id[MAXN];
void dfs_dp(int v) {
map_id[v] = v;
maps[v].clear();
for (int u : G[v]) {
dfs_dp(u);
}
if (G[v].empty()) {
maps[map_id[v]].insert(cnt[v], 1);
} else if (G[v].size() == 1) {
map_id[v] = map_id[G[v][0]];
maps[map_id[v]].add_all(cnt[v]);
} else {
int place_real = map_id[G[v][0]];
maps[map_id[G[v][0]]].add_all(cnt[v]);
int place_extra = v;
for (int i = 1; i < G[v].size(); ++i) {
maps[place_extra].clear();
int cur = map_id[G[v][i]];
for (auto [x, cnt1] : maps[place_real].dp) {
for (auto [y, cnt2] : maps[cur].dp) {
int total_cnt = x + y + maps[place_real].to_add + maps[cur].to_add;
if (total_cnt <= k + 1)
maps[place_extra].insert(total_cnt, mul(cnt1, cnt2));
}
}
swap(place_real, place_extra);
}
map_id[v] = place_real;
}
auto& cur_dp = maps[map_id[v]];
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;
--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);
auto& res = maps[map_id[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: 2ms
memory: 9388kb
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 ...