Original question:
If you've seen television commercials for long-distance phone companies lately, you've noticed that many companies has been En 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 business. Libertybell have calling circles, but they figure 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 is contain one or more data sets. Each data set begins with a line containing the 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 was represented by and names, separated by a single space. Names is first Names only (unique within a data set), is case sensitive, and consist of the only alphabetic characters; No Name
is longer than letters.
For example, if Ben called Dolly, it would is represented in the data file as
Ben Dolly
Input is terminated by the values of zero (0) for N and M.
Output
For each of the input set, print a header line with the data set number, followed by a line for each calling circle in that data Set. Each calling Circle line contains the names of all the people in any order within the 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
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.
Effect:
Q247-calling Circles
The company offers a "circle of friends" service that provides you with a list of the most frequently contacted friends, and when you flick call these friends, you will be more cost-efficient.
Libertybell Phone Co. Has recently offered a better service, and they have helped you find a circle of friends. The principle is this, first of all, they will record all the conversation records, including who calls who.
For example, Ben called Alexander,alexander to call dolly,dolly to call Ben, and they would be in the same circle of friends. If Dolly call Benedict, and Benedict also call Dolly, Benedict will add to Ben, Alexander, Dolly these three friends circle. If Alexander calls Aaron, but Aaron does not call Alexander or Ben or Dolly or Benedict, Aaron is not in this circle of friends.
To set up a conversation record for a group of people, please find out all the circle of friends.
Input
The input contains multiple tests, and the first column of each group has two integer n, m,n represents the number of people in the information (n <= 25), and then there will be M columns, each of which is a group of conversation records, indicating who called who, the name of the person is separated by a blank character, different people do not have the same name, and the region is the size of the write, The name contains only English letters and has a length not exceeding 25 characters.
For example, Ben's call to Dolly will be expressed as follows:
Ben Dolly
When n = m = 0 Indicates the end of the test material.
Output
Please refer to the sample information format to output each group of answers, each group of friends to stand alone in a column, the name of the circle of friends can be in any order to output, and please with a number of symbols and a blank character separated. Each test will be separated by a blank line.
#include <bits/stdc++.h> using namespace std;
BOOL G[26][26];
BOOL Mark[26][26];
Map<string,int> name;
Map<int,string> Iname;
Vector<int> vname[26]; void floyed (int x) {for (int. k=1;k<=x;k++) {for (int. i=1;i<=x;i++) {for (Int. J=1 ; j<=x;j++) g[i][j]=g[i][j]| |
(G[i][k]&&g[k][j]);
}}} int main () {Ios::sync_with_stdio (false);
int n,m,t=1;
String s1,s2;
while (cin>>n>>m,n+m) {int k=1;
Name.clear ();
Iname.clear ();
for (int i=0;i<=25;i++) vname[i].clear ();
memset (g,false,sizeof (G));
memset (Mark,0,sizeof (Mark));
for (int i=0;i<=25;i++) g[i][i]=true;
for (int i=1;i<=m;i++) {cin>>s1>>s2;
if (name[s1]==0) {name[s1]=k;
INAME[K]=S1;
++k; } if (NAme[s2]==0) {name[s2]=k;
INAME[K]=S2;
++k;
} g[name[s1]][name[s2]]=true;
} floyed (n);
if (t>1) cout<<endl;
cout<< "Calling circles for data set" <<t++<< ":" <<endl; for (int i=1;i<=n;i++) {for (int j=1;j<=n;j++) {if (g[i][j]&&
G[j][i]&&!mark[i][j]&&!mark[j][i]) {mark[j][j]=true;
Vname[i].push_back (j);
for (int l=0;l<vname[i].size (); l++) mark[j][vname[i][l]]=mark[vname[i][l]][j]=true;
}}} for (int i=1;i<=n;i++) {if (Vname[i].empty ())
Continue
for (int j=0;j<vname[i].size (); j + +) {if (J!=vname[i].size ()-1)cout<<iname[vname[i][j]]<< ",";
else cout<<iname[vname[i][j]]<<endl;
}}} return 0;
}
Answer:
uses the floyed algorithm to find the closure packet, and then outputs the corresponding closure. When the output is closed, DFS can be used, or you can find it. First Judge G[i][j] and G[j][i] is connected, if connected then add to the current closure, and in the closure of the points stored in the current electric mark can be.