Gym 100952F&&2015 HIAST Collegiate Programming Contest F. Contestants Ranking【BFS+STL乱搞(map+vector)+优先队列】
F. Contestants Ranking
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.
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.
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.
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
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)+优先队列】的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 【java】控制台实现贪吃蛇小游戏-LinkedList、Scanner
package com.myproj.snake; public class Node { private int i,j; public Node(){} public Node(int i, in ...
- JavaScript 中 apply 、call 的详解
apply 和 call 的区别 ECMAScript 规范给所有函数都定义了 call 与 apply 两个方法,它们的应用非常广泛,它们的作用也是一模一样,只是传参的形式有区别而已. 原文作者:林 ...
- xcode编译报错unknown error -1=ffffffffffffffff Command /bin/sh failed with exit code 1
升级完xcode9.1之后,编译项目出现如下错误: CI今日构建时报出如下错误: /Users/xxx/Library/Developer/Xcode/DerivedData/Snowball-ebl ...
- JS画几何图形之五【过圆外一点作切线】
样例:http://www.zhaojz.com.cn/demo/draw9.html 依赖:[点].[直线].[圆] //画切线 //point 圆外的一点 //dot 圆心 //r 半径 func ...
- Hadoop版本选择
刚开始学习Hadoop时就曾经一直抱怨Hadoop的安装部署为什么这么麻烦,对于一个新手需要捯饬一天才能把分布式环境安装配置好.而对于一个自学Hadoop而周围又没人交流的菜鸟来说,我对Hadoop的 ...
- 网络实时流量监控工具iftop---转
网络实时流量监控工具iftop 分类: LINUX 1.安装依赖软件库 [root@localhost ~]# yum install libpcap libpcap-devel ncurses nc ...
- python中星号的意义(**字典,*列表或元组)
传递实参和定义形参(所谓实参就是调用函数时传入的参数,形参则是定义函数是定义的参数)的时候,你还可以使用两个特殊的语法:*.** . 调用函数时使用* ,** test(*args)中 * 的作用:其 ...
- [js高手之路] html5 canvas教程 - 制作一个数码倒计时效果
效果图: 这个实例主要注意: 1,剩余时间的计算 2,每个时间数字的绘制 时间主要有0-9和一个冒号组成,用数组来表示( 0: 就是不画圆,1:就是画一个蓝色的圆 ) num.js文件: var di ...
- SpringMVC 控制器默认支持GET和POST两种方式
在SpringMVC的controller中,@RequestMapping只写路径,不包含RequetMethod.GET和RequetMethod.POST,HttpServletRequest的 ...
- SSH实战OA 11:BBS模块
<SSH实战OA>系列博客的系统管理.权限管理等内容后面再补上吧,先继续第三个模块:网上交流模块.网上交流主要做两个需求:论坛管理和论坛. BBS的一些基本术语: 板块:也叫做" ...