F. Contestants Ranking

time limit per test:1 second
memory limit per test:24 megabytes
input:standard input
output:standard output

Ahmad is one of the best students in HIAST, and also a very good problems Solver. In the time you will spend reading this problem statement Ahmad would have solved a problem. Maybe, even two... Ahmad participated so many times in programming contest (ACM-HCPC) with several teams. many of the former and present contestants at HIAST have known Ahmad for quite a few years. Some of them are proud to say that they either played in the same team with him or played in the same team with one of his teammates... Let us define ranking number as follows. Ahmad's ranking is 0, for people who played in the same team with him, the ranking is 1. For people who never played with Ahmad but played in the same team with one or more of his teammates, the ranking is 2, and so on. Your task is to automate the process of calculating the ranking numbers for each contestant at HIAST.

Input

The first line of input file contains the number of test cases (0<T<10). Each test case will begin with one line containing the number of teams (1<N ≤ 100). In each of the following N lines you are given the names of the three members of the corresponding team. Each name is a nonempty string contains only English letters starts with capital letter, and its length is at most 20 symbols. Same student can be found in more than one team, but each student should have only one rank. Ahmad will be found in one team at least. The first letter of a name is capital and the other letters are lowercase and each name will consist of only one word.

Output

For each test case output a line with the number of contestants, then for each contestant output a line with his name and his ranking. If the ranking is undefined, output “undefined” instead of it. The contestants must be ordered by rank from 0 to undefined and then lexicographical by name.

Examples
Input
2
1
Ahmad Mousaab Khalid
7
Ahmad Mousaab Khalid
Ali Mousaab Nizar
Ali Bassel Nizar
Kassem Ahmad Mousaab
Saeed Kassem Fadel
Salwa Saeed Samer
Mona Abdo Qussi
Output
3
Ahmad 0
Khalid 1
Mousaab 1
14
Ahmad 0
Kassem 1
Khalid 1
Mousaab 1
Ali 2
Fadel 2
Nizar 2
Saeed 2
Bassel 3
Salwa 3
Samer 3
Abdo undefined
Mona undefined
Qussi undefined

题目链接:http://codeforces.com/gym/100952/problem/F

题意:Ahmad是最厉害的acmer,他的排名是0,所有与他组队打比赛的人的排名都是1,没有和他组过队但是和排名为1的人组过对的人排名为2,以此类推求出所有可以求得排名的人的排名,按排名小到大的顺序输出,如果排名相同按字典序小到大输出,最后按名字字典序小到达输出不能求得排名的

下面给出AC代码:

 #include <bits/stdc++.h>
using namespace std;
inline int read()
{
int x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')
f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
inline void write(int x)
{
if(x<)
{
putchar('-');
x=-x;
}
if(x>)
write(x/);
putchar(x%+'');
}
typedef pair<int,int>pi;
const int maxn=1e9;
const int N=;
int score[N];
map<string,int>M;
int cnt,tcase,query;
string s1,s2,s3;
string name[N];
vector<int>ed[N];
int index[N];
inline void BFS(int k)
{
queue<int>Q;
Q.push(k);
while(!Q.empty())
{
int u=Q.front();
Q.pop();
for(int i=;i<ed[u].size();i++)
{
int v=ed[u][i];
if(score[v]<maxn)
continue;
score[v]=score[u]+;
Q.push(v);
}
}
}
int pos;
inline bool cmp(int x,int y)
{
if(score[x]==score[y])
return name[x]<name[y];
return score[x]<score[y];
}
int main()
{
tcase=read();
while(tcase--)
{
pos=-;
query=read();
M.clear();
cnt=;
while(query--)
{
cin>>s1>>s2>>s3;
if(!M[s1])
{
M[s1]=++cnt;
name[cnt]=s1;
if(s1=="Ahmad")
pos=cnt;
}
if(!M[s2])
{
M[s2]=++cnt;
name[cnt]=s2;
if(s2=="Ahmad")
pos=cnt;
}
if(!M[s3])
{
M[s3]=++cnt;
name[cnt]=s3;
if(s3=="Ahmad")
pos=cnt;
}
int a=M[s1];
int b=M[s2];
int c=M[s3];
ed[a].push_back(b);
ed[a].push_back(c);
ed[b].push_back(c);
ed[b].push_back(a);
ed[c].push_back(a);
ed[c].push_back(b);
}
if(pos==-)
{
for(int i=;i<=cnt;i++)
cout <<name[i]<<"undefined\n"<<endl;
continue;
}
for(int i=;i<=cnt;i++)
score[i]=maxn;
score[pos]=;
BFS(pos);
for(int i=;i<=cnt;i++)
index[i]=i;
sort(index+,index++cnt,cmp);
write(cnt);
printf("\n");
for(int j=;j<=cnt;j++)
{
int i=index[j];
cout<<name[i]<<" ";
if(score[i]==maxn)
cout<<"undefined"<<endl;
else write(score[i]),cout<<endl;
}
for(int i=;i<=cnt;i++)
ed[i].clear();
}
return ;
}

Gym 100952F&&2015 HIAST Collegiate Programming Contest F. Contestants Ranking【BFS+STL乱搞(map+vector)+优先队列】的更多相关文章

  1. Gym 100952E&&2015 HIAST Collegiate Programming Contest E. Arrange Teams【DFS+剪枝】

    E. Arrange Teams time limit per test:2 seconds memory limit per test:64 megabytes input:standard inp ...

  2. Gym 100952I&&2015 HIAST Collegiate Programming Contest I. Mancala【模拟】

    I. Mancala time limit per test:3 seconds memory limit per test:256 megabytes input:standard input ou ...

  3. Gym 100952H&&2015 HIAST Collegiate Programming Contest H. Special Palindrome【dp预处理+矩阵快速幂/打表解法】

    H. Special Palindrome time limit per test:1 second memory limit per test:64 megabytes input:standard ...

  4. Gym 100952G&&2015 HIAST Collegiate Programming Contest G. The jar of divisors【简单博弈】

    G. The jar of divisors time limit per test:2 seconds memory limit per test:64 megabytes input:standa ...

  5. Gym 100952D&&2015 HIAST Collegiate Programming Contest D. Time to go back【杨辉三角预处理,组合数,dp】

    D. Time to go back time limit per test:1 second memory limit per test:256 megabytes input:standard i ...

  6. Gym 100952C&&2015 HIAST Collegiate Programming Contest C. Palindrome Again !!【字符串,模拟】

    C. Palindrome Again !! time limit per test:1 second memory limit per test:64 megabytes input:standar ...

  7. Gym 100952B&&2015 HIAST Collegiate Programming Contest B. New Job【模拟】

    B. New Job time limit per test:1 second memory limit per test:64 megabytes input:standard input outp ...

  8. Gym 100952A&&2015 HIAST Collegiate Programming Contest A. Who is the winner?【字符串,暴力】

    A. Who is the winner? time limit per test:1 second memory limit per test:64 megabytes input:standard ...

  9. Gym 100952J&&2015 HIAST Collegiate Programming Contest J. Polygons Intersection【计算几何求解两个凸多边形的相交面积板子题】

    J. Polygons Intersection time limit per test:2 seconds memory limit per test:64 megabytes input:stan ...

随机推荐

  1. 如何在MQ中实现支持任意延迟的消息?

    什么是定时消息和延迟消息? 定时消息:Producer 将消息发送到 MQ 服务端,但并不期望这条消息立马投递,而是推迟到在当前时间点之后的某一个时间投递到 Consumer 进行消费,该消息即定时消 ...

  2. bzoj 2427: [HAOI2010]软件安装

    Description 现在我们的手头有N个软件,对于一个软件i,它要占用Wi的磁盘空间,它的价值为Vi.我们希望从中选择一些软件安装到一台磁盘容量为M计算机上,使得这些软件的价值尽可能大(即Vi的和 ...

  3. sort 命令详解

    sort  作用:将文本文件内容加以排序,sort可针对文本文件的内容,以行为单位来排序  参数: -b 忽略每行前面开始出的空格字符. -c 检查文件是否已经按照顺序排序. -d 排序时,处理英文字 ...

  4. lesson - 4 笔记 /inode / suid / sgid / sbit / chmod /umask / chown / rwx / wc /grep / tr / sort / cut /which / whereis / locate / find / ln /

    一.帮助+基本文件管理+用户管理 1.怎么查看命令帮助 ls --help man ls :查看命令/man 5 file:查看配置文件 二.基本文件管理,通过{查,建,删,改} 四个维度介绍了不同的 ...

  5. Python第二十天 shutil 模块 zipfile tarfile 模块

    Python第二十天  shutil 模块  zipfile   tarfile 模块 os文件的操作还应该包含移动 复制  打包 压缩 解压等操作,这些os模块都没有提供 shutil 模块shut ...

  6. C# 多线程传递多个参数

    http://www.cnblogs.com/lvdongjie/p/5416883.html 3. 方式三:采用lambda表达式 对于lambda表达式不熟悉的可以查看微软MSDN上的说明文档.此 ...

  7. 鸟哥的linux私房菜学习-(五)Linux系统的在线求助man page与info page

    1.man page man是manual(操作说明)的简写啦!只要下达:『man date』 马上就会有清楚的说明出现在你面前喔!如下所示: 进入man命令的功能后,你可以按下『空格键』往下翻页,可 ...

  8. 砸黑板! 正则表达式!!!re 模块

    模块是什么? 一个模块就是一个包含了 python 定义和声明的文件,文件名就是模块名字加上.py 的后缀. 但其实 import 加载的模块分为四个通用类别: 1:使用 python 编写的代码(. ...

  9. PHP动态图像处理

    相关代码见:https://www.github.com/lozybean/learn_www/ 目录 1. 画布管理:     imagecreate():创建一个基于调色板的画布,指定画布的长.宽 ...

  10. 物流包裹一站式查询(TrackingMore)

    快递查询接口 目前提供快递查询的接口平台有: Trackingmore 快递100 快递网 不同接口的区别: (1)Trackingmore支持380家快递公司,其中有55家为国内的快递,其余325家 ...