QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#300019 | #7895. Graph Partitioning 2 | Loging# | WA | 21ms | 14848kb | C++20 | 2.2kb | 2024-01-07 16:24:17 | 2024-01-07 16:24:18 |
Judging History
answer
#include <bits/stdc++.h>
#define Max 100005
using namespace std;
const int mod=998244353;
int T,n,k,N1,N2,son[Max];
int f[Max][350],F[Max][350],g[Max][350],G[Max][350];
vector<int>E[Max];
inline int dfs(int u,int fa){
int p=1;
son[u]=1;
f[u][1]=1;
for(int v:E[u]){
if(v==fa)continue;
int siz=dfs(v,u);
for(int i=min(p,k+1);i>=0;i--){
for(int j=0;j<=siz&&i+j<=k+1;j++){
F[u][i+j]=(F[u][i+j]+1ll*f[u][i]*f[v][j])%mod;
}
f[u][i]=0;
}
for(int j=0;j<=siz;j++)f[v][j]=0;
for(int i=0;i<=siz+p&&i<=k+1;i++){
f[u][i]=F[u][i];
F[u][i]=0;
// cout<<u<<" "<<i<<" "<<f[u][i]<<endl;
}
son[u]+=son[v];
p+=siz;
}
f[u][0]=((f[u][0]+f[u][k]%mod)+f[u][k+1])%mod;
f[u][k+1]=0;
// for(int i=0;i<=k+1;i++)cout<<u<<" "<<i<<" "<<f[u][i]<<endl;
return p;
}
inline int dfs1(int u,int fa){
f[u][0]=1;
g[u][0]=0;
int p=0;
son[u]=1;
for(int v:E[u]){
if(v==fa)continue;
int siz=dfs1(v,u);
for(int i=min(p,N1+1);i>=0;i--){
for(int j=0;j<=siz&&i+j<=N1+1;j++){
F[u][i+j]=(F[u][i+j]+1ll*f[u][i]*f[v][j])%mod;
G[u][i+j]=g[u][i]+g[v][j];
}
}
for(int j=0;j<=siz;j++)f[v][j]=0,g[v][j]=0;
for(int i=0;i<=siz+p&&i<=N1+1;i++){
f[u][i]=F[u][i];
g[u][i]=G[u][i];
F[u][i]=0;
G[u][i]=0;
// cout<<u<<" "<<i<<" "<<f[u][i]<<" "<<g[u][i]<<endl;
}
p+=siz;
son[u]+=son[v];
}
for(int i=min(N1+1,p+1);i>=0;i--){
if(!f[u][i])continue;
int now=son[u]-(k+1)*g[u][i]-k*i;
if(now<0)continue;
if(now==k+1){
g[u][i]=g[u][i]+1;
}else if(now==k){
f[u][i+1]=(f[u][i+1]+f[u][i])%mod;
g[u][i+1]=g[u][i];
p=max(p,i+1);
}
// cout<<"!"<<u<<" "<<i<<" "<<g[u][i]<<" "<<now<<endl;
}
// for(int i=0;i<=p+1;i++){
// cout<<u<<" "<<i<<" "<<f[u][i]<<" "<<g[u][i]<<endl;
// }
return p;
}
int main(){
ios::sync_with_stdio(false);
cin>>T;
while(T--){
cin>>n>>k;
for(int i=1;i<=n;i++)E[i].clear();
for(int i=1;i<n;i++){
int u,v;
cin>>u>>v;
E[u].push_back(v);
E[v].push_back(u);
}
if(k<=sqrt(n)){
dfs(1,1);
cout<<f[1][0]<<'\n';
}else{
N1=n/k;
N2=n%k;
N1-=N2;
dfs1(1,1);
cout<<f[1][N1]<<'\n';
}
for(int j=0;j<350;j++)f[1][j]=g[1][j]=0;
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 10952kb
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: 21ms
memory: 14848kb
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 1 1 1 0 1 0 140 0 0 0 814 1 6 1 1 2 2 1 612 0 1 0 1 0 1 1 0 0 121 4536 0 0 1718 0 1 1 0 444 1 1908 1813 3 74 0 1 1 46 0 0 1 0 0 0 0 0 1 1 0 1 1 1 239 0 0 1 1 1 0 0 1 1 1 0 1 1 1 0 0 0 1 1 0 0 48 0 2 0 0 2 1 364 0 206 0 1 76 1 1 0 0 2 1 1 2 0 1 1 0 0 4 1 1 1 0 0 1 1 1 0 0 1 1 ...
result:
wrong answer 12th lines differ - expected: '0', found: '1'