社区讨论

为什么我记忆化错了求调(我可能记忆化写错了)

P11962[GESP202503 六级] 树上漫步参与者 2已保存回复 1

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@mhj93hf9
此快照首次捕获于
2025/11/03 22:44
4 个月前
此快照最后确认于
2025/11/03 22:44
4 个月前
查看原帖
CPP
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
inline int read(){
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0' || ch>'9'){
		if(ch=='-'){
			f=-1;
		}
		ch=getchar();
	}
	while(ch>='0' && ch<='9'){
		x=x*10+(ch-'0');
		ch=getchar();
	}
	return x*f;
}
const int xx=2e5+5;
vector<int> G[xx];
int ans=0;
bool pd[xx];
int jl[xx];
int dfs(int u,int size){
	if(G[u].size()==0){
		return jl[u]=1;
	}
	if(jl[u]!=0){
		return jl[u];
	}
	if(size%2==0) ans++;
	for(int i:G[u]){
		if(pd[i]) continue;
		pd[i]=true;
		jl[u]+=dfs(i,size+1);
		pd[i]=false;
	}
	return jl[u]+=1;
}
signed main(){
	n=read();
	for(int i=1;i<=n;i++){
		pd[i]=false;
	}
	for(int i=1;i<=n;i++){
		jl[i]=0;
	}
	for(int i=1;i<n;i++){
		int u,v;
		u=read(),v=read();
		G[u].push_back(v);
		G[v].push_back(u);
	}
	for(int i=1;i<=n;i++){
		pd[i]=true;
		dfs(i,0);
		pd[i]=false;
		cout<<ans<<' ';
		ans=0;
	}
	return 0;
} 

回复

1 条回复,欢迎继续交流。

正在加载回复...