Relative Relatives
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 3550 | Accepted: 1561 |
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.
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:
Note:
A single data set has 2 components:
- Descendant Count - A line containing a single integer X (where 0 < X < 100) indicating the number of Ted's descendants.
- 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.
- FNAME is the father's name.
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
Source
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<cctype>
#include<utility>
#pragma comment(linker, "/STACK:102400000,102400000")
#define PI 3.1415926535897932384626
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n) for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n) for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n) for(int i=(n) ;i>=0 ;i--)
#define lson num<<1,le,mid
#define rson num<<1|1,mid+1,ri
#define MID int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)
#define mk make_pair
#define _f first
#define _s second
using namespace std;
//const int INF= ;
typedef long long ll;
//const ll inf =1000000000000000;//1e15;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
const int INF =0x3f3f3f3f;
const int maxn= 100+10 ;
//const int maxm= ;
char name1[10000],name2[10000];
map<string,int> mp;
int root,n;
int age[maxn];
int getid(string s)
{
if(!mp.count(s))
{
int num=mp.size();
mp[s]=num+1;
}
if(s=="Ted") root=mp[s],age[root]=100;
return mp[s];
}
vector<int >nex[maxn];
int d[maxn];
void fatherson(int fa,int son,int delta)
{
d[son]=delta;
nex[fa].push_back(son);
}
void dfs(int x)
{
for(int i=0;i<nex[x].size();i++)
{
int y=nex[x][i];
age[y]=age[x]-d[y];
dfs(y);
}
}
struct Node
{
string name;
int age;
Node(){}
Node(string name ,int ag):name(name),age(ag){}
} des[maxn];
bool cmp(Node x, Node y)
{
if(x.age==y.age)
return x.name<y.name;
return x.age>y.age;
}
int main()
{
int T,x,kase=0;scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
mp.clear();
for(int i=1;i<=n+1;i++)
{
nex[i].clear();
}
memset(d,0,sizeof d);
for(int i=1;i<=n;i++)
{
scanf("%s%s%d",name1,name2,&x);
int id1=getid((string)name1);
int id2=getid((string)name2);
fatherson(id1,id2,x);
}
dfs(root);
int now=1;
for(map<string,int > ::iterator it =mp.begin();it!=mp.end();it++)
{
string name=it->first;
// cout<<name<<endl;
int id=it->second;
int ag=age[id];
des[now++]=Node(name,ag);
}
sort(des+1,des+n+2,cmp);
printf("DATASET %d\n",++kase);
for(int i=1;i<=n+1;i++)
{
if(des[i].name=="Ted") continue;
printf("%s %d\n", des[i].name.c_str() ,des[i].age);
}
}
return 0;
}