【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.
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.
A single integer output for every test case denoting the maximum number of Jovians they can send.
1 ≤ T ≤ 200
1 ≤ N ≤ 5000
0 < A < N
0 ≤ B < N
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.
(父亲和孩子不能同时上船,求最多几人上船)(巧妙方法,因为一个家庭里面孩子数量肯定大于等于父亲)实际上画出来是一棵树
树的结构,所以肯定下一层>=上一层。因此只要找最后一层数起,每隔一层的层
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 ≤ 2001 ≤ 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; }
相关文章推荐
- 解决Response.Redirect和 Response.Write不能同时使用
- 同一个帐号不能同时登陆的问题
- Permission error with creating a team project from VS 2010 on TFS 2012 tfs2012在vs2010下不能创建团队项目的问题
- 限制一个帐户不能同时登陆。(针对于不是同一台计算机)
- 当孩子伸手向你要钱时,千万不能这样做,一辈子的阴影。
- 父亲给孩子的备忘录
- OEM 12c不能正常启动,同时repository数据库大量library cache lock等待事件
- mysql in和limit不能同时使用
- Treeview 中1.选择父节点时子节点同时被选中 反之...... 2.每个节点只能选一个且不能影响其他项
- 【转译】希望将来孩子评价父亲的35句话
- MySQL SELECT同时UPDATE同一张表 You can't specify target table 'sys_user' for update in FROM clause
- 解决opencv中cvCaptureFromFile不能打开一些视频文件
- FastMM4 在三层插件框架中不能使用解决办法(IsMultiThread' from unit 'FastMM4)
- transform属性不能同时写scale和rotate,怎么办??
- resultType跟resultMap不能同时存在
- (HDU 1520)Anniversary party <树型DP> 公司开派对,上下级不能同时到
- 乐观锁(两个以上用户不能同时进行同一操作)
- VueJS ElementUI el-table 的 formatter 和 scope template 不能同时存在
- fcitx 关于多语言,不同编码的一点说明;同时也可以用于解决fcitx不能输入问题
- iis里不能同时启动多个站点的原因总结: