80分求大佬证明错误
  • 板块P3915 树的分解
  • 楼主聊机
  • 当前回复1
  • 已保存回复1
  • 发布时间2022/7/10 19:16
  • 上次更新2023/10/27 21:11:58
查看原帖
80分求大佬证明错误
290959
聊机楼主2022/7/10 19:16

由于我太弱了,现在连黄题都切不了了

#include<bits/stdc++.h>
using namespace std;
inline int qr(){
	int k=0;bool f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=0;ch=getchar();}
	while(isdigit(ch)){k=(k<<1)+(k<<3)+(ch^48);ch=getchar();}
	return f?k:~k+1;
}
int n,k;
struct Edge{
	int to,next;
}e[100002];
int h[100002],tot;
inline void add(int u,int v) {
	e[++tot].next=h[u];
	e[tot].to=v;
	h[u]=tot;
}
int siz[100002],f[100002];
bool build(int x) {
	int cnt=1;siz[x]=1;
	for(int i=h[x];i;i=e[i].next) {
		if(f[x]==e[i].to) continue;
		f[e[i].to]=x;
		if(!build(e[i].to)) return false;
		siz[x]+=siz[siz[e[i].to]];
		cnt+=siz[e[i].to]%k;
	}
	if(cnt>k) return false;
	return true;
}
int main(){
	int T=qr();
	while(T--) {
		for(;tot;tot--) {
			e[tot].next=e[tot].to=0;
		}
		for(int i=1;i<=n;i++) h[i]=siz[i]=f[i]=0;
		n=qr(),k=qr();
		for(int i=1,u,v;i<n;i++) {
			u=qr(),v=qr();
			add(u,v);add(v,u);
		}
		if(n%k) puts("NO");
		else {
			if(build(1)) puts("YES");
			else puts("NO");
		}
	}
	return 0;
}

思路是这样的:就是正常建树,特判n%k,然后对于每个节点如果它每个子树大小取余k的和加1大于k那么不可行,否则可行。

2022/7/10 19:16
加载中...