Gym - 100952F

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.

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

map+set就可以过了。
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <algorithm>
 5 #include <set>
 6 #include <map>
 7 #include <vector>
 8 #define ll long long
 9 using namespace std;
10 string s[110],ss[110],sss[110];
11 
12 int main(){
13     int t,n;
14     cin>>t;
15     while(t--){
16         scanf("%d",&n);
17         map<string,int> mp;
18         //vector<string> vs[110];
19         set<string> st,st1[110];
20         for(int i = 0; i < n; i ++){
21             cin>>s[i]>>ss[i]>>sss[i];
22             //st.insert(s[i]);
23             //st.insert(ss[i]);
24             //st.insert(sss[i]);
25         }
26         mp["Ahmad"] = 1;
27         st1[0].insert("Ahmad");
28         for(int i = 0; i < n; i ++){
29             for(int j = 0; j < n; j ++){
30                 if(st1[i].count(s[j])){
31                     if(mp[ss[j]] == 0){
32                         st1[i+1].insert(ss[j]);
33                         mp[ss[j]] = 1;
34                     }
35                     if(mp[sss[j]]==0){
36                         st1[i+1].insert(sss[j]);
37                         mp[sss[j]] = 1;
38                     }
39                 }
40                 if(st1[i].count(ss[j])){
41                     if(mp[sss[j]] == 0){
42                         st1[i+1].insert(sss[j]);
43                         mp[sss[j]] = 1;
44                     }
45                     if(mp[s[j]]==0){
46                         st1[i+1].insert(s[j]);
47                         mp[s[j]] = 1;
48                     }
49                 }
50                 if(st1[i].count(sss[j])){
51                     if(mp[ss[j]] == 0){
52                         st1[i+1].insert(ss[j]);
53                         mp[ss[j]] = 1;
54                     }
55                     if(mp[s[j]]==0){
56                         st1[i+1].insert(s[j]);
57                         mp[s[j]] = 1;
58                     }
59                 }
60             }
61         }
62         for(int i = 0; i < n; i ++){
63             if(mp[s[i]]==0)st.insert(s[i]);
64             if(mp[ss[i]]==0)st.insert(ss[i]);
65             if(mp[sss[i]]==0)st.insert(sss[i]);
66             mp[s[i]]=1;mp[ss[i]]=1;mp[sss[i]]=1;
67         }
68         cout << mp.size() << endl;
69         set<string>::iterator it;
70         for(int i = 0; i <= n; i ++){
71             if(st1[i].size() == 0)break;
72             for(it = st1[i].begin(); it != st1[i].end(); ++it){
73                 cout << (*it) << ' ' << i << endl;
74             }
75         }
76         for(it = st.begin(); it != st.end(); ++it){
77             cout << (*it) << " undefined\n";
78         }
79     }
80     return 0;
81 }

 

 
posted @ 2017-07-17 20:06  starry_sky  阅读(167)  评论(0编辑  收藏  举报