首页 > 其他分享 >天梯赛练习题 L3-008 喊山(bfs)

天梯赛练习题 L3-008 喊山(bfs)

时间:2023-04-10 21:35:04浏览次数:47  
标签:练习题 dist int LL 喊山 bfs vis while const

https://pintia.cn/problem-sets/994805046380707840/exam/problems/994805050709229568

输入样例:
7 5 4
1 2
2 3
3 1
4 5
5 6
1 4 5 7
输出样例:
2
6
4
0
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18,MINN=-MAXN,INF=0x3f3f3f3f;
const LL N=1e6+10,M=2023;
const LL mod=100000007;
const double PI=3.1415926535;
#define endl '\n'
LL n,m,k,maxn=0,a[N];
LL dist[N];
vector<LL> v[N];
bool vis[N];
int bfs(int x)
{
    memset(vis,false,sizeof vis);
    memset(dist,0,sizeof dist);
    queue<LL> q;
    q.push(x);
    dist[x]=0;
    vis[x]=true;
    while(q.size())
    {
        auto t=q.front();
        q.pop();
        for(int i=0;i<v[t].size();i++)
        {
            if(vis[v[t][i]]==true) continue;
            vis[v[t][i]]=true;
            q.push(v[t][i]);
            dist[v[t][i]]=dist[t]+1;
            maxn=max(maxn,dist[v[t][i]]);
        }
    }
}
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    while(T--)
    {
        cin>>n>>m>>k;
        while(m--)
        {
            LL x,y;
            cin>>x>>y;
            v[x].push_back(y);
            v[y].push_back(x);
        }
        while(k--)
        {
            LL x;
            cin>>x;
            if(v[x].size()==0)
            {
                cout<<"0"<<endl;
                continue;
            }
            maxn=0;
            bfs(x);
            for(int i=1;i<=n;i++)
            {
                if(dist[i]==maxn)
                {
                    cout<<i<<endl;
                    break;
                }
            }
        }
    }
    return 0;
}

标签:练习题,dist,int,LL,喊山,bfs,vis,while,const
From: https://www.cnblogs.com/Vivian-0918/p/17304367.html

相关文章

  • 天梯赛练习题 L3-004 肿瘤诊断(bfs)
    https://pintia.cn/problem-sets/994805046380707840/exam/problems/994805052626026496输入样例:3452111111111111001100110011101101000000101100000000000100011000输出样例:26LLdz[]={1,-1,0,0,0,0},dx......
  • spfa求最短路——BFS,数组实现邻接表,数组实现队列
    题目描述题目来源AcWing给定一个n个点m条边的有向图,图中可能存在重边和自环,边权可能为负数。请你求出1号点到n号点的最短距离,如果无法从1号点走到n号点,则输出impossible。数据保证不存在负权回路。输入格式第一行包含整数n和m。接下来m行每行包含三个......
  • CSAPP练习题2.11
    练习题2.111/*2CSAPP练习题2.11,并做了一些扩展3指定或者用户输入一个数组(100以内),打印反转前后的所有数组元素4*/5#include<stdio.h>67voidinplace_swap(int*x,int*y);//互换值8voidreverse_array(inta[],intcnt);//数组反转9voi......
  • java -- 练习题
    第一题1.定义一个Person类,要求有姓名和年龄,并且符合JavaBean标准,定义Student类继承Person,定义测试类,创建Student对象,要求创建Student对象的同时,指定Student对象的姓名为"张三",只能指定姓名不许指定年龄classPerson{privateStringname;privateintage;......
  • Leetcode(剑指offer专项训练)——DFS/BFS专项(2)
    课程顺序题目现在总共有numCourses 门课需要选,记为 0 到 numCourses-1。给定一个数组 prerequisites,它的每一个元素 prerequisites[i] 表示两门课程之间的先修顺序。 例如 prerequisites[i]=[ai,bi] 表示想要学习课程ai ,需要先完成课程bi 。请根据给出的......
  • mysql 查询练习题
    1.查出至少有一个员工的部门。显示部门编号、部门名称、部门位置、部门人数。selectd.deptno,d.dname,d.loc,r.countfromdeptd,(selectdeptno,count(*)countfromempgroupbydeptno)rwhered.deptno=r.deptno;2.列出薪金比smith高的所有员工。select*fro......
  • Leetcode(剑指offer专项训练)——DFS/BFS专项(1)
    计算除法题目给定一个变量对数组equations和一个实数值数组values作为已知条件,其中equations[i]=[Ai,Bi]和values[i]共同表示等式Ai/Bi=values[i]。每个Ai或Bi是一个表示单个变量的字符串。另有一些以数组queries表示的问题,其中queries[j]=[Cj,Dj]......
  • 结对编程——四则运算练习题
    结对编程题目如下:小学老师要每周给同学出300道四则运算练习题。这个程序有很多种实现方式:C/C++C#/VB.net/JavaExcelUnixShellEmacs/Powershell/VbscriptPerlPython一个或两个运算符(a+b或a+b+c),100以内的数字,不需要写答案。需要检查答案是否正确,并且保证答案在0......
  • NFS练习题
    NFS练习题1.开放/nfs/share目录,提供给任意用户只读(/etc/exportsro)查询1.任意客户端2.任意的用户​​​​ 服务端showmoutexportfssystemctlstartnfs 修改了nfs配置文件,需要重启什么吗?修改了nfs配置文件,只需要让nfs重新读取该配置文件即可,你都不需要重新,因为你......
  • C++ Primer 第五版 第十一章 练习题编程题目答案
    https://github.com/jzplp/Cpp-Primer-Answer练习11.1map用关键字索引,是一个字典。vector用整数索引,是一个列表。练习11.2list链表vector顺序列表deque双端队列map字典set集合练习11.311.3map单词计数程序代码练习11.411.4去标点map单词计数程序代码练习11.5如果关键......