Calling Circles

Description:

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

# include <cstdio>
# include <vector>
# include <cstring>
# include <iostream>
using namespace std;

vector < string > name;
int d[30][30],vis[30],n,m;

int ID(const string &s)
{
    for(int i=0;i<name.size();i++)
        if(name[i]==s)    return i;
    name.push_back(s);
    return name.size()-1;
}

void search(int i)
{
    vis[i]=1;
    for(int j=0;j<n;j++)
        if(d[i][j]&&!vis[j]&&d[j][i])
        {
            printf(", %s",name[j].c_str());
            search(j);
        }
}

int main()
{
    int kase=0;
    char c[99],s[99];
    while(~scanf("%d%d",&n,&m)&&n&&m)
    {
        int i,j,k;
        name.clear();
        memset(d,0,sizeof(d));
        memset(vis,0,sizeof(vis));
        for(i=0;i<n;i++)   d[i][i]=1;

        for(i=0;i<m;i++)
        {
            scanf("%s%s",c,s);
            d[ID(c)][ID(s)]=1;
        }

        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][k] && d[k][j];

        if(kase>0 )    printf("\n");

        printf("Calling circles for data set %d:\n",++kase);

        for(i=0;i<name.size();i++)
        {
            if(!vis[i])
            {
                printf("%s",name[i].c_str());
                search(i);
                printf("\n");
            }
        }
    }
    return 0;
}



### 函数调用的概念 函数调用是指程序执行过程中,通过指定名称来激活特定功能的过程。当一个函数被调用时,控制权会转移到该函数定义的位置,在完成一系列操作后返回到原来的地方继续执行。 在编程语言中实现函数调用的方式多种多样,下面是一些常见例子以及最佳实践: #### Python 中的简单函数调用 Python 是一种广泛使用的高级编程语言,支持直观简洁的语法来进行函数定义和调用。 ```python def greet(name): message = f'Hello {name}' return message result = greet('Alice') # 调用greet函数并传递参数'Alice' print(result) # 输出结果 'Hello Alice' ``` 此代码片段展示了如何创建接受单个字符串参数 `name` 的函数,并返回一条问候消息给调用者[^1]。 #### JavaScript 中的对象方法调用 JavaScript 支持面向对象特性,允许将多个属性与行为封装在一个实体内——即对象里。这些关联起来的数据项可以作为整体一起处理。 ```javascript const calculator = { add(a, b){ console.log(`Sum of ${a} and ${b}:`, a+b); return a + b; } }; calculator.add(5,7); // 执行add方法并向其传入两个数值型实参 // 控制台输出 "Sum of 5 and 7:" 和计算得到的结果 ``` 这里展示了一个名为 `calculator` 的对象及其内部的方法 `add()` 。这体现了基于类接口设计的思想。 #### C++中的重载机制 C++ 提供了一种称为运算符重载的功能,使得相同名字却具有不同签名(参数列表)的几个版本可以在同一个作用域中共存而不发生冲突。 ```cpp #include <iostream> using namespace std; class ComplexNumber{ public: double real,img; void display(){ cout << "("<<real<<","<<img<<")"; } friend ostream& operator<<(ostream &out ,ComplexNumber c){ out<<"("<<c.real<<","<<c.img<<")"; return out; } }; int main() { ComplexNumber num={3,-4}; cout << "The complex number is "; num.display(); cout<<"\nOr using overloaded stream insertion:\n"; cout <<num; return 0; } ``` 上述实例说明了怎样利用成员函数形式的操作符重写达成自定义类型的输入/输出格式化目的[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值