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
每次先确定一个等级在查找下个等级,没有等级的视为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[105][4],name="Ahmad"; map<string,int>m; set<string>ans[150],pos; int t,n; void check(int x,int y,int z) { if(ans[x].count(s[y][z])) { for(int i=1;i<=2;i++) {
if(m[s[y][(z+i)%3]]==0) { ans[x+1].insert(s[y][(z+i)%3]); m[s[y][(z+i)%3]]=1; } } } } void check_undefined(int n) { for(int i=0;i<n;i++) { for(int j=0;j<3;j++) { if(m[s[i][j]]==0) pos.insert(s[i][j]),m[s[i][j]]=1; } } } int main() { scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i=0;i<n;i++) { for(int j=0;j<3;j++) { cin>>s[i][j]; } } m[name]=1; ans[0].insert(name); for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { for(int k=0;k<3;k++) { check(i,j,k); } } } check_undefined(n); printf("%d ",m.size()); for(int i=0;i<=n;i++) { if(ans[i].size()==0) 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=0;i<150;i++) ans[i].clear(); pos.clear(); m.clear(); } return 0; }