POJ 2021 Relative Relatives
2011-08-07 13:56
176 查看
//============================================================================
// Name : hello.cpp
// Author : key
// Version : 8
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <string>
#include <algorithm>
using namespace std;
#define NUM_INF 0XFFFFF
struct Node{
char fa[20];
char names[20];
int born;
int age;
}m[110];
int n;
int num;
bool cmp(Node &a, Node &b){
if(a.age > b.age) return true;
if(a.age == b.age && strcmp(a.names, b.names) < 0)
return true;
return false;
}
void dfs(char* str,int age)
{
if(num>=n)
return;
for(int i=0;i<n;i++)
{
if(m[i].age!=-1)
continue;
if(strcmp(str,m[i].fa)==0)
{
num++;
m[i].age = age - m[i].born;
dfs(m[i].names,m[i].age);
}
}
}
int main()
{
int t;
int cases=0;
int i;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%s%s%d",&m[i].fa,&m[i].names,&m[i].born);
m[i].age = -1;
}
num=0;
for(i=0;i<n;i++)
{
if(strcmp("Ted",m[i].fa)==0)
{
num++;
m[i].age = 100 - m[i].born;
dfs(m[i].names,m[i].age);
}
}
sort(m,m+n,cmp);
printf("DATASET %d\n",cases++);
for(i=0;i<n;i++)
printf("%s %d\n",m[i].names,m[i].age);
}
return 0;
}
// Name : hello.cpp
// Author : key
// Version : 8
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <string>
#include <algorithm>
using namespace std;
#define NUM_INF 0XFFFFF
struct Node{
char fa[20];
char names[20];
int born;
int age;
}m[110];
int n;
int num;
bool cmp(Node &a, Node &b){
if(a.age > b.age) return true;
if(a.age == b.age && strcmp(a.names, b.names) < 0)
return true;
return false;
}
void dfs(char* str,int age)
{
if(num>=n)
return;
for(int i=0;i<n;i++)
{
if(m[i].age!=-1)
continue;
if(strcmp(str,m[i].fa)==0)
{
num++;
m[i].age = age - m[i].born;
dfs(m[i].names,m[i].age);
}
}
}
int main()
{
int t;
int cases=0;
int i;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%s%s%d",&m[i].fa,&m[i].names,&m[i].born);
m[i].age = -1;
}
num=0;
for(i=0;i<n;i++)
{
if(strcmp("Ted",m[i].fa)==0)
{
num++;
m[i].age = 100 - m[i].born;
dfs(m[i].names,m[i].age);
}
}
sort(m,m+n,cmp);
printf("DATASET %d\n",cases++);
for(i=0;i<n;i++)
printf("%s %d\n",m[i].names,m[i].age);
}
return 0;
}
相关文章推荐
- POJ 2021 BFS+大量STL。。
- poj 2021 Relative Relatives(典型数据结构题)
- POJ 2021
- POJ 2021 Relative Relatives
- POJ 2021
- [BZOJ1982][POJ1740][Spoj 2021]Moving Pebbles|解题报告
- poj 2021 Relative Relatives
- POJ 2021 Relative Relatives(map+树的遍历)
- poj 2021 Relative Relatives
- poj 2021 优先队列bfs
- poj 2021 Relative Relatives
- Poj 2021 Relative Relatives
- poj 2021 Relative Relatives 排序
- poj1218——THE DRUNK JAILER
- poj水题
- poj 2411 状态压缩
- poj 2250Compromise
- poj 1260
- POJ 2653 Pick-up sticks
- Poj 1125 Stockbroker Grapevine(Floyd算法求结点对的最短路径问题)