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 ...
随机推荐
- bitnami redmine每日自动备份
主要思路:在半夜时停止服务,进行完整备份,然后再开启服务. 1.主脚本backup.bat: call backup-stopserver.batping /n 20 127.1 >nul ca ...
- php中环境变量
$_ENV $_ENV是一个包含服务器端环境变量的数组.它是PHP中一个超级全局变量,我们可以在PHP 程序的任何地方直接访问它. $_ENV只是被动的接受服务器端的环境变量并把它们转换为数组元素 ...
- JSON对象转换问题
今天调用别人接口遇到一个问题,原本约定的data格式为:JSON字符串,但本次返回了一个空字符串"", 大概是这样的 字符串类型的httpResult.data值为字result: ...
- 65279 !!!BOM
java.lang.NumberFormatException: For input string: "1".莫名其妙的String的第一个字符,是个空格样的东西,但绝对不是空格, ...
- activiti 中文文档
http://www.mossle.com http://www.mossle.com/docs/activiti/index.html
- wkhtmltopdf 安装使用笔记(CentOS6)
1. 在官网下载安装文件. http://wkhtmltopdf.org/ 安装时如果提示某些库找不到的话,使用yum安装即可. 2. 命令行测试 $ wkhtmltopdf http://news. ...
- mac上eclipse用gdb调试(转)
mac上eclipse用gdb调试 With its new OS release, Apple has discontinued the use of GDB in OS X. Since 2005 ...
- BW顾问必需要清楚的:时间相关数据建模场景需求分析
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- IE9 打不开界面也不报错,只有打开控制台才会显示 - console
IE9下,打开界面不报错也不展示数据,打开控制台时就能加载出数据,有可能是代码中有console
- Log4j源代码学习
了解log4j的源代码来源于项目中一次需求,我们想将系统所有的warn日志统一收集到common-warn.log的日志中去,以便于系统对其进行监控处理.于是模拟自动生成的error配置完成了warn ...