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: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代码:
1 #include <bits/stdc++.h>
2 using namespace std;
3 inline int read()
4 {
5 int x=0,f=1;
6 char ch=getchar();
7 while(ch<'0'||ch>'9')
8 {
9 if(ch=='-')
10 f=-1;
11 ch=getchar();
12 }
13 while(ch>='0'&&ch<='9')
14 {
15 x=x*10+ch-'0';
16 ch=getchar();
17 }
18 return x*f;
19 }
20 inline void write(int x)
21 {
22 if(x<0)
23 {
24 putchar('-');
25 x=-x;
26 }
27 if(x>9)
28 write(x/10);
29 putchar(x%10+'0');
30 }
31 typedef pair<int,int>pi;
32 const int maxn=1e9;
33 const int N=444;
34 int score[N];
35 map<string,int>M;
36 int cnt,tcase,query;
37 string s1,s2,s3;
38 string name[N];
39 vector<int>ed[N];
40 int index[N];
41 inline void BFS(int k)
42 {
43 queue<int>Q;
44 Q.push(k);
45 while(!Q.empty())
46 {
47 int u=Q.front();
48 Q.pop();
49 for(int i=0;i<ed[u].size();i++)
50 {
51 int v=ed[u][i];
52 if(score[v]<maxn)
53 continue;
54 score[v]=score[u]+1;
55 Q.push(v);
56 }
57 }
58 }
59 int pos;
60 inline bool cmp(int x,int y)
61 {
62 if(score[x]==score[y])
63 return name[x]<name[y];
64 return score[x]<score[y];
65 }
66 int main()
67 {
68 tcase=read();
69 while(tcase--)
70 {
71 pos=-1;
72 query=read();
73 M.clear();
74 cnt=0;
75 while(query--)
76 {
77 cin>>s1>>s2>>s3;
78 if(!M[s1])
79 {
80 M[s1]=++cnt;
81 name[cnt]=s1;
82 if(s1=="Ahmad")
83 pos=cnt;
84 }
85 if(!M[s2])
86 {
87 M[s2]=++cnt;
88 name[cnt]=s2;
89 if(s2=="Ahmad")
90 pos=cnt;
91 }
92 if(!M[s3])
93 {
94 M[s3]=++cnt;
95 name[cnt]=s3;
96 if(s3=="Ahmad")
97 pos=cnt;
98 }
99 int a=M[s1];
100 int b=M[s2];
101 int c=M[s3];
102 ed[a].push_back(b);
103 ed[a].push_back(c);
104 ed[b].push_back(c);
105 ed[b].push_back(a);
106 ed[c].push_back(a);
107 ed[c].push_back(b);
108 }
109 if(pos==-1)
110 {
111 for(int i=1;i<=cnt;i++)
112 cout <<name[i]<<"undefinedn"<<endl;
113 continue;
114 }
115 for(int i=1;i<=cnt;i++)
116 score[i]=maxn;
117 score[pos]=0;
118 BFS(pos);
119 for(int i=1;i<=cnt;i++)
120 index[i]=i;
121 sort(index+1,index+1+cnt,cmp);
122 write(cnt);
123 printf("n");
124 for(int j=1;j<=cnt;j++)
125 {
126 int i=index[j];
127 cout<<name[i]<<" ";
128 if(score[i]==maxn)
129 cout<<"undefined"<<endl;
130 else write(score[i]),cout<<endl;
131 }
132 for(int i=1;i<=cnt;i++)
133 ed[i].clear();
134 }
135 return 0;
136 }
- Android4.0 声卡配置-高通msm8916移植
- PHP安全:session劫持的防御
- 函数式非凡的抽象能力
- 设备树的interrupt
- gitignore文件的作用
- 《Redis设计与实现》读书笔记(十八) ——Redis客户端属性设计与原理
- 使用Python Pandas处理亿级数据
- PHP函数
- Spray中的Authentication和JMeter测试
- 《Redis设计与实现》读书笔记(二十一) ——Redis服务器定时函数serverCron详解
- Spark SQL访问Postgresql
- sysfs_create_group创建sysfs接口
- apache服务器配置
- Android i2c-tools移植
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 微信头像加国旗
- FlutterDojo设计之道—状态管理之路(四)
- FlutterDojo设计之道—状态管理之路(五)
- Python高级进阶(一)Python框架之Django入门
- python 自动化测试(2):针对密码输入框为密码控件时的解决方案
- 微信小程序开发之视图容器swiper
- WINDOWS服务器同时安装多个版本的MYSQL的方法,MQYSQL5和MQYSQL8的共存。
- 使用PHP对接国外的PAYPAL支付最新REST API接口。
- golang 学习(10): 使用go语言调用c语言的so动态库
- Django的form,model自定制
- 排序算法与查找算法
- nginx学习(1): openresty安装
- 2020年8月26日更新WINDOWS服务器同时安装多个版本的MYSQL的方法,MQYSQL5和MQYSQL8的共存。
- python 自动化测试(3):数据库连接与使用
- 微信小程序实战开发二:微信小程序 WEUL组件之 画廊组件