QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#581552#7895. Graph Partitioning 2gggggggWA 37ms15216kbC++141.9kb2024-09-22 13:21:142024-09-22 13:21:15

Judging History

你现在查看的是最新测评结果

  • [2024-09-22 13:21:15]
  • 评测
  • 测评结果:WA
  • 用时:37ms
  • 内存:15216kb
  • [2024-09-22 13:21:14]
  • 提交

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{
        for(int i=1;i<=n;i++) for(int j=0;j<=k;j++) dp[i][j]=0;
        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
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 14044kb

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: 37ms
memory: 15216kb

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
8
1
0
1
3
0
0
1
0
1
0
0
1
0
222
0
0
0
1381
1
17
3
1
2
2
0
1044
0
1
0
0
0
1
1
0
0
344
39743328
0
0
4617
2
0
1
0
1356
1
691069
152237970
3
384
0
1
0
73
0
0
0
0
0
1
0
0
0
1
0
1
1
1
421
0
0
0
1
0
0
0
2
0
1
1
1
1
1
0
0
0
1
0
0
0
77
0
5
0
2
0
2
656
0
578
0
0
208
0
1
0
0
2
0
2
4
0
0
2
0
0
12
0
1
1
...

result:

wrong answer 2nd lines differ - expected: '3', found: '5'