247 - Calling Circles【Floyd算法/传递闭包】

题目传送门
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

题意:
如果两个人互相打电话(直接或间接),则说他们在同一个电话圈里。
例如,a打给b,b打给c,c打给d,d打给a,则这4个人在同一个圈里;
如果e打给f,但f不打给e,则e和f不在同一个电话圈。
输入两个数n(n<=25),m,分别表示n个人和m次通话关系,找出所有的电话圈。人名只包含字母,不超过25个字符,且不重复

Floyd算法的简单应用,判断是否连通
用map将名字换成不同数字,方便处理

#include<cstdio>
#include<iostream>
#include<cstring>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
string str[30];
map<string, int>mp;
int vis[30];
int g[30][30];  //值为1代表连通,0则不连通
int n, m;
string s1, s2;
void floyd()
{
	for (int k = 0; k < n; k++)
	{
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < n; j++)
			{
				g[i][j] = (g[i][j] || (g[i][k] && g[k][j]));  
			}
		}
	}
}
int main()
{
	int mycase=1;
	while (cin>>n>>m)
	{
		if (n == 0 && m == 0) break;
		memset(g, 0, sizeof(g));     //初始化(下面也是)
		
		for (int i = 0; i < 30; i++)
		{
			str[i].clear();
		}
		mp.clear();
		s1.clear();
		s2.clear();
		int t = 0;  //t是代表不同名字,出现了新的名字,t就+1,
		for (int i = 0; i < m; i++)
		{
			cin >> s1>>s2; //输入两个名字,s1打给s2
			int from, to;
			if (!mp.count(s1)) //如果是一个新名字
			{
				from = t;  //t就是对应新名字的数字
				str[t] = s1; //str是存放名字的数组,方便后续的输出
				mp[s1] = t++; //s1这个名字就映射到t了,同时t+1
			}
			else      //这个名字有了
			{
				from = mp[s1]; //找到它的映射
			}
			if (!mp.count(s2)) //同上
			{
				to = t;
				str[t] = s2;
				mp[s2] = t++;
			}
			else
			{
				to = mp[s2];
			}
			g[from][to] = 1; //从from到to有通路
		}
		floyd(); //floyd算法
		if (mycase != 1)
		{
			cout << endl;
		}
		cout << "Calling circles for data set " << mycase++ << ":"<<endl;
		memset(vis, 0, sizeof(vis));
		for (int i = 0; i < n; i++) //遍历找出来就行
		{
			if (!vis[i]) //如果已经输出,说明在之前的电话圈中出现了
			{
				cout << str[i];
				
				for (int j = i + 1; j < n; j++) //j从i+1开始是因为j之前的数在找单源路径时已经判断过j了
				{
					if (!vis[j]&&g[i][j]&&g[j][i]) //判断是否已经输出,且双向连通才能构成电话圈
					{
						cout << ", "<< str[j] ; //输出电话圈中新增加的成员,注意格式,有一个空格
						vis[j] = 1; //标记为已输出
					}
				}
				cout << endl;
			}
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Description: An attempt was made to call a method that does not exist. The attempt was made from the following location: org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration$PoolBuilderFactory.getPoolConfig(LettuceConnectionConfiguration.java:207) The following method did not exist: 'void org.apache.commons.pool2.impl.GenericObjectPoolConfig.setMaxWait(java.time.Duration)' The calling method's class, org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration$PoolBuilderFactory, was loaded from the following location: jar:file:/D:/Developing%20learning%20software/apache-maven-3.9.2-bin/nfv/org/springframework/boot/spring-boot-autoconfigure/3.1.2/spring-boot-autoconfigure-3.1.2.jar!/org/springframework/boot/autoconfigure/data/redis/LettuceConnectionConfiguration$PoolBuilderFactory.class The called method's class, org.apache.commons.pool2.impl.GenericObjectPoolConfig, is available from the following locations: jar:file:/D:/Developing%20learning%20software/apache-maven-3.9.2-bin/nfv/org/apache/commons/commons-pool2/2.6.0/commons-pool2-2.6.0.jar!/org/apache/commons/pool2/impl/GenericObjectPoolConfig.class The called method's class hierarchy was loaded from the following locations: org.apache.commons.pool2.impl.GenericObjectPoolConfig: file:/D:/Developing%20learning%20software/apache-maven-3.9.2-bin/nfv/org/apache/commons/commons-pool2/2.6.0/commons-pool2-2.6.0.jar org.apache.commons.pool2.impl.BaseObjectPoolConfig: file:/D:/Developing%20learning%20software/apache-maven-3.9.2-bin/nfv/org/apache/commons/commons-pool2/2.6.0/commons-pool2-2.6.0.jar org.apache.commons.pool2.BaseObject: file:/D:/Developing%20learning%20software/apache-maven-3.9.2-bin/nfv/org/apache/commons/commons-pool2/2.6.0/commons-pool2-2.6.0.jar Action: Correct the classpath of your application so that it contains compatible versions of the classes org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration$PoolBuilderFactory and org.apache.commons.pool2.impl.GenericObjectPoolConfig
最新发布
07-24

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值