Huawei's written questions, "the last few people know."

In communications software, which forwards the message in the group can make a message spread to the most there. Known assume there are m groups, one of them a message sent to the group of which he is located, each of these groups of people inside again message is forwarded again to all of his group which, may I ask all people of all groups are forwarded once after the last few people receive the message (including message people)? Output number of messages received (in decimal integer output and does not add line breaks)

Enter a description:

Send the first message names

Group number m of

Group 1 member names list

2 members of the group name list

....

M group members a list of names

English name is a string that contains letters and spaces, a maximum length of no more than 100 characters.

M is an integer of decimal number of the group, the maximum does not exceed 100.

Group member list contains the names of up to 1 personal names, separated by commas between the two names.

 

Output Description:

Output last decimal number can receive messages.

 

Example:

Input:

Jack
3
Jack, Tom, Anny, Lucy
Tom, Danny
, Jack, Lily

Output:

6

 

Ideas:

Statistics first group number where the owner name, and then follow the BFS to traverse; (note that only a Jack in groups, but not included in the case of other groups, and this time should be output 1)

 

 1 #include <iostream>
 2 #include <vector> 
 3 #include <bits/stdc++.h> 
 4 using namespace std;
 5 
 6 vector<string> pro(string& str){
 7     vector<string> res;
 8     int index =0;
 9     for(int i=0;i<str.size(); i++){
10         if(str[i]==','){
11             res.push_back(str.substr(index, i-index) );
12             index = i+1;
13         }
14     }
15     if(index<str.size()){
16         res.push_back(str.substr(index));
17     }
18     return res;
19 }
20 
21 int main(){
22     string name;
23     while(cin>>name){
24         int m;
25         cin>>m;
26         
27         string cur;
28         vector<vector<string> > data;
29         for(int i=0; i<m; i++){ 
30             cin>>cur;
31             data.push_back(  pro(cur ) );
32         }
33         
34         map<string, vector<int> > exist;
35         
36         for(int i=0;i<data.size(); i++){
37             for(int j=0; j<data[i].size();j++ ){
38                 exist[ data[i][j] ].push_back(i) ;                
39             } 
40         }
41         
42         queue<int> temp;         
43         for(int i=0; i<exist[name].size(); i++){
44             temp.push( exist[name][i] );
45         }
46         
47         vector<int> visited(m,0);
48         set<string> res; 
49         res.insert(name);    
50         
51         while(!temp.empty()){
52             int cur = temp.front();
53             temp.pop();
54             visited[cur] = 1;
55             for(int i=0; i<data[cur].size(); i++){
56                 for(int j=0; j< exist[ data[cur][i]  ].size(); j++ ){
57                     if( !visited[ exist[  data[cur][i]  ][j] ]  )    {
58                         temp.push( exist[ data[cur][i] ][j] );
59                     }                     
60                 }                
61                 res.insert( data[cur][i] );
62             }
63         }         
64         cout<<res.size();         
65     }     
66 }

 

Guess you like

Origin www.cnblogs.com/liugl7/p/11391332.html