题意:给定 n 个人,和关系,问你这个朋友圈里任意两者之间最短的距离是多少。

析:很明显的一个BFS,只要去找最长距离就好。如果不能全找到,就是-1.

代码如下:

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <cstring>
  8. #include <set>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <map>
  13.  
  14. using namespace std ;
  15. typedef long long LL;
  16. typedef pair<int, int> P;
  17. const int INF = 0x3f3f3f3f;
  18. const double inf = 0x3f3f3f3f;
  19. const double eps = 1e-8;
  20. const int maxn = 1e3 + 5;
  21. const int dr[] = {0, 0, -1, 1};
  22. const int dc[] = {-1, 1, 0, 0};
  23. int n, m;
  24. inline bool is_in(int r, int c){
  25. return r >= 0 && r < n && c >= 0 && c < m;
  26. }
  27. map<string, int> id;
  28. vector<int> G[maxn];
  29. int getid(const string &s){
  30. return id[s];
  31. }
  32. int vis[maxn];
  33. int vvis[maxn];
  34. int d[maxn];
  35.  
  36. int bfs(int rt){
  37. queue<int> q;
  38. q.push(rt);
  39. memset(vis, 0, sizeof(vis));
  40. memset(d, -1, sizeof(d));
  41. vis[rt] = vvis[rt] = 1;
  42. int ans = rt;
  43. d[rt] = 0;
  44. int mmax = 0;
  45. while(!q.empty()){
  46. int u = q.front(); q.pop();
  47. for(int i = 0; i < G[u].size(); ++i){
  48. int v = G[u][i];
  49. if(vis[v]) continue;
  50. vis[v] = vvis[v] = 1;
  51. d[v] = d[u] + 1;
  52. if(mmax < d[u] + 1){
  53. mmax = d[u] + 1;
  54. ans = v;
  55. }
  56. q.push(v);
  57. }
  58. }
  59. return ans;
  60. }
  61.  
  62. int solve(int i){
  63. int u = bfs(i);
  64. int v = bfs(u);
  65. return d[v];
  66. }
  67.  
  68. int main(){
  69. while(scanf("%d", &n) == 1 && n){
  70. id.clear();
  71. string s, s1, s2;
  72. for(int i = 1; i <= n; ++i){
  73. cin >> s;
  74. id[s] = i;
  75. G[i].clear();
  76. }
  77. scanf("%d", &m);
  78. for(int i = 0; i < m; ++i){
  79. cin >> s1 >> s2;
  80. int u = getid(s1);
  81. int v = getid(s2);
  82. G[u].push_back(v);
  83. G[v].push_back(u);
  84. }
  85. if(m + 1 < n){ printf("-1\n"); continue; }
  86. memset(vvis, 0, sizeof(vvis));
  87. int ans = 0;
  88. for(int i = 1; i <= n; ++i)
  89. if(!vvis[i]) ans = max(ans, solve(i));
  90. for(int i = 1; i <= n; ++i)
  91. if(d[i] == -1){ ans = -1; break; }
  92. printf("%d\n", ans);
  93. }
  94. return 0;
  95. }

HDU 4460 Friend Chains (BFS,最长路径)的更多相关文章

  1. HDU 4460 Friend Chains --BFS

    题意:问给定的一张图中,相距最远的两个点的距离为多少.解法:跟求树的直径差不多,从1 开始bfs,得到一个最远的点,然后再从该点bfs一遍,得到的最长距离即为答案. 代码: #include < ...

  2. HDU 4460 Friend Chains(map + spfa)

    Friend Chains Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total ...

  3. HDU 4460 Friend Chains

    Problem Description For a group of people, there is an idea that everyone is equals to or less than ...

  4. HDU 1069 Monkey and Banana / ZOJ 1093 Monkey and Banana (最长路径)

    HDU 1069 Monkey and Banana / ZOJ 1093 Monkey and Banana (最长路径) Description A group of researchers ar ...

  5. Going from u to v or from v to u? POJ - 2762(强连通 有向最长路径)

    In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has n rooms, an ...

  6. hdoj 2196 Computer【树的直径求所有的以任意节点为起点的一个最长路径】

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  7. ubuntu 终端设置(颜色与长路径)

    Linux给人最大的享受就是可以根据个人喜好去定制令自己舒服的系统配置,像终端颜色的设置就是一个典型的例子. 图1 系统默认状态下的终端显示     在没有经过自定义配置的终端下工作久了,难免容易疲劳 ...

  8. Codefroces Gym 100781A(树上最长路径)

    http://codeforces.com/gym/100781/attachments 题意:有N个点,M条边,问对两两之间的树添加一条边之后,让整棵大树最远的点对之间的距离最近,问这个最近距离是多 ...

  9. 【POJ 3162】 Walking Race (树形DP-求树上最长路径问题,+单调队列)

    Walking Race   Description flymouse's sister wc is very capable at sports and her favorite event is ...

随机推荐

  1. 【转载】Morris遍历二叉树 & BST(二叉搜索树) Traverse & 空间O(1) 时间O(n)

    因为做一道Leetcode的题目(前面博客有:link),需要用Space O(1)空间复杂度来中序遍历树, 看了Discuss,也上网搜了一下,发现空间O(1)可以用 Morris遍历的方法.方法介 ...

  2. poj 3790 Recursively Palindromic Partitions (递推)

    题目 题意:求输入的数字的递归回文. 思路:答案等于这个数字一半之前的所有的 之和. #include <iostream> #include <cstdio> #includ ...

  3. bzoj2829

    裸题,直接上凸包,然后加上一个圆周即可 只是在这之前没写过旋转而已 const pi=3.14159265358979323; eps=1e-8; type point=record x,y:doub ...

  4. codevs 1171 潜伏者

    要是NOIP自己这样水就完了... 仔细啊!!!! #include<iostream> #include<cstdio> #include<cstring> #i ...

  5. vs2013编译boost库

    打开vs2013>>visual studio tools>>VS2013 x64 本机工具命令提示 cd D:\lib\boost_1_55_0\boost_1_55_0 b ...

  6. 让你的 Node.js 应用跑得更快的 10 个技巧(转)

    Node.js 受益于它的事件驱动和异步的特征,已经很快了.但是,在现代网络中只是快是不行的.如果你打算用 Node.js 开发你的下一个Web 应用的话,那么你就应该无所不用其极,让你的应用更快,异 ...

  7. php.ini配置中文详解

    ;;;;;;;;;;; ; 警告 ; ;;;;;;;;;;; ; 此配置文件是对于新安装的PHP的默认设置. ; 默认情况下,PHP使用此配置文件安装 ; 此配置针对开发目的,并且*不是*针对生产环境 ...

  8. django - get_or_create() 使用提醒

    [omron - debug] user_id建表的时候,不能使用unique,因为一个用户,可能有多个product_id,相对应的是,get_or_create()中的查询参数,如果在建表中有un ...

  9. 【转】Git 少用 Pull 多用 Fetch 和 Merge

    原文网址:http://www.cnblogs.com/flying_bat/p/3408634.html 本文有点长而且有点乱,但就像Mark Twain Blaise Pascal的笑话里说的那样 ...

  10. PHP经验集锦

    最近刚刚完成手中的项目,比较闲.来这儿转转,把积累的一些技巧分享给大家!1.关于PHP重定向 方法一:header("Location: index.php"); 方法二:echo ...