calling Circles
Time Limit: 3000MS |
|
Memory Limit: Unknown |
|
64bit IO Format: %lld &%llu |
Submitstatus
Description
If you've seen television commercials for long-distance phone companies lately, you ' ve noticed that many companies hav E been spending a lot of money trying to convince people this they provide the best service at the lowest cost. One company has calling circles. " You provide a list of people the most frequently. If you call someone in your calling circle (who's also a customer of the same company), you get bigger discounts than if You call outside your circle. Another company points out this you only get the big discounts for people in your calling circle, and if your change who yo U call the most frequently, it's up to the add them to your calling circle.
Libertybell Phone Co. is a new company, that thinks they has the calling plan that can put other companies out of Busi Ness. Libertybell has calling circles and they gure out your calling circle for you. How it works. Libertybell keeps track of all phone calls. In addition to yourself, your calling circle consists of all people whom-you-call and who-call-you, either directly or IND irectly. For example, if Ben calls Alexander, Alexander calls Dolly, and Dolly calls Ben, they is all within the same circle. If Dolly also calls Benedict and Benedict calls Dolly, then Benedict are in the same calling circle as Dolly, Ben, and Alex Ander. Finally, if Alexander calls Aaron but Aaron doesn ' t call Alexander, Ben, Dolly, or Benedict, then Aaron was not in the CIRC Le. You ' ve been hired by Libertybell to write the program to determine calling circles given a log of phone calls between Peop Le.
Input
the input file will contain one or more data sets.&nb sp; each data set begins with a line containing both Integers,n and M . The first integer,n, represents the number of different people who is in the data set. the maximum value for
N is 25. the remainder of the data set consists of M lines, each representing a phone call. each call is Represe Nted by-names, separated by a single space. names is rst names only (unique within a data set), is case sensit Ive, and consist of only alphabetic characters; No name is longer than letters.
For example, if Ben called Dolly, it would being represented in the data file as
Ben Dolly
Input is terminated b Y values of zero (0) for N and M.
Output
For each input set, print a header line with the data set number, followed Alling Circle in so data set. each calling Circle line contains the names of all the people on any order within th E Circle, separated by Comma-space (a comma followed by a space) . Output sets is separated by blank lines.
Sample Input
5 6
Ben Alexander
Alexander Dolly
Dolly Ben
Dolly Benedict
Benedict Dolly
Ale Xander Aaron
+
John Aaron
Aaron Benedict
Betsy John
Betsy Ringo
Ringo Dolly
Benedict paul< br> 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 Ma Rtha
Ben Patrick
Betsy Ringo
Patrick Stephen
Paul Alexander
Patrick Ben
Stephen Quincy
Ringo B Etsy
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
Aa Ron
Calling circles for data set 2:
John, Betsy, Ringo, Dolly
Aaron
Benedict
Paul, George, Martha, Ben, Alexander, Stephen, Quincy, Patrick.
Use map to contact person names and corresponding numbers, d[i][j]=d[j][i]=1, can judge two people in a circle.
#include <iostream> #include <cstdio> #include <map> #include <string> #include <cstring>
using namespace Std;
const int MAXN=50;
const int inf=1000000;
int n,m,cnt;
Map<string,int> idx;
String RSTR[MAXN];
int D[MAXN][MAXN];
BOOL VIS[MAXN];
void Floyed () {for (int k=0;k<n;k++) for (int. i=0;i<n;i++) for (int j=0;j<n;j++) d[i][j]=d[i][j]| |
(D[i][k]&&d[k][j]);
} int Get_idx (string name) {if (Idx.count (name)) return Idx[name];
idx[name]=++cnt;
Rstr[cnt]=name;
return CNT;
} int main () {int cases=1;
while (scanf ("%d%d", &n,&m) && (n+m)) {cnt=-1;
memset (vis,0,sizeof (VIS));
Memset (d,0,sizeof (d));
Idx.clear ();
String A, B;
int idx1,idx2;
for (int i=0;i<m;i++) {cin>>a>>b;
Idx1=get_idx (a);
Idx2=get_idx (b);
D[idx1][idx2]=1;
} floyed (); Printf ("Calling circles for data set%d:\n", cases++);
for (int i=0;i<n;i++) {if (vis[i]) continue;
cout<<rstr[i];
for (int j=i+1;j<n;j++) {if (vis[j]) continue;
if (D[i][j]&&d[j][i]) {vis[j]=true;
cout << "," <<rstr[j];
}}cout<<endl;
}} return 0;
}