Calling Circles
If you’ve seen television commercials for long-distance phone companies lately, you’ve noticed thatmany companies have been spending a lot of money trying to convince people that they provide thebest service at the lowest cost. One company has “calling circles.” You provide a list of people thatyou call most frequently. If you call someone in your calling circle (who is also a customer of the samecompany), you get bigger discounts than if you call outside your circle. Another company points outthat you only get the big discounts for people in your calling circle, and if you change who you callmost frequently, it’s up to you to add them to your calling circle.
LibertyBell Phone Co. is a new company that thinks they have the calling plan that can put othercompanies out of business. LibertyBell has calling circles, but they figure out your calling circle foryou. This is how it works. LibertyBell keeps track of all phone calls. In addition to yourself, yourcalling circle consists of all people whom you call and who call you, either directly or indirectly.
For example, if Ben calls Alexander, Alexander calls Dolly, and Dolly calls Ben, they are all withinthe same circle. If Dolly also calls Benedict and Benedict calls Dolly, then Benedict is in the samecalling circle as Dolly, Ben, and Alexander. Finally, if Alexander calls Aaron but Aaron doesn’t callAlexander, Ben, Dolly, or Benedict, then Aaron is not in the circle.
You’ve been hired by LibertyBell to write the program to determine calling circles given a log ofphone calls between people.
Input
The input file will contain one or more data sets. Each data set begins with a line containing twointegers, n and m. The first integer, n, represents the number of different people who are in the dataset. The maximum value for n is 25. The remainder of the data set consists of m lines, each representinga phone call. Each call is represented by two names, separated by a single space. Names are first namesonly (unique within a data set), are case sensitive, and consist of only alphabetic characters; no nameis longer than 25 letters.
For example, if Ben called Dolly, it would be represented in the data file as
Ben Dolly
Input is terminated by values of zero (0) for n and m.
Output
For each input set, print a header line with the data set number, followed by a line for each callingcircle in that data set. Each calling circle line contains the names of all the people in any order withinthe circle, separated by comma-space (a comma followed by a space). Output sets are separated byblank lines.
Sample Input
5 6
Ben Alexander
Alexander Dolly
Dolly Ben
Dolly Benedict
Benedict Dolly
Alexander Aaron
14 34
John Aaron
Aaron Benedict
Betsy John
Betsy Ringo
Ringo Dolly
Benedict Paul
John Betsy
John Aaron
Benedict George
Dolly Ringo
Paul Martha
George Ben
Alexander George
Betsy Ringo
Alexander Stephen
Martha Stephen
Benedict Alexander
Stephen Paul
Betsy Ringo
Quincy Martha
Ben Patrick
Betsy Ringo
Patrick Stephen
Paul Alexander
Patrick Ben
Stephen Quincy
Ringo Betsy
Betsy Benedict
Betsy Benedict
Betsy Benedict
Betsy Benedict
Betsy Benedict
Betsy Benedict
Quincy Martha
0 0
Sample Output
Calling circles for data set 1:
Ben, Alexander, Dolly, Benedict
Aaron
Calling circles for data set 2:
John, Betsy, Ringo, Dolly
Aaron
Benedict
Paul, George, Martha, Ben, Alexander, Stephen, Quincy, Patrick
题目大意:如果两个人相互打电话(直接或间接),则说明他们在同一个电话圈里。例如,a打给b,b打给c,c打给d,d打给a,则这4个人在同一个电话圈里;如果e打给f但f不打给e,则不能推出e和f在同一个算法圈里。输入n(n <= 25)个人的m次电话,找出所有电话圈。人名只包含字母,不会超过25个字符且不重复。
解题思路:首先利用Floyd求出传递闭包,即dp[i][j]表示i是否直接或者间接给j打过电话,则当且仅当dp[i][j] == dp[j][i] == 1时二者处于同一电话圈。构造一个新图,在“在一个电话圈里”的两个人之间连一条边,然后依次输出各个连通分量的所有人即可。
代码如下:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 33;
map<string,int> mp;
vector<string> G[maxn];
string str[maxn];
int dp[maxn][maxn];
bool vis[maxn];
int n,m;
void init()
{
mp.clear();
for(int i = 1;i <= n;i++)
G[i].clear();
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++)
dp[i][j] = (i == j);
memset(vis,0,sizeof(vis));
}
void floyd()
{
for(int k = 1;k <= n;k++)
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++)
dp[i][j] = dp[i][j] || (dp[i][k] && dp[k][j]);
}
int main()
{
string p,q;
int ncase = 0;
while(cin>>n>>m && (n || m)){
if(ncase) cout<<endl;
init();
int cnt = 1;
while(m--){
cin>>p>>q;
if(!mp[p]) mp[p] = cnt,str[cnt] = p,cnt++;
if(!mp[q]) mp[q] = cnt,str[cnt] = q,cnt++;
dp[mp[p]][mp[q]] = 1;
}
floyd();
for(int i = 1;i <= n;i++){
for(int j = 1 ;j <= n;j++){
if(!vis[j] && dp[i][j] && dp[j][i]) G[i].push_back(str[j]),vis[j] = 1;
}
}
cout<<"Calling circles for data set "<< ++ncase << ":" << endl;
for(int i = 1;i <= n;i++){
if(G[i].size()){
cout<<G[i][0];
for(int j = 1;j < (int)G[i].size();j++){
cout<<", " << G[i][j];
}
cout<<endl;
}
}
}
return 0;
}