题目连接(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()
{
    ; k<n; k++)
        ; i<n; i++)
            ; j<n; j++)
                d[i][j]=d[i][j]||(d[i][k]&&d[k][j]);
}

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

int main()
{
    int cnt;
    ;
    string name1,name2;
    string namenumber[MAX];
    map <string,int> mp;
    while(~scanf("%d%d",&n,&m)&&n)
    {
        T++;
        cnt=;
        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]=;
        }
        floyd();
        printf("Calling circles for data set %d:\n",T);
        ; i<n; i++)
        {
            if(vis[i])
                continue;
            cout<<namenumber[i];
            ; j<n; j++)
            {
                if(vis[j])
                    continue;
                if(d[j][i]&&d[i][j])
                {
                    cout<<", "<<namenumber[j];
                    vis[j]=;
                }
            }
            cout<<endl;
        }
    }
}

UVa247的更多相关文章

  1. UVa247 Calling Circles

    Time Limit: 3000MS     64bit IO Format: %lld & %llu map存人名,floyd传递闭包,DFS查询. 输出答案的逗号后面还有个空格,被坑到了2 ...

  2. uva247 - Calling Circles(传递闭包+DFS)

    题意:两人相互打电话(直接或间接),则在一个电话圈.即a给b打电话,b给c打电话,则a给c间接打电话. 注意:1.注意标记.2.注意输出格式. #include<iostream> #in ...

  3. [Uva247][Tarjan求强连通分量][Calling Circles]

    题目大意: 例如:A跟B打电话,B跟C打电话,C跟A打电话..D跟E打电话,E跟D不打电话.则A,B,C属于同一个电话圈,D,E分别属于一个电话圈,问有多少个电话圈. 分析 就是裸的求强连通分量,直接 ...

  4. 紫书 例题11-4 UVa247 (Floyd判断联通)

    Floyd联通, 然后为了输出联通分量而新建一个图, 让互相可以打电话的建立一条边, 然后dfs输出联通分量就ok了. #include<cstdio> #include<iostr ...

  5. 洛谷 题解 UVA247 【电话圈 Calling Circles】

    [题意] 如果两个人互相打电话(直接或者间接),则说他们在同一个电话圈里.例如,\(a\)打给\(b\),\(b\)打给\(c\),\(c\)打给\(d\),\(d\)打给\(a\),则这四个人在同一 ...

  6. UVA 247 电话圈(Floyd传递闭包+输出连通分量)

    电话圈 紫书P365 [题目链接]电话圈 [题目类型]Floyd传递闭包+输出连通分量 &题解: 原来floyd还可以这么用,再配合连通分量,简直牛逼. 我发现其实求联通分量也不难,就是for ...

  7. UVa 247 电话圈(Floyd传递闭包)

    https://vjudge.net/problem/UVA-247 题意: 如果两个人相互打电话,则说他们在同一个电话圈里.例如,a打给b,b打给c,c打给d,d打给a,则这4个人在同一个圈里:如果 ...

  8. 简单的floyd——初学

     前言: (摘自https://www.cnblogs.com/aininot260/p/9388103.html): 在最短路问题中,如果我们面对的是稠密图(十分稠密的那种,比如说全连接图),计算多 ...

  9. [笔记-图论]Floyd

    用于可带负权的多源最短路 时间复杂度O(n^3) 注意一定不要给Floyd一个带负环的图,不然就没有什么意义了(最短路不存在) 模板 // Floyd // to get minumum distan ...

随机推荐

  1. 服务器下面的WEB-INF 不能直接访问,可以通过servlet进行访问

    服务器下面的WEB-INF 不能直接访问,可以通过servlet进行访问

  2. C#中转义符

    C#转义字符: 一种特殊的字符常量:以反斜线"\"开头,后跟一个或几个字符.具有特定的含义,不同于字符原有的意义,故称“转义”字符.主要用来表示那些用一般字符不便于表示的控制代码. ...

  3. [USACO] 2004 Open MooFest 奶牛集会

    题目背景 MooFest, 2004 Open 题目描述 约翰的N 头奶牛每年都会参加"哞哞大会".哞哞大会是奶牛界的盛事.集会上的活动很 多,比如堆干草,跨栅栏,摸牛仔的屁股等等 ...

  4. java的哈希遍历 hashmap

    Map<String,String> map = new HashMap<String, String>(); map.put("title"," ...

  5. IDEA2017 使用(二)

    1.鼠标悬浮在方法上显示api 2.关闭拼写检查 3.自动导入包(存在多个包时需要手动导入) 4.设置方法线

  6. nginx+webpy+uswgi+jwplayer组合搭建流媒体服务器

    转载自:http://blog.csdn.net/cjsafty/article/details/7892392 目前,由于Flash的流行,网络上绝大多数的微视频网站都采用了Flv格式来播放视频. ...

  7. MySql数据库学习总结(MySQL入门到精通)

    2017.1.24-2.3日(在大兴实验室) 1.数据库存储引擎: (1)MyISAM: 访问速度快,对事物完整性没要求,并以访问为主的适合这个 (2)InnoDB: 更占磁盘空间,需要进行频繁的更新 ...

  8. php windows rename 中文出错

    php windows rename 中文出错 rename()函数可以重命名文件.目录等,但是要注意目的地和起始地址的编码. 比如:我的PHP文件编码是UTF-8,但是在WINDOW系统中中文默认编 ...

  9. python基础(3)_列表、元组、字典

    一.列表 定义:[ ] 内以逗号分隔,按照索引,存放各种数据类型,每个位置代表一个元素 特性: > 可存放多个值 > 可修改指定索引位置对应的值,可变 > 按照从左到右的顺序定义列表 ...

  10. Linux基础(1)

    一.Linux的安装及相关配置 1.VMware Workstation安装CentOS7.0 图文版 详细步骤可以看连接:http://blog.csdn.net/alex_my/article/d ...