QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#303666 | #7895. Graph Partitioning 2 | Loging | WA | 681ms | 148680kb | C++20 | 3.1kb | 2024-01-12 20:59:47 | 2024-01-12 20:59:47 |
Judging History
answer
#include<cstdio>
#include<algorithm>
#include<map>
#include<unordered_map>
#define P 998244353
#define M 100005
using namespace std;
struct E{
int to,nx;
}edge[M<<1];
int tot,head[M];
void Addedge(int a,int b){
edge[++tot].to=b;
edge[tot].nx=head[a];
head[a]=tot;
}
int K;
int fa[M];
int dp[M][350],sz[M],DP[350];
void Add(int &x,int y){
x=(x+y)%P;
}
void dfs1(int now){
// printf("now=%d\n",now);
sz[now]=1;
dp[now][K+1]=dp[now][K]=0;
dp[now][0]=0;
dp[now][1]=1;
for(int i=head[now];i;i=edge[i].nx){
int nxt=edge[i].to;
if(nxt==fa[now])continue;
// printf("nxt=%d\n",nxt);
fa[nxt]=now;
dfs1(nxt);
int sza=min(sz[now],K+1),szb=min(sz[nxt],K+1);
for(int j=0;j<=min(sza+szb,K+1);j++)DP[j]=0;
for(int j=0;j<=sza;j++){
for(int k=0;k<=szb&&j+k<=K+1;k++){
Add(DP[j+k],1ll*dp[now][j]*dp[nxt][k]%P);
}
}
for(int j=0;j<=min(sza+szb,K+1);j++)dp[now][j]=DP[j];
sz[now]+=sz[nxt];
}
Add(dp[now][0],dp[now][K+1]);
Add(dp[now][0],dp[now][K]);
dp[now][K+1]=0;
}
//void dfs2(int now){
// sz[now]=1;
// dp[now][0]=1;
// for(int i=head[now];i;i=edge[i].nx){
// int nxt=edge[i].to;
// if(nxt==fa[now])continue;
// fa[nxt]=now;
// dfs2(nxt);
// int sza=sz[now]/(K+1),szb=sz[nxt]/(K+1),szc=(sz[now]+sz[nxt])/(K+1);
// for(int j=0;j<=szc;j++)DP[j]=0;
// for(int j=0;j<=sza;j++){
// for(int k=0;k<=szb;k++){
// Add(DP[j+k],1ll*dp[now][j]*dp[nxt][k]%P);
// }
// }
// for(int j=0;j<=szc;j++)dp[now][j]=DP[j];
// sz[now]+=sz[nxt];
// }
// for(int i=0;i<=sz[now]/(K+1);i++){
// int res=sz[now]-i*(K+1);
// if(res<K){}
// else if(res==K){
// if(now!=1)Add(dp[now][i],dp[now][i]);
// }else if(res==K+1){
// Add(dp[now][i+1],dp[now][i]);
// dp[now][i]=0;
// }else dp[now][i]=0;
// printf("dp[%d][%d]=%d\n",now,i,dp[now][i]);
// }
//}
unordered_map<int,int>mp[M];
void dfs2(int now){
// printf("now=%d\n",now);
mp[now].clear();
sz[now]=1;
mp[now][1]=1;
for(int i=head[now];i;i=edge[i].nx){
int nxt=edge[i].to;
if(nxt==fa[now])continue;
// printf("nxt=%d\n",nxt);
fa[nxt]=now;
dfs2(nxt);
unordered_map<int,int>tmp;
for(auto it1=mp[now].begin();it1!=mp[now].end();it1++){
for(auto it2=mp[nxt].begin();it2!=mp[nxt].end();it2++){
int cnt=(*it1).first+(*it2).first;
if(cnt>K+1)continue;
int val=1ll*(*it1).second*(*it2).second%P;
if(tmp.find(cnt)==tmp.end())tmp[cnt]=val;
else Add(tmp[cnt],val);
}
}
mp[now]=tmp;
}
if(mp[now].find(K+1)!=mp[now].end()){
if(mp[now].find(0)!=mp[now].end()){
Add(mp[now][0],mp[now][K+1]);
}else mp[now][0]=mp[now][K+1];
}
if(mp[now].find(K)!=mp[now].end()){
if(mp[now].find(0)!=mp[now].end()){
Add(mp[now][0],mp[now][K]);
}else mp[now][0]=mp[now][K];
}
mp[now][K+1]=0;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
int n;
scanf("%d%d",&n,&K);
tot=0;for(int i=1;i<=n;i++)head[i]=0;
for(int i=1;i<n;i++){
int a,b;
scanf("%d%d",&a,&b);
Addedge(a,b);
Addedge(b,a);
}
if(K*K<=n){
dfs1(1);
printf("%d\n",dp[1][0]);
}else{
dfs2(1);
printf("%d\n",mp[1][0]);
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 12548kb
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: 0
Accepted
time: 29ms
memory: 13872kb
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 2 0 612 0 1 0 0 0 1 1 0 0 121 4536 0 0 1718 0 0 1 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 1 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:
ok 5550 lines
Test #3:
score: 0
Accepted
time: 165ms
memory: 148680kb
input:
3 99990 259 23374 69108 82204 51691 8142 67119 48537 97966 51333 44408 33147 68485 21698 86824 15746 58746 78761 86975 58449 61819 69001 68714 25787 2257 25378 14067 64899 68906 29853 31359 75920 85420 76072 11728 63836 55505 43671 98920 77281 25176 40936 66517 61029 61440 66908 52300 92101 59742 69...
output:
259200 247 207766300
result:
ok 3 lines
Test #4:
score: 0
Accepted
time: 119ms
memory: 30784kb
input:
3 99822 332 11587 83046 63424 60675 63423 73718 74622 40130 5110 26562 28361 80899 30886 70318 8708 11068 34855 96504 7904 75735 31904 42745 87892 55105 82374 81319 77407 82147 91475 12343 13470 95329 58766 95716 83232 44156 75907 92437 69785 93598 47857 33018 62668 31394 24238 72675 98254 43583 180...
output:
315881300 4505040 185631154
result:
ok 3 lines
Test #5:
score: 0
Accepted
time: 98ms
memory: 30776kb
input:
3 99021 1000 41739 4318 72541 76341 31227 15416 49232 13808 50837 51259 74464 11157 92684 84646 95226 64673 74155 82511 33301 31373 5901 29318 38227 98893 96752 57411 35167 42401 24344 90803 6956 33753 51120 24535 29594 2646 70305 32961 93079 38070 49273 48987 62799 77986 94353 84447 74970 31546 263...
output:
917568 1 1213
result:
ok 3 lines
Test #6:
score: -100
Wrong Answer
time: 681ms
memory: 98352kb
input:
3 100000 10000 15556 26349 14984 68012 43040 63434 32168 60646 70509 38559 26238 29031 45952 19431 29510 54395 5676 59515 28220 41438 46902 56640 8221 80059 77225 66558 17473 87324 20819 35098 29515 12641 84108 41157 11223 66562 25999 95852 3790 63605 20963 15799 217 58841 61619 13324 3406 60525 693...
output:
1 1 0
result:
wrong answer 3rd lines differ - expected: '1', found: '0'