HDU 4460 Friend Chains(BFS)

Vue3 - vite 项目报错:Uncaught ReferenceError: require is not defined(解决 vue3 vite 项目无法识别 require 语法导致报错) vue3,vite,报错解决,vue3+vite报错requireisnotdefined,vue3vite引插件不识别require,老插件必须用require怎么办,vite项目插件要求使用require引入但报错,vue3不兼容require导致第三方包插件无法使用,vue3vite中使用require报错怎么办,解决Vite+vue3使用第三方依赖报错“requireisnotdefined”问题,vite项目require语法兼容问题解决requireisnotdefine 阅读详情

Friend Chains

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4943    Accepted Submission(s): 1603


Problem Description
For a group of people, there is an idea that everyone is equals to or less than 6 steps away from any other person in the group, by way of introduction. So that a chain of "a friend of a friend" can be made to connect any 2 persons and it contains no more than 7 persons.
For example, if XXX is YYY’s friend and YYY is ZZZ’s friend, but XXX is not ZZZ's friend, then there is a friend chain of length 2 between XXX and ZZZ. The length of a friend chain is one less than the number of persons in the chain.
Note that if XXX is YYY’s friend, then YYY is XXX’s friend. Give the group of people and the friend relationship between them. You want to know the minimum value k, which for any two persons in the group, there is a friend chain connecting them and the chain's length is no more than k .
 

Input
There are multiple cases.
For each case, there is an integer N (2<= N <= 1000) which represents the number of people in the group.
Each of the next N lines contains a string which represents the name of one people. The string consists of alphabet letters and the length of it is no more than 10.
Then there is a number M (0<= M <= 10000) which represents the number of friend relationships in the group.
Each of the next M lines contains two names which are separated by a space ,and they are friends.
Input ends with N = 0.
 

Output
For each case, print the minimum value k in one line.
If the value of k is infinite, then print -1 instead.
 

Sample Input
3 XXX YYY ZZZ 2 XXX YYY YYY ZZZ 0
 

Sample Output
2
 题目大意:给出n个点m条边,求出所有任意两点间最短路的最大值。
思路:用BFS,求出每个点到其他所有点的最短路,最后遍历取最大值即可。
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
using namespace std;

const int maxn = 1000 + 10;
const int INF = 1e9+10;
map<string, int> ID;
vector<string> names;
int id, n, m, dist[maxn][maxn], vis[maxn];
vector<int> con[maxn];

int getID(string &s)
{
    if (ID.count(s)) return ID[s];
    else return ID[s] = id++;
}

void bfs(int s, int *d)
{
    queue<int> q;
    while(!q.empty()) q.pop();
    memset(vis, 0, sizeof vis);
    for (int i = 1; i <= n; i++) d[i] = INF;
    d[s] = 0;
    vis[s] = 1;
    q.push(s);
    while(!q.empty()) {
        int m = q.front(); q.pop();
        for (int i = 0; i < con[m].size(); i++) {
            int x = con[m][i];
            if (vis[x]) continue;
            vis[x] = 1;
            d[x] = d[m]+1;
            q.push(x);
        }
    }
}

int main()
{
    while(scanf("%d",&n) == 1 && n) {
        ID.clear();
        names.clear();
        for (int i = 1; i <= n; i++) con[i].clear();
        id = 1;
        string s;
        for (int i = 0; i < n; i++) {
            cin >> s;
            getID(s);
        }
        scanf("%d",&m);
        while(m--) {
            string s1,s2;
            cin >> s1 >> s2;
            int x = getID(s1), y = getID(s2);
            con[x].push_back(y);
            con[y].push_back(x);
        }
        for (int i = 1; i <= n; i++) bfs(i, dist[i]);
        int ans = -1;
        for (int i = 1; i <= n; i++)
            for (int j = i+1; j <= n; j++)
               ans = max(ans, dist[i][j]);
        printf("%d\n",ans == INF ? -1 : ans);
    }
}


AI测试:手把手教你用AI做桌面自动化「喂饭教程」 本文介绍了基于MCP协议的DesktopCommanderMCP自动化工具,通过自然语言指令实现桌面操作。系统采用客户端-服务器架构,支持应用程序管理、文件操作、屏幕控制等功能。详细说明了环境搭建步骤,包括Python和Node.js依赖安装、API密钥配置及MCP服务部署。通过实战案例展示了文件整理、批量截图等典型应用场景,并分享了任务描述优化、权限控制等最佳实践。该方案能显著提升重复性桌面操作的效率,让AI成为智能办公助手。 阅读详情

相关推荐

别再只调电位器了!用单片机PWM控制LM317输出电压,精度和玩法都多了

本文探讨了如何利用单片机PWM控制LM317输出电压,替代传统的电位器调节方案。通过PWM信号的有源滤波和运放处理,实现了更高精度(±5mV)、更好温度稳定性(0.01%/°C)的数控稳压电源设计,支持远程控制和可编程曲线输出,为经典电路注入数字化智能。

weixin_29164497的博客 360

HDU - 4460 Friend Chains(邻接表&BFS)

小白日记.2 HDU - 4460 Friend Chains 题目大意:如果两个人互为朋友则距离为一,距离可累加,求一群人中任意两个人之间的最短距离的最大值。 常见的BFS 利用邻接表剪枝 代码如下: #include <iostream> #include <stdio.h> #include <string.h> #include <queue&gt...

m0_46313334的博客 501

MIL-STD-1553B协议解析与工程实践指南

MIL-STD-1553B作为军用航空电子系统的核心通信协议,采用命令/响应式串行总线架构,通过双冗余设计实现高可靠性数据传输。该协议定义了三类终端设备(总线控制器、远程终端、总线监视器)和六种标准消息格式,典型应用包括飞行控制系统、导航设备等关键航电组件的实时数据交互。深入解析模式代码机制和状态字位域定义,其中传输向量字(10000)和同步命令(00001)等关键指令在分布式系统时间对齐、故障诊断等场景发挥重要作用。工程实践中需特别注意78Ω终端阻抗匹配、4-12μs响应时间窗口等硬性指标,结合变压器耦合

weixin_33431149的博客 436

HDU-4460 Friend Chains (BFS+字符串编号)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4460 “首先数据库处理数字比字符要快,在正常情况下查询数字就比查询字符要快 对数字建立索引也要比字符快 另外对数字的大小比对,如果拿到程序短来比对还要转型,在数据库端比对也挺麻烦” 这是引用了别人的话,我觉得这就是为什么直接用string存取会TML而编号之后存取可以过的一部分原因吧。 参考文章:ht...

TK_wang_的博客 316

从零开始:使用CMake构建GreenHills RH850项目的完整工作流(2018.1.5版)

本文详细介绍了如何从零开始使用CMake构建GreenHills RH850嵌入式项目的完整工作流。通过编写工具链文件、定义CMakeLists.txt,实现自动化、可维护的跨平台构建,有效解决了传统IDE在多模块、持续集成场景下的局限性,显著提升团队协作效率和项目可维护性。

weixin_28839699的博客 130

用友U8库龄分析避坑指南:物料出入库日期计算的5个常见错误

本文深入剖析了在用友U8系统中进行库龄分析时,物料出入库日期计算的五个常见陷阱。从数据源选择、业务类型过滤、计价方式逻辑、期初数据处理到查询性能优化,逐一解析错误根源并提供避坑指南,帮助企业获得准确的库龄数据,有效管理库存与资金风险。

weixin_29209801的博客 456

【笔记】frechet distance(弗朗明歇距离):最大的最短距离

最大的最短距离

nyist_yangguang的博客 1779

WOFOST模型参数调优实战:从‘跑不通’到‘预测准’的避坑指南

本文详细介绍了WOFOST模型参数调优的实战技巧,帮助用户从模型报错诊断到参数敏感性分析,再到土壤-作物参数联调策略,逐步解决‘跑不通’到‘预测准’的问题。通过Python和PCSE工具,提供参数扰动测试模板和关键参数优先级矩阵,助力作物模型的高效校准与预测。

weixin_32597009的博客 623

HDU-#4460 Friend Chains(bfs)

题目大意:给出n个人以及m种朋友关系,求一个系数k,使得任意两人的关系长度不会超过k。         解题思路:最容易想到的是Floyd算法,求出每两人之间的的距离,然后更新最长关系值。但在这样O(N^3)会超时,这里可以用bfs去跑出所有点的距离,然后再判断。也可利用SPFA算法进行求解,这与bfs的写法相差不大。不过还有大神用了字典树来写,实在是高呀。这里给出前两种的code。    

VoidSun 592

【100%通过率】华为OD机试真题 C 实现【最多等和不相交连续子序列】【2022.11 Q4 新题】

给定一个数组,我们称其中连续的元素为连续子序列,称这些元素的和为连续子序列的和。数组中可能存在几组连续子序列,组内的连续子序列互不相交且有相同的和。求一组连续子序列,组内子序列的数目最多。输出这个数目。

MISAYAONE的博客 1万+

Flink Gauss CDC:深度剖析存量与增量同步的创新设计

开发Flink Gauss CDC是为了适配华为自研GaussDB的特殊日志格式和逻辑复制机制,解决Flink CDC无法直接支持的问题,同时优化存量与增量同步的效率和一体化读取能力,满足GaussDB复合主键处理、数据落库等定制化需求,以实现高效、准确的数据同步。

数据与算法架构提升之路专栏 1985

hdu 4460 Friend Chains (BFS)

简单BFS #include #include #include #include #include using namespace std; const int maxn = 1005; int n , ans , e , m; int head[maxn] , vis[maxn]; bool flag; struct T{ int u , v , next; }edge[m

yyg的专栏 2436

【肌电处理】肌电信号EMG积分肌电值+均方根值+中值频率+平均功率频率【含Matlab源码 3386期】

肌电信号EMG积分肌电值+均方根值+中值频率+平均功率频率 完整的代码,方可运行;可提供运行操作视频!适合小白!

订阅付费专栏Matlab(奶茶价版),可赠送奶茶价版付费专栏指定代码1份; 823

HDU4460 LA6378 Friend Chains【BFS】

Friend Chains Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6960 Accepted Submission(s): 2249 Problem Description For a group of people, t...

海岛Blog 667

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

Friend Chains bfs hdu-4460

题面链接:Problem - 4460 题面: 题意:有n个人,m条关系,每条关系给定两个人,表示这两个人是朋友,但是朋友的朋友不是朋友关系,现在求k的最小值,任意两个人的朋友链的长度都小于k(也就是求任意两个人的朋友链的最大长度),如果有两个人之间没有朋友链就输出-1 思路:刚开始我题目看一半以为是并查集(最近并查集刷的比较多),但是看完题目就觉得惯性思维了,然后我就开始纠结是dfs,还是最短路,然而这不是单源最短路,但是我会的dij写法铁t,我就一直往dfs上面想,但是还是t,后来我发现.

m0_55682843的博客 313

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

HDU 4460 Friend Chains 【bfs】【spfa】

点击打开链接 题意,求两两朋友之间的最短路中的最大值。 我一直理解错题意,能写过才奇怪了!!!!, 题解:用bfs  把每个能走的朋友都走一遍,然后记录最远的距离。     最后求最大值。 #include #include #include #include #include #include #include #include using namespace st

zoro_n的博客 388

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

hdu 4460 Friend Chains(bfs)

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4460题意:给出n个点m条边的关系网,求一个最小的k使得任意两个人之间的距离不大于k。其中,2≤n≤10002 \le n \le 1000。想法:此题出在了我们的组队赛里,比赛时刚看觉得只是单纯地Floyd最短路,后来发现n的范围有点大,想法就被否了。后来发现我们并不需要那么多次操作,事实上

你是我天边最美的云彩~~ 556

HDU 4460 Friend Chains ( BFS )

每个点求一次最短路,在最短路中找最长的。 用邻接表存边,尽量不要用STL,很容易超时。 #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> using namespace std; const int MAXN =...

aegt43853的博客 194

hdu 4460 Friend Chains(BFS)

题目链接:hdu 4460 Friend Chains 题目大意:给定N个点和M条边,求出两点之间的最短距离的最大值。 解题思路:N才1000,枚举点然后做BFS。 #include #include #include #include #include #include #include #include using namespace std; const i

不慌不忙、不急不躁 1107

HDU 4460 Friend Chains BFS 最短路

题意:给出N个人,和他们之间的朋友关系。求出

leodestiny 661

【 HDU - 4460 】 G - Friend Chains (BFS)

For a group of people, there is an idea that everyone is equals to or less than 6 steps away from any other person in the group, by way of introduction. So that a chain of “a friend of a friend” can be made to connect any 2 persons and it contains no more

黑桃 361

HDU 4460 Friend Chains 【最短路(bfs优化)】

传送门 // 题意: 给定n个人的名字以及他们的m个关系, 任意两个人的距离为他们人数最少的关系链中-1, 然后问这幅图中最长的距离是多少.// 思路: 很明显是求任意两点之间的最短路最长是多少., 但是floyd(O(^3))会T. 跑n次优化的dij(O(n^2logn))也会T, 然后我们可以发现他们的边长都为1, 所以我们用bfs来跑最短路, 这样的复杂度只有O(n^2), 因为bfs跑最

Anxdada -- 我等风来也等你 888

HDU 4460Friend Chains(对每个点BFS)

Friend Chains Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2040    Accepted Submission(s): 712 Problem Description For a group o

布生气 1759

HDU 4460 Friend Chains --BFS

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

whatbeg 445

hdu_4460 Friend Chains bfs+vector

#include #include #include #include #include #include #include #include using namespace std; #define inf 0x7f7f7f7f #define N 1010 mapma; vectorg[N]; int dis[N][N]; queue p; bool vis[N]; void bfs(int

DieOrThink 609

HDU 4460 Friend Chains(任意两点最短路的最大值 bfs或spfa)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4460 题意:求任意两点间最短路,输出这些最短路中的最长距离。为了不超时,进行n次bfs取最大值即可。详解请见代码。 #include #include #include #include #include #include #include using namespace std; typed

smwqd_yehua_cx的博客 545
上一篇: UVa 1151 Buy or Build (最小生成树)
下一篇: CodeForces 589B Layer Cake(枚举)
Interstellar_
博客等级 码龄10年 8粉丝 121原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值