QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#299111 | #7895. Graph Partitioning 2 | ucup-team1303# | WA | 35ms | 16400kb | C++14 | 3.1kb | 2024-01-06 17:15:38 | 2024-01-06 17:15:39 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define mk make_pair
#define fi first
#define se second
using namespace std;
inline int read(){
int x=0,f=1;char c=getchar();
for(;(c<'0'||c>'9');c=getchar()){if(c=='-')f=-1;}
for(;(c>='0'&&c<='9');c=getchar())x=x*10+(c&15);
return x*f;
}
const int mod=998244353;
int ksm(int x,int y,int p=mod){
int ans=1;
for(int i=y;i;i>>=1,x=1ll*x*x%p)if(i&1)ans=1ll*ans*x%p;
return ans%p;
}
int inv(int x,int p=mod){return ksm(x,p-2,p)%p;}
mt19937 rnd(time(0));
int randint(int l,int r){return rnd()%(r-l+1)+l;}
void add(int &x,int v){x+=v;if(x>=mod)x-=mod;}
void Mod(int &x){if(x>=mod)x-=mod;}
int cmod(int x){if(x>=mod)x-=mod;return x;}
void cmax(int &x,int v){x=max(x,v);}
void cmin(int &x,int v){x=min(x,v);}
const int N=1e5+5;
const int B=700;
const int NB=N/B;
vector<int>G[N];
int n,k;
namespace solve1{
int f[N][B+5],sz[N],tmp[B+5];
void dp(int u,int fa){
for(int v:G[u])if(v!=fa)dp(v,u);
sz[u]=1;f[u][1]=1;
// cout<<"DP "<<u<<" "<<fa<<endl;
for(int v:G[u]){
if(v==fa)continue;
// cout<<"ins v = "<<v<<endl;
// cout<<"f = ";for(int j=0;j<=min(sz[v],k+1);j++)cout<<f[v][j]<<" ";puts("");
for(int i=0;i<=min(sz[u]+sz[v],k+1);i++)tmp[i]=0;
for(int i=0;i<=min(sz[u],k+1);i++)add(tmp[i],1ll*f[u][i]*f[v][k]%mod);
for(int i=0;i<=min(sz[u],k+1);i++)add(tmp[i],1ll*f[u][i]*f[v][k+1]%mod);
for(int i=0;i<=min(sz[u],k+1);i++)for(int j=0;j<=sz[v]&&i+j<=k+1;j++){
add(tmp[i+j],1ll*f[u][i]*f[v][j]%mod);
}
for(int i=0;i<=min(sz[u]+sz[v],k+1);i++)f[u][i]=tmp[i];
sz[u]+=sz[v];
// cout<<"-> f "<<u<<" = ";for(int j=0;j<=min(sz[u],k+1);j++)cout<<f[u][j]<<" ";puts("");
}
// cout<<"f "<<u<<" = ";for(int j=0;j<=min(sz[u],k+1);j++)cout<<f[u][j]<<" ";puts("");
}
void solve(){
dp(1,0);
cout<<cmod(f[1][k]+f[1][k+1])<<endl;
for(int i=1;i<=n;i++)for(int j=0;j<=k;j++)f[i][j]=0;
}
};
namespace solve2{
map<pair<int,int>,int>f[N];
int sz[N];
void dp(int u,int fa){
for(int v:G[u])if(v!=fa)dp(v,u);
sz[u]=1,f[u][mk(0,0)]=1;
for(int v:G[u]){
if(v==fa)continue;
auto tmp=f[u];f[u].clear();
auto Add=[&](int p,int q,int v){
auto it=f[u].find(mk(p,q));
if(it==f[u].end())f[u][mk(p,q)]=v;
else add((*it).second,v);
};
for(auto [A,x]:tmp)for(auto [B,y]:f[v]){
int tu=sz[u]-A.fi*k-(A.se)*(k+1),tv=sz[v]-B.fi*k-(B.se)*(k+1);
if(tu+tv<=k+1)Add(A.fi+B.fi,A.se+B.se,1ll*x*y%mod);
if(tv==k||tv==k+1)Add(A.fi+B.fi+(tv==k),A.se+B.se+(tv==k+1),1ll*x*y%mod);
}
sz[u]+=sz[v];
}
for(int v:G[u])if(v!=fa)f[v].clear();
}
void solve(){
// puts("solve2");
dp(1,0);int ans=0;
for(auto [A,x]:f[1]){
int siz=n-k*A.fi-(k+1)*A.se;
if(siz==k||siz==k+1)add(ans,x);
}
cout<<ans<<endl;
f[1].clear();
}
}
const int LIM=700;
void solve(){
n=read(),k=read();
for(int i=1;i<=n-1;i++){
int u=read(),v=read();
G[u].emplace_back(v),G[v].emplace_back(u);
}
if(k<=LIM)solve1::solve();
else solve2::solve();
for(int i=1;i<=n;i++)G[i].clear();
}
signed main(void){
#ifndef ONLINE_JUDGE
freopen("in.in","r",stdin);
#endif
int tt=read();while(tt--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 12008kb
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: 35ms
memory: 16400kb
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:
0 3 112 172 1 0 1 0 0 0 1 0 1 0 0 1 0 140 0 0 0 814 1 6 12 1 2 2 0 612 0 2 0 0 0 1 1 0 0 121 4718772 137214096 0 1718 0 0 1 0 444 2 85140 68470387 21943332 74 0 1 0 46 0 0 0 0 0 0 0 0 0 1 0 1 1 1 239 0 0 0 1 0 0 0 1 0 1 0 0 1 1 0 0 0 1 0 0 0 48 0 2 0 2 1 1 364 0 328 0 0 76 0 1 0 0 2 1 2 4 0 0 1 0 0 ...
result:
wrong answer 4th lines differ - expected: '0', found: '172'