Calling Circles

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<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
int v[30][30], d[30][30], leap[30];
string gather[30];
int main()
{
    int i, j, m, n, ans, t, kase = 1;
    string a, b;
    while (scanf("%d%d", &n, &m) != EOF)
    {
        map<string, int>p;
        if (!n&&!m)break;
        memset(v, 0, sizeof(v));
        memset(d, 0, sizeof(d));
        memset(leap, 0, sizeof(leap));
        int cnt = 1;
        for (i = 1; i <= m; i++)
        {
            cin >> a >> b;
            if (!p[a])
            {
                p[a] = cnt;
                gather[cnt] = a;
                cnt++;
            }
            if (!p[b])
            {
                p[b] = cnt;
                gather[cnt] = b;
                cnt++;
            }
            d[p[a]][p[b]] = 1;
        }
        int k;
        for (k = 1; k <= n;k++)
        for (i = 1; i <= n;i++)
        for (j = 1; j <= n; j++)
            d[i][j] = d[i][j] || (d[i][k] && d[k][j]);
        if (kase != 1)
            cout << endl;
        cout << "Calling circles for data set " << kase << ":" << endl;
        kase++;
        for (i = 1; i <= n; i++)
        {
            if (!leap[i])
            {
                leap[i] = 1;
                cout << gather[i];
                for (j = i + 1; j <= n; j++)
                {
                    if (!leap[j] && d[i][j] && d[j][i])
                    {
                        cout << ", " << gather[j];
                        leap[j] = 1;
                    }
                }
                cout << endl;
            }
        }
    }
    return 0;
}
### 函数调用的概念及其在编程中的实现 #### 什么是函数调用? 函数调用是指程序运行过程中,通过指定名称激活某个预先定义好的功能模块的过程。这种机制允许开发者将复杂的任务分解成多个可重用的小单元,从而提高代码的清晰度和维护性[^1]。 #### Function Calling 的作用 Function Calling 不仅限于简单的内部方法调用,在现代开发环境中,它还被用来扩展模型的功能边界。例如,当涉及到与外部系统的交互时,可以通过嵌入 API 来增强模型的能力[^2]。具体来说,这通常涉及以下几个方面: - **数据获取**:从第三方服务中提取实时信息。 - **计算处理**:利用专门设计的服务完成复杂运算。 - **存储管理**:访问云端数据库或其他形式的数据仓库。 以下是基于 Python 实现的一个简单例子展示如何设置并执行带有 JSON Schema 描述参数结构以及目标 URL 地址指向所需 Web Service 的 function call: ```python import requests def get_current_weather(location): url = f"https://api.weatherapi.com/v1/current.json?key=YOUR_API_KEY&q={location}" response = requests.get(url) data = response.json() weather_description = data['current']['condition']['text'] temperature_celsius = data['current']['temp_c'] return {"description": weather_description, "temperature": temperature_celsius} # Example usage of the defined function with a hypothetical location. weather_info = get_current_weather("London") print(weather_info) ``` 此脚本片段展示了如何构建一个用于查询特定地点当前天气状况的应用场景下的 function calling 流程[^3]。 #### 如何解决问题? 如果遇到关于 function calling 方面的具体技术难题,则需考虑以下几点建议: 1. 明确需求——确认是要调用本地库还是远程 RESTful APIs; 2. 查阅官方文档——确保遵循所选框架/平台给出的最佳实践指南; 3. 错误调试——仔细分析返回错误消息以便快速定位问题根源所在。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值