第一个强连通分量的题。

  题意:有一堆人,a给b打电话表示a有一条向b的边,一个强连通分量代表一个电话圈,把每个电话圈里的人在一行内输出出来。

  直接上模板即可,但是要注意把string用map映射一下的技巧。

  代码如下:

 #include <stdio.h>
#include <algorithm>
#include <string.h>
#include <string>
#include <iostream>
#include <vector>
#include <map>
#include <stack>
using namespace std; int n,m;
vector<string> names;
vector<int> G[];
map<string,int> mp;
stack<int> S;
int tot;
int belong[],scc_cnt,low[],dfn[],dfs_clock; void dfs(int u)
{
dfn[u]=low[u]=++dfs_clock;
S.push(u);
for(int i=;i<G[u].size();i++)
{
int v = G[u][i];
if(!dfn[v])
{
dfs(v);
low[u]=min(low[u],low[v]);
}
else if(!belong[v])
{
low[u]=min(low[u],low[v]);
}
}
if(dfn[u]==low[u])
{
scc_cnt++;
for(;;)
{
int x = S.top();S.pop();
belong[x] = scc_cnt;
if(x==u) break;
}
}
} void scc()
{
dfs_clock=scc_cnt=;
memset(belong,,sizeof(belong));
memset(dfn,,sizeof(dfn));
for(int i=;i<n;i++) if(!dfn[i]) dfs(i);
} void print()
{
vector<int> ans[];
for(int i=;i<n;i++) ans[belong[i]].push_back(i);
for(int i=;i<=scc_cnt;i++)
{
for(int j=;j<ans[i].size();j++)
{
if(j) printf(", ");
cout<<names[ans[i][j]];
}
puts("");
}
} int main()
{
int cnt=;
while(scanf("%d%d",&n,&m)==)
{
tot=;
names.clear();
mp.clear();
for(int i=;i<n;i++) G[i].clear();
if(n== && m==) break;
for(int i = ; i <= m;i++)
{
string u,v;
cin>>u>>v;
if(mp.find(u)==mp.end()) mp[u]=tot++,names.push_back(u);
if(mp.find(v)==mp.end()) mp[v]=tot++,names.push_back(v);
G[mp[u]].push_back(mp[v]);
}
scc(); if(cnt != ) cout << endl;
cout << "Calling circles for data set " << cnt ++ <<":" << endl;
//printf("Calling circles for data set %d:\n",cnt++);
print();
//puts("");
}
return ;
}

  但是搞不懂的是为什么注释掉的输出部分是错的呢- -

UVA 247 Calling Circles —— (强连通分量模板题)的更多相关文章

  1. UVa 247 Calling Circles【传递闭包】

    题意:给出n个人的m次电话,问最后构成多少个环,找出所有的环 自己想的是:用map来储存人名,每个人名映射成一个数字编号,再用并查集,求出有多少块连通块,输出 可是map不熟,写不出来,而且用并查集输 ...

  2. UVA - 247 Calling Circles Floyd判圈

    思路:利用的Floyd判圈,如果i能到j,j也能到i说明i和j在同一个圈里.每个人的名字可用map编号.最后DFS打印答案即可. AC代码 #include <cstdio> #inclu ...

  3. UVa 247 Calling Circles (DFS+Floyd)

    题意:如果两个人互通电话,那么他们就在一个电话圈里,现在给定 n 个人,并且给定 m 个通话记录,让你输出所有的电话圈. 析:刚开始没想到是Floyd算法,后来才知道是这个算法,利用这个算法进行连通性 ...

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

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

  5. UVA 247 - Calling Circles (Floyd)

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

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

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

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

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

  8. POJ 2186 Popular Cows 强连通分量模板

    题意 强连通分量,找独立的块 强连通分量裸题 #include <cstdio> #include <cstdlib> #include <cstring> #in ...

  9. 求强连通分量模板(tarjan算法)

    关于如何求强连通分量的知识请戳 https://www.byvoid.com/blog/scc-tarjan/ void DFS(int x) { dfn[x]=lowlink[x]=++dfn_cl ...

随机推荐

  1. MyBatis 源码篇-Transaction

    本章简单介绍一下 MyBatis 的事务模块,这块内容比较简单,主要为后面介绍 mybatis-spring-1.**.jar(MyBatis 与 Spring 集成)中的事务模块做准备. 类图结构 ...

  2. 怎样修改 VS Code 主题?

    方法1. 点击左上角 File > Preferences > Color Theme. 方法2. 使用快捷键: Ctrl + K , Ctrl + T  PS: 查询各种操作的快捷键可以 ...

  3. vue网络不好时不间断请求

    配置默认参数 const { apiConfig: { timeout, retry, retryDelay } } = config; if(timeout) axios.defaults.time ...

  4. HTML的标签简单概括

    段落标签 <p></p> 属性  说明 值 align 对其方式 left(默认).right.center 水平线 <hr /> 属性  说明   值 width ...

  5. body测试onclick等鼠标事件无效果详解

    DOM事件机制包括五部分: DOM事件级别 DOM事件流 DOM事件模型 事件代理 Event对象常见的方法和属性 但是有时候发现给body标签里设置onclick属性,不起作用,代码如下: 不管单击 ...

  6. window上mongoDB的安装及常用mongodb命令

    前几天在学习node操作数据库时使用的mongoDB数据库,今天来对mongodb的安装过程及配置以及后面需要使用的一些常用命令做一下总结. 安装MongoDB (可参考菜鸟教程中的安装步骤) 首先, ...

  7. liunx pip安装

    方法一 wget https://bootstrap.pypa.io/get-pip.py python get-pip.py 方法二 wget https://pypi.python.org/pac ...

  8. Yii 2.0 GII 访问404错误

    网上大部分都是普通的开启和配置资料 按照网上资料配置 访问localhost/index/php?r=gii 总是提示404错误 解决方法如下: Yii基础版中的 web.php 代码如下 if (Y ...

  9. 【python】python _、__、__xx__之间的差别

    本文来自 yzl11 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/yzl11/article/details/53792416?utm_source=copy 单下 ...

  10. 虚拟机不能桥接联网 vmnet0上的网桥当前未运行

    win10家庭版更新到内测版后,原来可以正常桥接工作的虚拟机ubuntu不能在桥接模式下联网和ssh连接了,因为获取不到IP地址了. 上网搜索一下,发现直接粗暴的方法--修复VMware Workst ...