您的位置:首页 > 其它

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

2015-04-21 19:40 190 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: