【codechef】Jovians from Jupiter(父亲和孩子不能同时上船)

本文深入探讨了遥远星球Jovians如何利用其先进科技,通过复杂的家庭关系网络,最大效率地派遣成员到地球进行生命互动考察。通过分析家庭结构和旅行限制,我们发现了一种巧妙的方法来确定他们能派遣的最大人数。

Jovians, the citizens of Jupiter, have a very long life, and all the Jovians are a part of one family. Since they have a very long life, in order to control population, all the Jovians are allowed to have at most 2 children and not more.

Jovians recently discovered life on earth. Since, they are very friendly, they want to send a group to interact with new species found on earth.

They have fuel for only one round trip. They want to send as many as Jovians as possible but the technology they use restrict them to do so.

The technology of their space ship does not allow a father and his child to travel. That is, if A is the father of B, then A and B cannot travel together in the space ship.

Since they are not very smart, they need your help to find out the maximum number of Jovians they can send.

Input

First line contains number of test cases.

The first line of every test case is N, the total number of Jovians.

The next N-1 lines contains two space separated integers A and B, where B is the father of A.

Tips: 0th Jovian is the supreme and all other are his descendants.

 

Output

A single integer output for every test case denoting the maximum number of Jovians they can send.

 

Constraints

  • 1 ≤ T ≤ 200
  • 1 ≤ N ≤ 5000
  • 0 < A < N
  • 0 ≤ B < N

 

Example

Input:
1
4
1 0
2 0
3 2

Output:
2

 

Explanation

Example case 1 : Here 0 is the supreme Jovian, 1 and 2 are his child, and 3 is the child of 2, therefore they can send maximum 2 Jovians.

They can send either 0 and 3, 1 and 2 or 1 and 3.

(父亲和孩子不能同时上船,求最多几人上船)(巧妙方法,因为一个家庭里面孩子数量肯定大于等于父亲)实际上画出来是一棵树

树的结构,所以肯定下一层>=上一层。因此只要找最后一层数起,每隔一层的层 

#include<iostream>  
#include<algorithm>  
#include<string>  
#include<map>  
#include<string.h>  
#include<vector>  
#include<cmath>  
#include<stdlib.h>  
#include<cstdio>  
#define ll long long  
using namespace std;
int main(){
	int t,n,a[5001],b,d,count,c[5001];
	cin>>t;
	while(t--){
		count=0;
		cin>>n;
		for(int i=0;i<n-1;++i){
			cin>>b>>d;
			a[b]=d; // d是b的父节点 
		}
		memset(c,0,sizeof(c));
		for(int i=n-1;i>=0;--i){
			if(c[i]==0){   //0是最后一层开始每隔一层的层 
				++count;
				c[a[i]]=1; //1就是最后第二层开始每隔一层的层 
			}
		}
		cout<<count<<endl;
	}
	return 0;
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值