UVa247

题目连接(vj,比较方便):https://vjudge.net/problem/UVA-247

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.

题目大意:

来自紫书推荐的经典题目,题目大意详见紫书

解题思路:

floyd算法求传递闭包,floyd算法可以求每两点之间的最短路,同时也可以求强连通分量

顺便说一下传递闭包和强连通分量,首先传递闭包定义更广泛,可以用在图论之外的地方。然后再图论中,根据传递闭包的定义,该强连通块一定是边数最少的,而强连通分量不一定是边数最少的,只要求两两可达。他们定义出发点不同。

代码如下:

#include<bits/stdc++.h>
#define MAX 30
using namespace std;

int d[MAX][MAX];
bool vis[MAX];
int n,m;

void floyd()
{
    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][j]||(d[i][k]&&d[k][j]);
}

void init()
{
    for(int i=0; i<n; i++)
        for(int j=0; j<n; j++)
            d[i][j]=i==j?1:0;
    memset(vis,0,sizeof(vis));
}

int main()
{
    int cnt;
    int T=0;
    string name1,name2;
    string namenumber[MAX];
    map <string,int> mp;
    while(~scanf("%d%d",&n,&m)&&n)
    {
        T++;
        cnt=0;
        mp.clear();
        init();
        while(m--)
        {
            int u,v;
            cin>>name1>>name2;
            if(!mp.count(name1))
                mp[name1]=cnt++;
            if(!mp.count(name2))
                mp[name2]=cnt++;
            u=mp[name1];
            v=mp[name2];
            namenumber[u]=name1;
            namenumber[v]=name2;
            d[u][v]=1;
        }
        floyd();
        printf("Calling circles for data set %d:\n",T);
        for(int i=0; i<n; i++)
        {
            if(vis[i])
                continue;
            cout<<namenumber[i];
            for(int j=i+1; j<n; j++)
            {
                if(vis[j])
                    continue;
                if(d[j][i]&&d[i][j])
                {
                    cout<<", "<<namenumber[j];
                    vis[j]=1;
                }
            }
            cout<<endl;
        }
    }
}

 

转载于:https://www.cnblogs.com/cryingrain/p/8877424.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值