poj 2732 Countdown(East Central North America 2005)
题意:建一个家庭树,找出有第d代子孙的名字,按照要求的第d代子孙的数从大到小输出三个人名,如果有一样大小子孙数的,就按字母序从小到大将同等大小的都输出,如果小于三个人的就全输出。
题目链接:http://poj.org/problem?id=2732
分析:数据量很小,直接建树,枚举暴力输出。我直接套模板用map建树的。
注意:1.每次测试数据时要清空map和vector!!!
2.注意输出要从大到小输出答案,如果答案有一样的按名字的字母序从小到大输出。答案用vector保存,vector排序:sort(v.begin(),v.end(),cmp);
3.注意嵌套模板的使用,map<string,vector<string> >fa;
另,关于建树,可以直接用int类型代替名字的string类型,给每个名字赋予一个编号。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
string name[];
map<string,int>mp;
map<string,vector<string> >fa;
vector<pair<string,int> >ans;
vector<string>v;
int fin(string na,int gen)
{
int ans=;
if(gen==)
return ;
for(int i=;i<mp[na];i++)
{
if(gen>=)
ans+=fin(fa[na][i],gen-);
}
return ans;
}
int cmp(pair<string,int>p1,pair<string,int>p2)
{
if(p1.second==p2.second)
return p1.first<p2.first;
return p1.second>p2.second;
}
int main()
{
int t,cnt=;
cin>>t;
while(t--)
{
fa.clear(); //每次样例注意清空数据
ans.clear();
mp.clear();
int n,d;
cin>>n>>d;
int num;
for(int i=;i<n;i++)
{
cin>>name[i];
cin>>num;
string child;
mp[name[i]]=num;
v.clear();
for(int j=;j<num;j++)
{
cin>>child;
v.push_back(child);
}
fa[name[i]]=v; //嵌套模板的使用
}
for(int i=;i<n;i++)
{
int res=fin(name[i],d);
if(res>)
ans.push_back(make_pair(name[i],res));
}
int len=ans.size();
sort(ans.begin(),ans.end(),cmp);
printf("Tree %d:\n",cnt);
for(int i=;i<len;i++) //注意答案的输出
{
if((i>)&&ans[i].second!=ans[i-].second)
break;
cout<<ans[i].first<<" "<<ans[i].second<<endl;
}
cnt++;
}
return ;
}
poj 2732 Countdown(East Central North America 2005)的更多相关文章
- MPI Maelstrom(East Central North America 1996)(poj1502)
MPI Maelstrom 总时间限制: 1000ms 内存限制: 65536kB 描述 BIT has recently taken delivery of their new supercom ...
- [bfs,深度记录] East Central North America Regional Contest 2016 (ECNA 2016) D Lost in Translation
Problem D Lost in Translation The word is out that you’ve just finished writing a book entitled How ...
- POJ 1240 Pre-Post-erous! && East Central North America 2002 (由前序后序遍历序列推出M叉树的种类)
题目链接 问题描述 : We are all familiar with pre-order, in-order and post-order traversals of binary trees. ...
- POJ 1240 Pre-Post-erous! && East Central North America 2002 (由前序后序遍历序列推出M叉树的种类)
题目链接:http://poj.org/problem?id=1240 本文链接:http://www.cnblogs.com/Ash-ly/p/5482520.html 题意: 通过一棵二叉树的中序 ...
- Gym-101673 :East Central North America Regional Contest (ECNA 2017)(寒假自训第8场)
A .Abstract Art 题意:求多个多边形的面积并. 思路:模板题. #include<bits/stdc++.h> using namespace std; typedef lo ...
- 2017-2018 ACM-ICPC East Central North America Regional Contest (ECNA 2017) Solution
A:Abstract Art 题意:给出n个多边形,求n个多边形分别的面积和,以及面积并 思路:模板 #include <bits/stdc++.h> using namespace st ...
- 2014-2015 ACM-ICPC East Central North America Regional Contest (ECNA 2014) A、Continued Fractions 【模拟连分数】
任意门:http://codeforces.com/gym/100641/attachments Con + tin/(ued + Frac/tions) Time Limit: 3000/1000 ...
- East Central North America 2006 Hie with the Pie /// 状压dp oj22470
题目大意: 输入n,有n个地方(1~n)需要送pizza pizza点为0点 接下来n+1行每行n+1个值 表示 i 到 j 的路径长度 输出从0点到各点送pizza最后回到0点的最短路(点可重复走) ...
- East Central North America Region 2015
E 每过一秒,当前点会把它的值传递给所有相邻点,问t时刻该图的值 #include <iostream> #include <cstdio> #include <algo ...
随机推荐
- Intellij Idea 14 使用jetty-maven-plugin配置运行web工程
在项目中接触,虽然比较简单,也是经验的积累,web工程使用maven管理和构建,IDEA也是新接触的开发工具,用了一段时间,感觉so nice! 1:Run->Edit Configuratio ...
- Oracle Partition By 的使用
1.概述 Parttion by 关键字是Oracle中分析性函数的一部分,它和聚合函数不同的地方在于它能够返回一个分组中的多条记录,儿聚合函数一般只有一条反映统计值的结果. 2.使用方式 场景:查询 ...
- js+jquery的等价用法
js: 获取属性的值: document.getElementById("id").value; 设置属性的样式: document.getElementById("id ...
- 分表的一个记录---Ruby
sql1=" UPDATE user_red_info_"sql2=" SET status = '#{status}', update_time = '#{update ...
- sqlserver2008一直显示正在还原
sqlserver2008一直显示正在还原.如果不需要还原,则使用: restore database test with recovery如果只需要还原,则使用: restore databas ...
- 20161014006 DataGridView Combobox 数据绑定与传值
//Combobox private void T_Form_CY_CBD_D_CurrentCellChanged(object sender, EventArgs e) ...
- shell crontab执行结果不同问题处理
shell脚本手工执行结果正确,但是放到crontab中执行,结果却是错误的. 脚本执行的路径肯定是没有问题的,比较怀疑crontab执行和手工执行两者的环境不同,在手工执行脚本时,通过ssh登录到服 ...
- 后端Apache获取前端Nginx反向代理的真实IP地址 (原创贴-转载请注明出处)
====================说在前面的话==================== 环境:前段Nginx是反向代理服务器:后端是Apache是WEB项目服务器 目的:让后端Apapche获取 ...
- 2017年1月2日 星期一 --出埃及记 Exodus 21:28
2017年1月2日 星期一 --出埃及记 Exodus 21:28 "If a bull gores a man or a woman to death, the bull must be ...
- 【转】T-SQL查询进阶—理解SQL Server中的锁
简介 在SQL Server中,每一个查询都会找到最短路径实现自己的目标.如果数据库只接受一个连接一次只执行一个查询.那么查询当然是要多快好省的完成工作.但对于大多数数据库来说是需要同时处理多个查 ...