QOJ.ac
QOJ
The 2nd Universal Cup Finals is coming! Check out our event page, schedule, and competition rules!
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#278830 | #7895. Graph Partitioning 2 | wenjing233 | WA | 19ms | 7732kb | C++14 | 3.7kb | 2023-12-07 21:09:00 | 2023-12-07 21:09:01 |
Judging History
answer
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int mod=998244353;
struct edge
{
int v,nxt;
}e[500005];
int T,n,k,f[100005][505],F[100005][505],t,h[100005],sz[100005],tmp1[505],tmp2[505],g[100005],len[100005];
void add(int u,int v)
{
e[++t].v=v;
e[t].nxt=h[u];
h[u]=t;
}
void dfs1(int u,int fa)
{
for(int i=0;i<=k+1;i++)
f[u][i]=0;
sz[u]=1;
f[u][1]=1;
for(int i=h[u];i;i=e[i].nxt)
{
int v=e[i].v;
if(v==fa) continue;
dfs1(v,u);
for(int j=0;j<=k+1;j++)
tmp1[j]=0;
for(int j=0;j<=min(sz[v],k);j++)
for(int l=0;l<=min(sz[u],k+1)&&j+l<=k+1;l++)
tmp1[j+l]=(tmp1[j+l]+1ll*f[v][j]*f[u][l])%mod;
for(int j=0;j<=k+1;j++)
f[u][j]=tmp1[j];
sz[u]+=sz[v];
}
f[u][0]=(1ll*f[u][0]+f[u][k]+f[u][k+1])%mod;
}
void dfs2(int u,int fa)
{
g[u]=sz[u]/k;
// len[u]=min(sz[u]%k,g[u]);
for(int i=0;i<=g[u];i++)
f[u][i]=F[u][i]=0;
// sz[u]=1;
f[u][0]=1;
int nw=1,sumg=0;
for(int i=h[u];i;i=e[i].nxt)
{
int v=e[i].v;
if(v==fa) continue;
dfs2(v,u);
for(int j=0;j<=g[u];j++)
tmp1[j]=tmp2[j]=0;
for(int j=0;j<=g[v];j++)
for(int l=0;l<=sumg&&j+l<=g[u];l++)
{
tmp1[j+l]=(tmp1[j+l]+1ll*f[v][j]*f[u][l])%mod;
tmp2[j+l]=(tmp2[j+l]+1ll*F[v][j]*f[u][l])%mod;
tmp2[j+l]=(tmp2[j+l]+1ll*f[v][j]*F[u][l])%mod;
}
for(int j=0;j<=g[u];j++)
f[u][j]=tmp1[j],F[u][j]=tmp2[j];
sumg+=g[v];
// sz[u]+=sz[v];
}
if(g[u]!=sumg)
{
if(sumg<=g[u]-2)
{
for(int j=0;j<=g[u];j++)
f[u][j]=F[u][j]=0;
return;
}
for(int j=0;j<=g[u];j++)
{
tmp1[j]=0;
tmp2[j]=f[u][j];
}
for(int t=0;t<=1;t++)
{
int nw=sz[u]-(g[u]*k+t);
if(0<=nw&&nw<=g[u]) tmp1[nw+t]=(tmp1[nw+t]+f[u][nw])%mod;
nw=sz[u]-((g[u]-1)*k+t);
if(0<=nw&&nw<=g[u]) tmp2[nw+t]=(tmp2[nw+t]+F[u][nw])%mod;
}
for(int j=0;j<=g[u];j++)
f[u][j]=tmp1[j],F[u][j]=tmp2[j];
}
else
{
for(int j=0;j<=g[u];j++)
{
tmp1[j]=f[u][j];
tmp2[j]=F[u][j];
}
for(int t=1;t<=1;t++)
{
int nw=sz[u]-(g[u]*k+t);
if(0<=nw&&nw<=g[u]) tmp1[nw+t]=(tmp1[nw+t]+F[u][nw])%mod;
}
for(int j=0;j<=g[u];j++)
f[u][j]=tmp1[j],F[u][j]=tmp2[j];
}
//printf("u=%d,g=%d\n",u,g[u]);
//for(int j=0;j<=g[u];j++)
// printf("%d %d\n",f[u][j],F[u][j]);
}
void dfs(int u,int fa)
{
sz[u]=1;
for(int i=h[u];i;i=e[i].nxt)
{
int v=e[i].v;
if(v==fa) continue;
dfs(v,u);
sz[u]+=sz[v];
}
}
int main()
{
// freopen("input.txt", "r", stdin);
// freopen("2.txt", "w", stdout);
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&k);
// k=300,n=k*k-3;
for(int i=0;i<=n;i++)
h[i]=0;
t=0;
for(int i=1;i<n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
// u=i,v=i+1;
add(u,v);
add(v,u);
}
dfs(1,0);
if(1ll*k*k<=n)
{
dfs1(1,0);
printf("%d\n",f[1][0]);
continue;
}
dfs2(1,0);
if(sz[1]%k<=g[1])
printf("%d\n",f[1][sz[1]%k]);
else printf("0\n");
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3728kb
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: 19ms
memory: 7732kb
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 0 1 0 1 0 0 0 1 0 1 0 0 1 0 140 0 0 0 814 1 6 1 1 2 1 0 612 0 1 0 0 0 1 1 0 0 121 4536 0 0 1718 0 0 0 0 444 1 1908 1813 3 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 0 0 1 364 0 206 0 0 76 0 0 0 0 2 0 1 2 0 0 1 0 0 4 0 1 1 0 0 1 1 1 0 0 1 1 ...
result:
wrong answer 28th lines differ - expected: '2', found: '1'