思路:用Floyd求传递闭包。

  附:逗号后的空格没看到,WA了好多次……。还有就是强连通分量也可以做,但是对这个题来说太麻烦,而且不方便输出,。

  代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
using namespace std;
int n,m;
map<string,int> ma;
map<int,string> mb;
int maps[][],vis[];
void dfs(int u)
{
vis[u] = ;
for(int i = ;i <= n;i++)
{
if(!vis[i] && maps[u][i] && maps[i][u])
{
cout<<", "<<mb[i];
dfs(i);
}
}
return ;
}
int main()
{
string a,b;
int tot,u,v,ca = ;
while(~scanf("%d%d",&n,&m))
{
if(n+m == ) break; ma.clear();
mb.clear();
memset(maps,,sizeof(maps));
tot = ;
for(int i = ;i <= m;i++)
{
cin>>a>>b;
if(!ma[a]) ma[a] = tot++;
if(!ma[b]) ma[b] = tot++;
u = ma[a]; v = ma[b];
mb[u] = a; mb[v] = b;
maps[u][v] = ;
}
for(int k = ;k <= n;k++)
{
for(int i = ;i <= n;i++)
{
for(int j = ;j <= n;j++)
{
maps[i][j] = maps[i][j] || (maps[i][k]&&maps[k][j]);
}
}
}
memset(vis,,sizeof(vis));
if(!ca) puts("");
printf("Calling circles for data set %d:\n",++ca);
for(int i = ;i <= n;i++)
{
if(!vis[i])
{
cout<<mb[i];
dfs(i);
cout<<endl;
}
}
}
}

World Finals 1996 Uva 247 (Floyd求闭包)的更多相关文章

  1. Calling Circles(UVa 247)(Floyd 算法)

    用Floyd算法求出传递闭包,然后用dfs求出每条连通分量.注意其中用到的几个小技巧: #include<cstdio> #include<iostream> #include ...

  2. POJ3275:Ranking the Cows(Bitset加速floyd求闭包传递)

    Each of Farmer John's N cows (1 ≤ N ≤ 1,000) produces milk at a different positive rate, and FJ woul ...

  3. UVa 247 - Calling Circles(Floyd求有向图的传递闭包)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  4. UVA - 247 Calling Circles(Floyd求传递闭包)

    题目: 思路: 利用Floyd求传递闭包(mp[i][j] = mp[i][j]||(mp[i][k]&&mp[k][j]);),当mp[i][j]=1&&mp[j][ ...

  5. UVA 247"Calling Circles"(floyd求传递闭包+SCC)

    传送门 题意: 如果两个人相互打电话(直接或间接),则说他们在同一个电话圈里. (a,b) 表示 a 打给 b: 例如,(a,b),(b,c),(c,d),(d,a),则这四个人在同一个电话圈里: 输 ...

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

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

  7. UVA 247 - Calling Circles (Floyd)

    互相可以打电话是一个传递关系,所以Floyd求传递封包,dfs找一个尽量大的圈. #include<bits/stdc++.h> using namespace std; ; map< ...

  8. 2017"百度之星"程序设计大赛 - 资格赛【1001 Floyd求最小环 1002 歪解(并查集),1003 完全背包 1004 01背包 1005 打表找规律+卡特兰数】

    度度熊保护村庄 Accepts: 13 Submissions: 488 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3276 ...

  9. POJ 3660 Cow Contest(Floyd求传递闭包(可达矩阵))

    Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16341   Accepted: 9146 Desc ...

随机推荐

  1. Chapter 15_4 子模块和包

    Lua支持具有层级性的模块名,可以用一个点来分隔名称中的层级. 比如,一个mod.sub模块,它就是mod的子模块.一个包(package)就是一个完整的模块树. 当你require "mo ...

  2. chapter9_1 协同程序

    协同程序与线程差不多,也就是一条执行序列:有自己独立的栈.局部变量.指令指针,以及和其他协同程序共享的全局变量和其他大部分东西. 两者区别在于:一个多线程的程序可以同时运行几个线程,而协同程序却需要彼 ...

  3. mongodb安装 win7版

    首先下载mongdb的zip包,可以到:http://www.mongodb.org/downloads.选择32位或64位的. 在D盘创建mongodb文件夹,d:\mongodb.把解压里面所有的 ...

  4. The type java.util.Map$Entry cannot be resolved. It is indirectly referenced from required .class files

    JDK版本的问题. 解决方法: 原来jdk1.8不向下兼容,用回1.6的就可以了. 下图有三个jdk,前两个自己装的,第三个MyEclipse自带的.

  5. Find and run the whalesay image

    Find and run the whalesay image People all over the world create Docker images. You can find these i ...

  6. List泛型集合常用方法

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace List ...

  7. 解决网页在手机浏览器打开不停刷新的方案(百度的ua自动转向js问题)

    一:发现问题 原有可能是网站内挂了一个百度的ua自动转向js,手机访问的话会被自动转到feiyujd.com,然后又被转到www点feiyujd点com,这样反复死循环.就形成了一直在刷新,网站一闪一 ...

  8. c语言-三字符组

    C 源程序源字符集在 7 位 ASCII 字符集中包含,但设置为 ISO 646-1983 固定的代码的超集. 三字符序列允许 C 程序编写使用 " 仅 ISO (国际标准组织的固定的代码. ...

  9. ASCII码对应表chr(9)、chr(10)、chr(13)、chr(32)、chr(34)、chr(39)

    chr(9) tab空格       chr(10) 换行      chr(13) 回车        Chr(13)&chr(10) 回车换行       chr(32) 空格符      ...

  10. Chapter 1 First Sight——25

    "They are… very nice-looking." I struggled with the conspicuous understatement. 他们都很好看,我与轻 ...