newwork Seoul 2007


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>
#include <map>
#include <string>
#include <climits> 
#include <set>
#include <string> 
#include <sstream>
#include <utility>
#include <ctime>
//#pragma comment(linker, "/STACK:1024000000,1024000000") 

using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::istringstream;
using std::getline;
using std::make_pair;
using std::greater;

const int MAXN(1010);
const int MAXE(2010);

int fa[1010];

struct EDGE
{
	int v;
	EDGE *next;
};

int count[MAXN];
EDGE *first[MAXN];
EDGE edge[MAXE];
EDGE *rear;

void init()
{
	memset(first, 0, sizeof(first));
	memset(count, 0, sizeof(count));
	rear = edge;
}

void insert(int tu, int tv)
{
	rear->v = tv;
	rear->next = first[tu];
	first[tu] = rear++;
	++count[tu];
}

int n, s, limit;
vector<int> node[MAXN];

void dfs1(int tf, int cur, int dep)
{
	fa[cur] = tf;
	if(count[cur] == 1 && dep > limit)
		node[dep].push_back(cur);
	for(EDGE *i = first[cur]; i; i = i->next)
		if(i->v != tf)
			dfs1(cur, i->v, dep+1);
}

bool cover[MAXN];

void dfs2(int tf, int cur, int dep)
{
	if(dep > limit)
		return;
	cover[cur] = true;
	for(EDGE *i = first[cur]; i; i = i->next)
		if(i->v != tf)
			dfs2(cur, i->v, dep+1);
}

int solve()
{
	memset(cover, 0, sizeof(cover));
	int ret = 0;
	for(int i = n-1; i > limit; --i)
	{
		int ts = node[i].size();
		for(int j = 0; j < ts; ++j)
			if(!cover[node[i][j]])
			{
				++ret;
				int tf = node[i][j];
				for(int k = 0; k < limit; ++k)
					tf = fa[tf];
				dfs2(-1, tf, 0);
			}
	}
	return ret;
}

int main()
{
	int T;
	scanf("%d", &T);
	while(T--)
	{
		init();
		scanf("%d%d%d", &n, &s, &limit);
		for(int i = 0; i <= n; ++i)
			node[i].clear();
		int tu, tv;
		for(int i = 1; i < n; ++i)
		{
			scanf("%d%d", &tu, &tv);
			insert(tu, tv);
			insert(tv, tu);
		}
		dfs1(-1, s, 0);
		printf("%d\n", solve());
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值