HDU 4714 Tree2cycle

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4714

/*
	一般一个点的度如果大于2的话,该点必然要断开一些连接,
	因为最终每个点的度都是2, 然后就是看点上断开那些连接了,
	其实,如果一个节点的除去父亲节点如果度大于1,例如为5的话,
	必然断开与父亲节点的那条边,且断掉其子节点的中的三条边,共删去4条边
*/
#pragma comment(linker, "/STACK:1024000000,1024000000")  
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x7fffffff
#define Max 1000000

vector<int>tree[Max+10];
int vis[Max+10];
int N,ans;

int DFS(int node){
	vis[node] = 1;
	int child_node = 0;
	for(int i = 0; i < tree[node].size(); i++){
		int temp = tree[node][i];
		if(!vis[temp]){
			child_node += DFS(temp);
		}
	}
	if(child_node >= 2){
		if(node == 1)
			ans += child_node-2;  //如果是根节点,
		else
			ans += child_node-1;  //有两个以上节点,必然需要减去childe_node-1个边
		return 0;  //与父节点的边已经断了,不再是子节点
	}
	return 1;
}

int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		scanf("%d",&N);
		for(int i = 0 ; i <= N; i ++)
			tree[i].clear();
		memset(vis,0,sizeof(vis));
		ans = 0;
		N--;
		while(N--){
			int a,b;
			scanf("%d%d",&a,&b);
			tree[a].push_back(b);
			tree[b].push_back(a);
		}
		DFS(1);
		printf("%d\n",ans*2+1);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值