HDU 4460 Friend Chains --BFS
题意:问给定的一张图中,相距最远的两个点的距离为多少。
解法:跟求树的直径差不多,从1 开始bfs,得到一个最远的点,然后再从该点bfs一遍,得到的最长距离即为答案。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>
using namespace std;
#define N 100007 map<string,int> mp;
vector<int> G[];
int vis[],n,m,Maxdis,Maxtag;
struct node{
int dis,u;
}; void dfs(int u)
{
vis[u] = ;
for(int i=;i<G[u].size();i++)
{
int v = G[u][i];
if(!vis[v]) dfs(v);
}
} void bfs(int u)
{
queue<node> que;
memset(vis,,sizeof(vis));
node now;
now.dis = ;
now.u = u;
que.push(now);
vis[u] = ;
while(!que.empty())
{
int SZ = que.size();
while(SZ--)
{
node tmp = que.front();
que.pop();
int dis = tmp.dis;
int u = tmp.u;
if(dis > Maxdis)
{
Maxdis = dis;
Maxtag = u;
}
for(int i=;i<G[u].size();i++)
{
int v = G[u][i];
if(!vis[v])
{
now.u = v;
now.dis = dis+;
vis[v] = ;
que.push(now);
}
}
}
}
} int main()
{
int i,j;
string S;
while(scanf("%d",&n)!=EOF && n)
{
mp.clear();
for(i=;i<=n;i++)
{
cin>>S;
mp[S] = i;
G[i].clear();
}
scanf("%d",&m);
string A,B;
for(i=;i<=m;i++)
{
cin>>A>>B;
int a = mp[A], b = mp[B];
G[a].push_back(b);
G[b].push_back(a);
}
memset(vis,,sizeof(vis));
dfs();
int Link = ;
for(i=;i<=n;i++)
if(!vis[i]) Link = ;
if(!Link)
{
puts("-1");
continue;
}
Maxdis = , Maxtag = -;
bfs();
int S = Maxtag;
Maxdis = , Maxtag = -;
bfs(S);
printf("%d\n",Maxdis);
}
return ;
}
HDU 4460 Friend Chains --BFS的更多相关文章
- HDU 4460 Friend Chains (BFS,最长路径)
题意:给定 n 个人,和关系,问你这个朋友圈里任意两者之间最短的距离是多少. 析:很明显的一个BFS,只要去找最长距离就好.如果不能全找到,就是-1. 代码如下: #pragma comment(li ...
- HDU 4460 Friend Chains(map + spfa)
Friend Chains Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total ...
- HDU 4460 Friend Chains
Problem Description For a group of people, there is an idea that everyone is equals to or less than ...
- HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)
题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...
- hdu 4460 第37届ACM/ICPC杭州赛区H题 STL+bfs
题意:一些小伙伴之间有朋友关系,比如a和b是朋友,b和c是朋友,a和c不是朋友,则a和c之间存在朋友链,且大小为2,给出一些关系,求出这些关系中最大的链是多少? 求最短路的最大距离 #include& ...
- hdu 2102 A计划-bfs
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- HDU 1072(记忆化BFS)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1072 题目大意:走迷宫.走到装置点重置时间,到达任一点时的时间不能为0,可以走重复路,求出迷宫最短时 ...
- HDU 2364 (记忆化BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2364 题目大意:走迷宫.从某个方向进入某点,优先走左或是右.如果左右都走不通,再考虑向前.绝对不能往 ...
- HDU 2579 (记忆化BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2579 题目大意:走迷宫.对于障碍点,只有当前(dep+1)%k才能走,问最少时间. 解题思路: 只有 ...
随机推荐
- 时间复杂度&空间复杂度
时间复杂度 参考链接: http://univasity.iteye.com/blog/1164707 空间复杂度 http://blog.csdn.net/booirror/article/deta ...
- SQL数据库基础(四)
聚合函数:sum,avg,max,min,count 使用方法示例: group by 分组的使用方法 分组的练习: 数学函数:ABS.ceiling.floor.power.round.sqrt. ...
- SQL 语句日期用法及函数
SQL 语句日期用法及函数 --DAY().MONTH().YEAR()——返回指定日期的天数.月数.年数:select day(cl_s_time) as '日' from class --返回天 ...
- AngularJS的指令用法
scope的绑定策略: @ :把当前属性作为字符串传递,你还可以绑定来自外层scope的值,在属性值中插入 {{}}即可 示例代码: scopeAt.html <!doctype html> ...
- 使用Sharepoint定时运行Excel中宏程序
需求:因为Excel中数据量很大,其中包含了几个宏程序从其他数据源读取数据,运行一次宏需要比较长的时间,为了不影响使用,要求每天半夜运行一次Excel中的宏(无参数),Excel存放在共盘上. 解决方 ...
- 在 SharePoint 2013 中配置 Office Web Apps
原文发布于 2012 年 7 月 23 日(星期一) 如您所知或您即将知道,SharePoint 2013 中的 Office Web Apps 不再是 SharePoint 场中的服务应用程序.相反 ...
- 在SharePoint中无代码开发InfoPath应用: 获取当前用户信息
很多种不同的场景下,会需要得到当前的用户信息,例如需要根据当前用户判断组,进而控制权限. 首先InfoPath提供了一个userName方法,来实现这个目的,不过这个方法的问题是只能获得不包含域名的用 ...
- APP icon 自动来做,photoshop 做圆角图片
项目上传到应用市场,没有美工配合,那就只能自己捉刀了. 有几个点快捷键要注意,对使用ps有帮助 (1)ctrl+enter 建立选区 (2)建立选区后,移动到另外的图层,按delete键就为删除 (3 ...
- Mac 下使用sourcetree操作git教程
SourceTree 是 Windows 和Mac OS X 下免费的 Git 和 Hg 客户端,同时也是Mercurial和Subversion版本控制系统工具.支持创建.克隆.提交.push.pu ...
- UIViewController的edgesForExtendedLayout属性
UIViewController的edgesForExtendedLayout属性 想必大家都遇到一种情况,明明y坐标设置的是0,但是总是被讨厌的导航栏给遮住.比如下面这个情况: UILabel *l ...