Relative Relatives
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 3339   Accepted: 1462

Description

Today is Ted's 100th birthday. A few weeks ago, you were selected by the family to contact all of Ted's descendants and organize a surprise party. To make this task easier, you created an age-prioritized list of everyone descended from Ted. Descendants of the same age are listed in dictionary order.

The only materials you had to aid you were birth certificates. Oddly enough, these birth certificates were not dated. They simply listed the father's name, the child's name, and the father's exact age when the baby was born.

Input

Input to this problem will begin with line containing a single integer n indicating the number of data sets. Each data set will be formatted according to the following description.

A single data set has 2 components: 

  1. Descendant Count - A line containing a single integer X (where 0 < X < 100) indicating the number of Ted's descendants.
  2. Birth Certificate List - Data for X birth certificates, with one certificate's data per line. Each certificate's data will be of the format "FNAME CNAME FAGE" where: 
    • FNAME is the father's name.
    • CNAME is the child's name.
    • FAGE is the integer age of the father on the date of CNAMEs birth.

Note:

  • Names are unique identifiers of individuals and contain no embedded white space.
  • All of Ted's descendants share Ted's birthday. Therefore, the age difference between any two is an integer number of years. (For those of you that are really picky, assume they were all born at the exact same hour, minute, second, etc... of their birth year.)
  • You have a birth certificate for all of Ted's descendants (a complete collection).

Output

For each data set, there will be X+1 lines of output. The first will read, "DATASET Y", where Y is 1 for the first data set, 2 for the second, etc. The subsequent X lines constitute your age-prioritized list of Ted's descendants along with their ages using the format "NAME AGE". Descendants of the same age will be listed in dictionary order.

Sample Input

2
1
Ted Bill 25
4
Ray James 40
James Beelzebub 17
Ray Mark 75
Ted Ray 20

Sample Output

DATASET 1
Bill 75
DATASET 2
Ray 80
James 40
Beelzebub 23
Mark 5
题目大意:Ted近年100岁,给出n行,每行有两个字符串和一个数字,第一个字符串为父亲的名字,后面为儿子的名字以及父亲比儿子大多少岁,输出整个家族中除了Ted之外所有人的年龄,按年龄从大到小输出,如果年龄相同,则按照名字字典序从小到大输出。
#include <stdio.h>
#include <string>
#include <map>
#include <algorithm>
#include <vector>
#include <iostream>
#include <queue>
using namespace std; typedef struct
{
int age;
string name;
}Person; //父亲和儿子名字键值对
multimap<string, string> father_son;
//儿子和儿子年龄键值对
map<string, int> son_age;
//儿子和儿子比父亲少多少岁键值对
map<string, int> person_toage;
//家族中每个成员
vector<Person> Family; bool cmp(const Person &p1, const Person &p2)
{
if (p1.age == p2.age)
{
return p1.name < p2.name;
}
else
{
return p1.age > p2.age;
}
} //用BFS遍历得出家族中每个人的年龄
void BFS()
{
queue<pair<string, int> > Queue;
Queue.push(make_pair("Ted", ));
while(!Queue.empty())
{
pair<string, int> p = Queue.front();
Queue.pop();
int nCount = father_son.count(p.first);
if (nCount != )
{
//遍历父亲的所有儿子
multimap<string, string>::iterator iter;
for (iter = father_son.lower_bound(p.first); iter != father_son.upper_bound(p.first); iter++)
{
//儿子的年龄等于父亲的年龄减去父子年龄之差
son_age[iter->second] = p.second - person_toage[iter->second];
Person temp;
temp.name = iter->second;
temp.age = son_age[iter->second];
Family.push_back(temp);
//将当前儿子入队,作为后序的父亲
Queue.push(make_pair(iter->second, son_age[iter->second]));
}
}
}
} int main()
{
int nCase, n, age, nDataset = ;
string father, son;
cin>>nCase;
while(nCase--)
{
cin>>n;
father_son.clear();
son_age.clear();
person_toage.clear();
Family.clear();
son_age.insert(make_pair("Ted", ));
++nDataset;
for (int i = ; i < n; i++)
{
cin>>father>>son>>age;
father_son.insert(make_pair(father, son));
person_toage.insert(make_pair(son, age));
}
BFS();
sort(Family.begin(), Family.end(), cmp);
cout<<"DATASET "<<nDataset<<endl;
for (vector<Person>::iterator iter = Family.begin(); iter != Family.end(); iter++)
{
cout<<iter->name<<" "<<iter->age<<endl;
}
}
return ;
}

最新文章

  1. LogStash配置、使用(三)
  2. clojure
  3. Houdini 13在Ubuntu系统下流畅运行、不崩溃
  4. JDBCTemplate.java
  5. JS控制文字一个一个出现
  6. 9.6noip模拟试题
  7. 我和小美的撸码日记(2)之第一个基于MVC+Jqgrid的列表页面
  8. KV型内存数据库Redis
  9. if_else_while_for
  10. c# WebApi之解决跨域问题:Cors
  11. elasticsearch6.7 05. Document APIs(3)GET API
  12. Android 调用系统相机拍照并获取原图
  13. zabbix准备:mysql安装
  14. 水仙花在python3在pycharm的实现
  15. MySQL在windows下的noinstall安装
  16. ABP框架入门踩坑-使用MySQL
  17. AngularJS 指令 实现文本水平滚动效果
  18. js 浅拷贝有大用
  19. ural1297 求最长回文子串 | 后缀数组
  20. Lumen rule

热门文章

  1. Python+selenium之unittest单元测试(3)关于测试用例执行的顺序
  2. HDU 5500 Reorder the Books (水题)
  3. FZU 2204 7
  4. Android(java)学习笔记136:利用谷歌API对数据库增删改查(推荐使用)
  5. 禁止DataGridView控件中添加和删除行
  6. appium---启动app
  7. string 的用法
  8. Js笔记-第11课
  9. dht 分布式hash 一致性hash区别
  10. cesium 基于天地图服务 完成底图标注渲染加切换