QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#300001#7895. Graph Partitioning 2Loging#WA 23ms12936kbC++201.7kb2024-01-07 16:01:472024-01-07 16:01:47

Judging History

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

  • [2024-01-07 16:01:47]
  • 评测
  • 测评结果:WA
  • 用时:23ms
  • 内存:12936kb
  • [2024-01-07 16:01:47]
  • 提交

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];
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 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;
	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 i=0;i<=siz+p&&i<=N1+1;i++){
			f[u][i]=F[u][i];
			F[u][i]=0;
		}
		p+=siz;
	}
	for(int i=p;i>=0;i--){
		int now=son[u]-(k+1)*g[u][i]-k*i;
		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];
		}
	}
	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';
		}
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 8164kb

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: 23ms
memory: 12936kb

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
496
322929383
424829751
1
648114622
848437854
-623392380
-593867722
942660499
847725436
1
1
1
332182034
317936728
439509110
220291830
0
3502
1032
456293131
1
1824399
454061580
1
-110755779
574639453
1
782159322
0
1
0
773052238
773052238
1
1
1183
559
14046388
-17034937
329631368
845080380
-59089305...

result:

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