Gym - 101875L PC is for kicking【bfs】

参考大佬的博客写的很好https://blog.csdn.net/gtgym321/article/details/90145411

题目

传送门
在这里插入图片描述

Input
5 2
1 2
1 3
2 5
3 4
Output
4

题意:给出两个数n,m,下面是n-1行,每行有两个数表示连通,从m点开始走最多能走过几个点(不能重复)

思路bfs遍历

AC code

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<sstream>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
vector<int>s[120000];
int vis[120000],ans=0;
struct gg
{
    int p,q;
}g;
queue<gg>que;
void bfs(int x)
{
    memset(vis,0,sizeof(vis));
    vis[x]=1;
    g.p=x;
    g.q=1;
    que.push(g);
    while(!que.empty())
    {
        que.pop();
        ans=max(ans,g.q);
        int f=g.p;
        g.q++;
        for(int i=0;i<s[f].size();i++)
           if(!vis[s[f][i]])
               {
                   vis[s[f][i]]=1;
                   g.p=s[f][i];
                   que.push(g);
               }
        g=que.front();
    }
}
int main()
{
    ios::sync_with_stdio(0);
    int n,m,a,b;
    cin>>n>>m;
    for(int i=0;i<n-1;i++)
       {
           cin>>a>>b;
           s[a].push_back(b);
           s[b].push_back(a);
       }
    bfs(m);
    printf("%d\n",ans);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值