QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#581545 | #7895. Graph Partitioning 2 | ggggggg | WA | 32ms | 16236kb | C++14 | 1.8kb | 2024-09-22 13:20:19 | 2024-09-22 13:20:19 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=100005,P=998244353;
int n,k,sz[N],dp[N][505],f[505],st[N];
vector<int> G[N];
unordered_map<int,int> g[N],gg;
void Inc(int&x,int y){
if((x+=y)>=P) x-=P;
}
void dfs(int u,int fa){
sz[u]=1,g[u][1]=(++st[u]),dp[u][1]=1;
for(auto v:G[u]) if(v^fa){
dfs(v,u),gg.clear();
int st1=0;
for(auto i:g[v]) for(auto j:g[u]){
int x=i.first,y=j.first;
if(x+y>k+1||dp[u][j.second]==0||dp[v][i.second]==0) continue;
if(!gg.count(x+y)) gg[x+y]=(++st1);
Inc(f[gg[x+y]],1ll*dp[u][j.second]*dp[v][i.second]%P);
}
for(int i=1;i<=st1;i++) dp[u][i]=f[i],f[i]=0;
sz[u]+=sz[v],st[u]=st1,g[u]=gg;
}
if(g[u].count(k)||g[u].count(k+1)){
if(!g[u].count(0)) g[u][0]=(++st[u]),dp[u][st[u]]=0;
if(g[u].count(k)) Inc(dp[u][g[u][0]],dp[u][g[u][k]]);
if(g[u].count(k+1)) Inc(dp[u][g[u][0]],dp[u][g[u][k+1]]);
}
}
void dfs1(int u,int fa){
sz[u]=1,dp[u][1]=1,dp[u][0]=0;
for(auto v:G[u]) if(v^fa){
dfs1(v,u);
for(int i=0;i<=min(sz[v],k);i++) for(int j=0;j<=min(sz[u],k+1-i);j++) if(dp[u][j]&&dp[v][i]){
Inc(f[i+j],1ll*dp[u][j]*dp[v][i]%P);
}
sz[u]+=sz[v];
for(int i=0;i<=sz[u];i++) dp[u][i]=f[i],f[i]=0;
}
Inc(dp[u][0],dp[u][k]);
Inc(dp[u][0],dp[u][k+1]);
}
void solve(){
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++) G[i].clear(),g[i].clear(),st[i]=0;
for(int i=2,x,y;i<=n;i++) scanf("%d%d",&x,&y),G[x].push_back(y),G[y].push_back(x);
if(k>450){
dfs(1,0);
printf("%d\n",dp[1][g[1][0]]);
}
else{
dfs1(1,0);
printf("%d\n",dp[1][0]);
}
}
int main(){
int T;
scanf("%d",&T);
while(T--) solve();
}
/*
1
4 3
1 2
1 3
2 4
*/
详细
Test #1:
score: 100
Accepted
time: 3ms
memory: 13104kb
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: 32ms
memory: 16236kb
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 5 512 12328 1 187848 444668188 305695180 0 0 6712 4 1 1337656 4 5 18058356 389258289 0 439625852 0 87370434 1 239417607 959039294 1 568320626 2 0 736873729 3 5 0 1 2 2 4 100 0 1712 126657668 608407114 0 258031265 15 0 333 10411 853903324 2 291280323 106879531 185814561 598760093 0 5356210 0 122 0 ...
result:
wrong answer 2nd lines differ - expected: '3', found: '5'