HDU 4460 Friend Chains (BFS,最长路径)
题意:给定 n 个人,和关系,问你这个朋友圈里任意两者之间最短的距离是多少。
析:很明显的一个BFS,只要去找最长距离就好。如果不能全找到,就是-1.
代码如下:
- #pragma comment(linker, "/STACK:1024000000,1024000000")
- #include <cstdio>
- #include <string>
- #include <cstdlib>
- #include <cmath>
- #include <iostream>
- #include <cstring>
- #include <set>
- #include <queue>
- #include <algorithm>
- #include <vector>
- #include <map>
- using namespace std ;
- typedef long long LL;
- typedef pair<int, int> P;
- const int INF = 0x3f3f3f3f;
- const double inf = 0x3f3f3f3f;
- const double eps = 1e-8;
- const int maxn = 1e3 + 5;
- const int dr[] = {0, 0, -1, 1};
- const int dc[] = {-1, 1, 0, 0};
- int n, m;
- inline bool is_in(int r, int c){
- return r >= 0 && r < n && c >= 0 && c < m;
- }
- map<string, int> id;
- vector<int> G[maxn];
- int getid(const string &s){
- return id[s];
- }
- int vis[maxn];
- int vvis[maxn];
- int d[maxn];
- int bfs(int rt){
- queue<int> q;
- q.push(rt);
- memset(vis, 0, sizeof(vis));
- memset(d, -1, sizeof(d));
- vis[rt] = vvis[rt] = 1;
- int ans = rt;
- d[rt] = 0;
- int mmax = 0;
- while(!q.empty()){
- int u = q.front(); q.pop();
- for(int i = 0; i < G[u].size(); ++i){
- int v = G[u][i];
- if(vis[v]) continue;
- vis[v] = vvis[v] = 1;
- d[v] = d[u] + 1;
- if(mmax < d[u] + 1){
- mmax = d[u] + 1;
- ans = v;
- }
- q.push(v);
- }
- }
- return ans;
- }
- int solve(int i){
- int u = bfs(i);
- int v = bfs(u);
- return d[v];
- }
- int main(){
- while(scanf("%d", &n) == 1 && n){
- id.clear();
- string s, s1, s2;
- for(int i = 1; i <= n; ++i){
- cin >> s;
- id[s] = i;
- G[i].clear();
- }
- scanf("%d", &m);
- for(int i = 0; i < m; ++i){
- cin >> s1 >> s2;
- int u = getid(s1);
- int v = getid(s2);
- G[u].push_back(v);
- G[v].push_back(u);
- }
- if(m + 1 < n){ printf("-1\n"); continue; }
- memset(vvis, 0, sizeof(vvis));
- int ans = 0;
- for(int i = 1; i <= n; ++i)
- if(!vvis[i]) ans = max(ans, solve(i));
- for(int i = 1; i <= n; ++i)
- if(d[i] == -1){ ans = -1; break; }
- printf("%d\n", ans);
- }
- return 0;
- }
HDU 4460 Friend Chains (BFS,最长路径)的更多相关文章
- HDU 4460 Friend Chains --BFS
题意:问给定的一张图中,相距最远的两个点的距离为多少.解法:跟求树的直径差不多,从1 开始bfs,得到一个最远的点,然后再从该点bfs一遍,得到的最长距离即为答案. 代码: #include < ...
- 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 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 ...
- 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 ...
- hdoj 2196 Computer【树的直径求所有的以任意节点为起点的一个最长路径】
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- ubuntu 终端设置(颜色与长路径)
Linux给人最大的享受就是可以根据个人喜好去定制令自己舒服的系统配置,像终端颜色的设置就是一个典型的例子. 图1 系统默认状态下的终端显示 在没有经过自定义配置的终端下工作久了,难免容易疲劳 ...
- Codefroces Gym 100781A(树上最长路径)
http://codeforces.com/gym/100781/attachments 题意:有N个点,M条边,问对两两之间的树添加一条边之后,让整棵大树最远的点对之间的距离最近,问这个最近距离是多 ...
- 【POJ 3162】 Walking Race (树形DP-求树上最长路径问题,+单调队列)
Walking Race Description flymouse's sister wc is very capable at sports and her favorite event is ...
随机推荐
- 【转载】Morris遍历二叉树 & BST(二叉搜索树) Traverse & 空间O(1) 时间O(n)
因为做一道Leetcode的题目(前面博客有:link),需要用Space O(1)空间复杂度来中序遍历树, 看了Discuss,也上网搜了一下,发现空间O(1)可以用 Morris遍历的方法.方法介 ...
- poj 3790 Recursively Palindromic Partitions (递推)
题目 题意:求输入的数字的递归回文. 思路:答案等于这个数字一半之前的所有的 之和. #include <iostream> #include <cstdio> #includ ...
- bzoj2829
裸题,直接上凸包,然后加上一个圆周即可 只是在这之前没写过旋转而已 const pi=3.14159265358979323; eps=1e-8; type point=record x,y:doub ...
- codevs 1171 潜伏者
要是NOIP自己这样水就完了... 仔细啊!!!! #include<iostream> #include<cstdio> #include<cstring> #i ...
- vs2013编译boost库
打开vs2013>>visual studio tools>>VS2013 x64 本机工具命令提示 cd D:\lib\boost_1_55_0\boost_1_55_0 b ...
- 让你的 Node.js 应用跑得更快的 10 个技巧(转)
Node.js 受益于它的事件驱动和异步的特征,已经很快了.但是,在现代网络中只是快是不行的.如果你打算用 Node.js 开发你的下一个Web 应用的话,那么你就应该无所不用其极,让你的应用更快,异 ...
- php.ini配置中文详解
;;;;;;;;;;; ; 警告 ; ;;;;;;;;;;; ; 此配置文件是对于新安装的PHP的默认设置. ; 默认情况下,PHP使用此配置文件安装 ; 此配置针对开发目的,并且*不是*针对生产环境 ...
- django - get_or_create() 使用提醒
[omron - debug] user_id建表的时候,不能使用unique,因为一个用户,可能有多个product_id,相对应的是,get_or_create()中的查询参数,如果在建表中有un ...
- 【转】Git 少用 Pull 多用 Fetch 和 Merge
原文网址:http://www.cnblogs.com/flying_bat/p/3408634.html 本文有点长而且有点乱,但就像Mark Twain Blaise Pascal的笑话里说的那样 ...
- PHP经验集锦
最近刚刚完成手中的项目,比较闲.来这儿转转,把积累的一些技巧分享给大家!1.关于PHP重定向 方法一:header("Location: index.php"); 方法二:echo ...