QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#625960 | #7895. Graph Partitioning 2 | MiniLong | WA | 2ms | 7692kb | C++17 | 5.3kb | 2024-10-09 22:03:39 | 2024-10-09 22:03:39 |
Judging History
answer
#include <bits/stdc++.h>
#define _rep(i, x, y) for(int i = x; i <= y; ++i)
#define _req(i, x, y) for(int i = x; i >= y; --i)
#define _rev(i, u) for(int i = head[u]; i; i = e[i].nxt)
#define pb push_back
#define fi first
#define se second
#define mst(f, i) memset(f, i, sizeof f)
using namespace std;
#ifdef ONLINE_JUDGE
#define debug(...) 0
#else
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#endif
typedef long long ll;
typedef pair<int, int> PII;
namespace fastio{
#ifdef ONLINE_JUDGE
char ibuf[1 << 20],*p1 = ibuf, *p2 = ibuf;
#define get() p1 == p2 && (p2 = (p1 = ibuf) + fread(ibuf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++
#else
#define get() getchar()
#endif
template<typename T> inline void read(T &t){
T x = 0, f = 1;
char c = getchar();
while(!isdigit(c)){
if(c == '-') f = -f;
c = getchar();
}
while(isdigit(c)) x = x * 10 + c - '0', c = getchar();
t = x * f;
}
template<typename T, typename ... Args> inline void read(T &t, Args&... args){
read(t);
read(args...);
}
template<typename T> void write(T t){
if(t < 0) putchar('-'), t = -t;
if(t >= 10) write(t / 10);
putchar(t % 10 + '0');
}
template<typename T, typename ... Args> void write(T t, Args... args){
write(t), putchar(' '), write(args...);
}
template<typename T> void writeln(T t){
write(t);
puts("");
}
template<typename T> void writes(T t){
write(t), putchar(' ');
}
#undef get
};
using namespace fastio;
#define multitest() int T; read(T); _rep(tCase, 1, T)
namespace Calculation{
const ll mod = 998244353;
ll ksm(ll p, ll h){ll base = p % mod, res = 1; while(h){if(h & 1ll) res = res * base % mod; base = base * base % mod, h >>= 1ll;} return res;}
void dec(ll &x, ll y){x = ((x - y) % mod + mod) % mod;}
void add(ll &x, ll y){x = (x + y) % mod;}
void mul(ll &x, ll y){x = x * y % mod;}
ll sub(ll x, ll y){return ((x - y) % mod + mod) % mod;}
ll pls(ll x, ll y){return ((x + y) % mod + mod) % mod;}
ll mult(ll x, ll y){return x * y % mod;}
}
using namespace Calculation;
const int N = 1e5 + 5, B = 1, M = 505;
int n, k, siz[N];
vector<int> G[N];
namespace solution1{
ll f[N][M], g[M];
void dfs(int u, int fa){
f[u][1] = 1, siz[u] = 1;
for(auto &v : G[u]){
if(v == fa) continue;
dfs(v, u);
_rep(i, 1, min(siz[u], k + 1)) _rep(j, 0, min(siz[v], k + 1 - i)){
add(g[i + j], f[u][i] * f[v][j] % mod);
}
siz[u] += siz[v];
_rep(i, 0, min(siz[u], k + 1)) f[u][i] = g[i], g[i] = 0;
}
add(f[u][0], f[u][k] + f[u][k + 1]), f[u][k + 1] = 0;
}
void solve(){
dfs(1, 0);
writeln(f[1][0]);
_rep(i, 1, n) _rep(j, 0, k + 1) f[i][j] = 0;
_rep(i, 1, n) G[i].clear(), siz[i] = 0;
}
}
namespace solution2{
ll f[N][M][2], g[M][2];
void dfs(int u, int fa){
f[u][0][0] = 1, siz[u] = 1;
for(auto &v : G[u]){
if(v == fa) continue;
dfs(v, u);
_rep(i, 0, siz[u] / k) _rep(j, 0, siz[v] / k){
_rep(p, 0, 1) _rep(q, 0, 1){
int x = (siz[u] - i * k) / (k + 1) - p, y = (siz[v] - j * k) / (k + 1) - q;
x = siz[u] - (k + 1) * x, y = siz[v] - (k + 1) * y;
if(x){
if(!p && x == 1){
if(q && k == 2){
add(g[i + j + 1][1], f[u][i][p] * f[v][j][q]);
}
if(!q && x + y == k + 1){
add(g[i + j][1], f[u][i][p] * f[v][j][q]);
}
}
if(p){
if(q || !y) add(g[i + j][1], f[u][i][p] * f[v][j][q]);
}
if(!p){
if(q && x + 1 <= k + 1) add(g[i + j + 1][0], f[u][i][p] * f[v][j][q]);
if(q) add(g[i + j][0], f[u][i][p] * f[v][j][q]);
if(!q && x + y <= k + 1) add(g[i + j][0], f[u][i][p] * f[v][j][q]);
}
}else if(!p){
if(q || !y) add(g[i + j][0], f[u][i][p] * f[v][j][q]);
}
}
}
siz[u] += siz[v];
_rep(i, 0, siz[u] / k) _rep(p, 0, 1) f[u][i][p] = g[i][p], g[i][p] = 0;
}
}
void solve(){
dfs(1, 0);
ll ans = 0;
_rep(i, 0, n){
_rep(j, 0, 1){
if(j || (n - i * k) % (k + 1) == 0) add(ans, f[1][i][j]);
}
}
writeln(ans);
_rep(i, 1, n) _rep(j, 0, n / k) _rep(p, 0, 1) f[i][j][p] = 0;
_rep(i, 1, n) G[i].clear(), siz[i] = 0;
}
}
int main(){
multitest(){
read(n, k);
_rep(i, 2, n){
int u, v; read(u, v);
G[u].pb(v), G[v].pb(u);
}
if(k <= B) solution1::solve();
else{
solution2::solve();
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 7692kb
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:
0 1
result:
wrong answer 1st lines differ - expected: '2', found: '0'