QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#625949 | #7895. Graph Partitioning 2 | MiniLong | WA | 11ms | 20632kb | C++17 | 5.2kb | 2024-10-09 21:59:31 | 2024-10-09 21:59:31 |
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) add(ans, f[1][i][0] + f[1][i][1]);
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: 100
Accepted
time: 1ms
memory: 7764kb
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: 11ms
memory: 20632kb
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:
1 16 112 0 4 0 2 0 0 0 0 0 2 0 0 1 0 140 0 0 0 814 2 0 0 2 0 2 0 612 0 2 1 0 0 1 2 0 0 121 4536 0 1 1718 0 0 0 0 444 0 1908 1813 16 74 0 0 0 46 0 1 0 0 0 0 0 1 0 0 0 0 1 1 239 0 0 0 4 0 0 0 0 0 0 0 0 1 2 0 0 0 0 0 0 0 48 0 0 1 0 0 0 364 0 206 0 0 76 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 0 0 0 2 2 0 0 2 ...
result:
wrong answer 1st lines differ - expected: '0', found: '1'