uva 274 Calling Circles

来源:互联网 发布:日本转区软件 编辑:程序博客网 时间:2024/05/01 05:00

原题:
If you’ve seen television commercials for long-distance phone companies lately, you’ve noticed that many companies have been spending a lot of money trying to convince people that they provide the best service at the lowest cost. One company has “calling circles.” You provide a list of people that you call most frequently. If you call someone in your calling circle (who is also a customer of the same company), you get bigger discounts than if you call outside your circle. Another company points out that you only get the big discounts for people in your calling circle, and if you change who you call most 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 other
companies out of business. LibertyBell has calling circles, but they figure out your calling circle for you. This is 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 indirectly.
For example, if Ben calls Alexander, Alexander calls Dolly, and Dolly calls Ben, they are all within the same circle. If Dolly also calls Benedict and Benedict calls Dolly, then Benedict is in the same calling circle as Dolly, Ben, and Alexander. Finally, if Alexander calls Aaron but Aaron doesn’t call Alexander, 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 of phone calls between people.
Input
The input file will contain one or more data sets. Each data set begins with a line containing two integers, n and m. The first integer, n, represents the number of different people who are 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 represented by two names, separated by a single space. Names are first names only (unique within a data set), are case sensitive, and consist of only alphabetic characters; no name
is 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 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 are 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
大意:
Q247 - Calling Circles
電信公司提供一種稱為「朋友圈」的電信服務,由你提供一組最常聯絡的朋友列表,當你撥打給這些朋友的時候會以較低的費率計費。
LibertyBell Phone Co.最近提供一種更好的服務方案,他們自動幫你找出朋友圈。原理是這樣的,首先他們會記錄所有通話記錄,包含誰打給誰。
例如Ben打給Alexander,Alexander打給Dolly,Dolly打給Ben,則他們會在同一個朋友圈內。若Dolly打給 Benedict,而Benedict也打給Dolly,則Benedict會加進Ben, Alexander, Dolly這三人的朋友圈。若Alexander打給Aaron,但Aaron不打給Alexander或Ben或Dolly或Benedict的任一人,則Aaron不在這朋友圈裡面。
給定一組人的通話記錄,請你找出所有朋友圈。
Input
輸入包含多組測試資料,每組資料的第一列會有兩個整數 n, m,n 表示資料中的人數( n <= 25),而接下來會有 m 列,每列為一組通話記錄,表示誰打給誰,人名中間以一個空白字元隔開,不同人不會有相同的名字,且區分大小寫,姓名僅包含英文字母且長度不超過25個字元。
例如Ben打給Dolly會以下列表示:
Ben Dolly
當 n = m = 0表示測試資料結束。
Output
請參考範例資料格式輸出每組答案,每組朋友圈請獨立以一列表示,朋友圈的人名可以以任意順序輸出,並請以逗號與一空白字元隔開。每組測試資料之間亦請以一列空行隔開。

#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;}

解答:
用floyed算法求闭包,然后输出对应的闭包即可。输出闭包的时候可以用dfs,也可以挨个找。首先判断G[i][j]且G[j][i]是否连通,如果连通那就加入到当前闭包当中,并用闭包当中存储的点把当前电标记即可。

0 0