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 ;
}

POJ 2021 Relative Relatives的更多相关文章

  1. poj 2021 Relative Relatives(暴力)

    题目链接:http://poj.org/problem?id=2021 思路分析:由于数据较小,采用O(N^2)的暴力算法,算出所有后代的年龄,再排序输出. 代码分析: #include <io ...

  2. POJ 2021 Relative Relatives(map+树的遍历)

    题意: 今天是Ted的100岁生日.凑巧的是,他家族里面每个人都跟他同一天生日,但是年份不同. 现在只给出一些 父亲的名字,孩子的名字,以及孩子出生时父亲的年龄, 要求将Ted以外的家族成员按年龄降序 ...

  3. POJ 2407:Relatives(欧拉函数模板)

    Relatives AC代码 Relatives Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16186   Accept ...

  4. POJ 2021

    #include <iostream> #include <string> #include <algorithm> #define MAXN 105 using ...

  5. 【POJ 2407】 Relatives

    [题目链接] 点击打开链接 [算法] 欧拉函数 [代码] #include <algorithm> #include <bitset> #include <cctype& ...

  6. 【poj 2407】Relatives(数论--欧拉函数 模版题)

    题意就是求10^9以内的正整数的欧拉函数(Φ(n)表示<=n的与n互质的正整数个数). 解法:用欧拉筛和欧拉函数的一些性质:    1.若p是质数,Φ(p)=p-1:    2.欧拉函数是积性函 ...

  7. HOJ题目分类

    各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...

  8. OJ题解记录计划

    容错声明: ①题目选自https://acm.ecnu.edu.cn/,不再检查题目删改情况 ②所有代码仅代表个人AC提交,不保证解法无误 E0001  A+B Problem First AC: 2 ...

  9. 这些MongoDB的隐藏操作你真的都掌握了吗?反正我是刚知道

    背景 最近公司系统还原用户时偶尔会出现部分用户信息未还原成功的问题,最为开发人员,最头疼的不是代码存在bug,而是测试发现了bug,但一旦我去重现,它就不见了.Are you kidding me? ...

随机推荐

  1. ABAP vs Java, 蛙泳 vs 自由泳

    去年7月定下的一年之内学会自由泳的目标终于实现了,特来还愿. ABAP和Java, 蛙泳和自由泳.前面的组合是Jerry用来挣钱养家的技术,后者是Jerry花了大量业余时间和金钱苦练的技能.或许有的朋 ...

  2. python_88_xml模块

    xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单例如创建xmltest.xml文件内容如上 注:/代表自结束符号 <?xml version=&quo ...

  3. Tomcat详细安装配置

    1.首先是Tomcat的获取和安装. 获取当然得上Apache的官方网站下载,开源免费,而且带宽也足够.下载会很快. 这是两种不同的下载,一个是普通安装版本,一个是解压安装版本.使用起来是一样的,只是 ...

  4. JAVA实现webSocket网页聊天室

    一.什么是webSocket WebSocket 是一种网络通信协议,是持久化协议.RFC6455 定义了它的通信标准. WebSocket 是 HTML5 开始提供的一种在单个 TCP 连接上进行全 ...

  5. Find the Longest Word in a String-freecodecamp算法题目

    Find the Longest Word in a String(找出最长单词) 要求 在句子中找出最长的单词,并返回它的长度 函数的返回值应该是一个数字. 思路 用.split(' ')将句子分隔 ...

  6. [BZOJ] 1520: [POI2006]Szk-Schools

    费用流解决. abs内传不了int..CE一次 #include<iostream> #include<cstring> #include<cstdio> #inc ...

  7. 02等待单个线程返回WaitForSingleObject

    windows 多线程之等待线程返回 ​ 多线程编程中,有时我们需要等待某一线程完成了特定的操作之后再继续做其他事情,要实现这个目的,可以使用 Windows API 函数 WaitForSingle ...

  8. 如何用纯 CSS 创作一副国际象棋

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/WyXrjz 可交互视频 ...

  9. React 基础知识总结

    data-id="1190000016885142" data-license=""> 一.Node.js Node.js并不是一个JavaScript框 ...

  10. STM32CUBEMX入门学习笔记2:关于STM32芯片使用内部flash

    找到正点原子的官网,下载他的HAL库:http://www.openedv.com/thread-109778-1-1.html 找到此例程,并打开其工程文件. 找到此文件,复制到自己工程里 复制到自 ...