QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#300096#7895. Graph Partitioning 2LogingWA 60ms25720kbC++142.6kb2024-01-07 17:33:332024-01-07 17:33:35

Judging History

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

  • [2024-01-07 17:33:35]
  • 评测
  • 测评结果:WA
  • 用时:60ms
  • 内存:25720kb
  • [2024-01-07 17:33:33]
  • 提交

answer

#include <bits/stdc++.h>

#define Max 100005
#define int long long

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+2);i>=0;i--){
			for(int j=0;j<=siz&&i+j<=k+2;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+2;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=N1+1;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;
            F[u][i]=f[u][i];
		}else if(now==k){
			F[u][i+1]=(F[u][i+1]+f[u][i])%mod;
            F[u][i]=f[u][i];
			g[u][i+1]=g[u][i];
			p=max(p,i+1);
		}else if(now>k+1){
			F[u][i]=0;
		}else if(now<0){
            F[u][i]=0;
        }else{
            F[u][i]=f[u][i];
        }
		// cout<<"!"<<u<<" "<<i<<" "<<g[u][i]<<" "<<now<<endl;
	}
    for(int i=N1+1;i>=0;i--){
        f[u][i]=F[u][i];
        F[u][i]=0;
    }
	// for(int i=0;i<=p+1;i++){
	// 	cout<<u<<" "<<i<<" "<<f[u][i]<<" "<<g[u][i]<<endl;
	// }
	return p;
}

signed 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]%mod<<'\n';
		}else{
			N1=n/k;
			N2=n%k;
			N1-=N2;
			dfs1(1,1);
			int now=n-N1*k-g[1][N1]*(k+1);
//			cout<<now<<" "<<f[1][N1]<<" "<<g[1][N1]<<endl;
			if(now){
				cout<<0<<'\n';
			}else{
				cout<<f[1][N1]%mod<<'\n';
			}
		}
		for(int j=0;j<350;j++){
			for(int i=1;i<=n;i++){
				f[i][j]=g[i][j]=0;
			}
		}
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 12060kb

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: 60ms
memory: 25720kb

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:

wrong answer 618th lines differ - expected: '1', found: '2'