其他分享
首页 > 其他分享> > CF1375G Tree Modification

CF1375G Tree Modification

作者:互联网

Description

Solution

**#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define maxn 200005
using namespace std;

int n,i,j,k,ans,sum;
int em,e[maxn*2],nx[maxn*2],ls[maxn];

void insert(int x,int y){
	em++; e[em]=y; nx[em]=ls[x]; ls[x]=em;
	em++; e[em]=x; nx[em]=ls[y]; ls[y]=em;
}

void dfs(int x,int p,int d){
	sum+=d&1^1;
	for(int i=ls[x];i;i=nx[i]) if (e[i]!=p)
		dfs(e[i],x,d+1);
}

int main(){
	freopen("ceshi.in","r",stdin);
	scanf("%d",&n);
	for(i=1;i<n;i++) scanf("%d%d",&j,&k),insert(j,k);
	dfs(1,0,0);
	ans=min(sum,n-sum)-1;
	printf("%d\n",ans);
}

标签:em,奇数,int,Tree,ls,深度,Modification,CF1375G,节点
来源: https://blog.csdn.net/qq_43649416/article/details/111304363