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
每次先确定一个等级在查找下个等级,没有等级的视为undefined
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define PI 3.141592653589793238462
#define INF 0x3f3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
string s[][],name="Ahmad";
map<string,int>m;
set<string>ans[],pos;
int t,n;
void check(int x,int y,int z)
{
if(ans[x].count(s[y][z]))
{
for(int i=;i<=;i++)
{
       if(m[s[y][(z+i)%]]==)
{
ans[x+].insert(s[y][(z+i)%]);
m[s[y][(z+i)%]]=;
}
}
}
}
void check_undefined(int n)
{
for(int i=;i<n;i++)
{
for(int j=;j<;j++)
{
if(m[s[i][j]]==) pos.insert(s[i][j]),m[s[i][j]]=;
}
}
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=;i<n;i++)
{
for(int j=;j<;j++)
{
cin>>s[i][j];
}
}
m[name]=;
ans[].insert(name);
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
for(int k=;k<;k++)
{
check(i,j,k);
}
}
}
check_undefined(n);
printf("%d\n",m.size());
for(int i=;i<=n;i++)
{
if(ans[i].size()==) break;
for(set<string>::iterator it=ans[i].begin();it!=ans[i].end();it++)
cout<<*it<<' '<<i<<endl;
}
for(set<string>::iterator it=pos.begin();it!=pos.end();it++)
cout<<*it<<" undefined"<<endl;
for(int i=;i<;i++)
ans[i].clear();
pos.clear();
m.clear();
}
return ;
}

Gym 100952 F. Contestants Ranking的更多相关文章

  1. Gym 100952F&&2015 HIAST Collegiate Programming Contest F. Contestants Ranking【BFS+STL乱搞(map+vector)+优先队列】

    F. Contestants Ranking time limit per test:1 second memory limit per test:24 megabytes input:standar ...

  2. codeforces gym 100952 A B C D E F G H I J

    gym 100952 A #include <iostream> #include<cstdio> #include<cmath> #include<cstr ...

  3. Gym 100952 H. Special Palindrome

    http://codeforces.com/gym/100952/problem/H H. Special Palindrome time limit per test 1 second memory ...

  4. Gym 100952 D. Time to go back(杨辉三角形)

    D - Time to go back Gym - 100952D http://codeforces.com/gym/100952/problem/D D. Time to go back time ...

  5. Gym 100952 G. The jar of divisors

    http://codeforces.com/gym/100952/problem/G G. The jar of divisors time limit per test 2 seconds memo ...

  6. Gym 100952 D. Time to go back

    http://codeforces.com/gym/100952/problem/D D. Time to go back time limit per test 1 second memory li ...

  7. Gym 100952 C. Palindrome Again !!

    http://codeforces.com/gym/100952/problem/C C. Palindrome Again !! time limit per test 1 second memor ...

  8. Gym 100637F F. The Pool for Lucky Ones

    F. The Pool for Lucky Ones Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...

  9. codeforces Gym 100187F F - Doomsday 区间覆盖贪心

    F. Doomsday Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/F ...

随机推荐

  1. Linux cp 复制命令

    Linux 的cp命令 功能: 复制文件或目录说明: cp指令用于复制文件或目录,如同时指定两个以上的文件或目录,且最后的目的地是一个已经存在的目录,则它会把前面指定的所有文件或目录复制到此目录中.若 ...

  2. 洛谷1034 NOIP2002 矩形覆盖

    问题描述 在平面上有 n 个点(n <= 50),每个点用一对整数坐标表示.例如:当 n=4 时,4个点的坐标分另为:p1(1,1),p2(2,2),p3(3,6),P4(0,7). 这些点可以 ...

  3. 用xmanager6启动Linux上的图形界面程序

    1.下载Xmanager6 并自行安装,这里不赘述了 2.打开Xmanager.启动Xstart 3.按提示输入:主机IP,协议,用户名,命令,完成后点击“保存”,接着点击“运行”,运行xmanage ...

  4. 云服务器 ECS Linux 系统下使用 dig 命令查询域名解析

    云服务器 ECS Linux 系统可以使用通常自带的 dig 命令来查询域名解析情况.本文对此进行简要说明. 查询域名 A 记录 命令格式: dig <域名> 比如,查询域名 www.al ...

  5. Java中发送http的get、post请求

    近期做项目中,须要把消息通过中间件的形式通过http请求的方式推送给第三方,因此用到了http协议,小编花费了一个多小时.对于http协议中的post和get请求,封装了一个工具类.以下与大家分享一下 ...

  6. PHP 做图片锐化处理

    <?php //读取图像的类型 //1=GIF,2=JPG,3=PNG,4=SWF,5=PSD,6=BMP,7=TIFF(intelbyteorder),8=TIFF(motorolabyteo ...

  7. Python使用Redis实现一个简单作业调度系统

    Python使用Redis实现一个简单作业调度系统 概述 Redis作为内存数据库的一个典型代表,已经在非常多应用场景中被使用,这里仅就Redis的pub/sub功能来说说如何通过此功能来实现一个简单 ...

  8. scikit-learn:3.2. Grid Search: Searching for estimator parameters

    參考:http://scikit-learn.org/stable/modules/grid_search.html GridSearchCV通过(蛮力)搜索參数空间(參数的全部可能组合).寻找最好的 ...

  9. c++动态库中使用命名空间的问题

    这是C++才会有的语言特性. 假如你使用一个程序库,他里面有桓霰淞拷衋bc,可是你自己也不小心定义了一个叫abc的变量,这样就会引起重定义错误.所以为了避免这样的现象,C++引入了名字空间(names ...

  10. FPGA主要应用

    FPGA主要应用: 1.逻辑控制(逻辑接口领域) 传统方向,主要用于通信设备的高速接口电路设计,用FPGA处理高速接口的协议,并完成高速的数据收发和交换.FPGA最初以及到目前最广的应用就是在通信领域 ...