• 题意:有\(n\)个房间,在这些房间中两两连\(m\)次条边,问除了第一个房间,其他房间走到第一个房间的最短路径,输出这个房间所连的上一个房间,如果走不到,输出\(no\).

  • 题解:刚开始我写了一个dfs,结果竟然编译不了(段错误),稍加分析了一下,发现样例1中成环了,然后又在纸上画了画,发现可以用bfs来求解,直接上bfs的板子就行了.

  • 代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <stack>
    #include <queue>
    #include <vector>
    #include <map>
    #include <set>
    #include <unordered_set>
    #include <unordered_map>
    #define ll long long
    #define fi first
    #define se second
    #define pb push_back
    #define me memset
    const int N = 1e6 + 10;
    const int mod = 1e9 + 7;
    using namespace std;
    typedef pair<int,int> PII;
    typedef pair<long,long> PLL; int n,m;
    int u,v;
    int ans[N];
    vector<int> s[N]; void bfs(){
    queue<int> q;
    q.push(1);
    ans[1]=1;
    while(!q.empty()){
    int tmp=q.front();
    q.pop();
    for(auto w:s[tmp]){
    if(ans[w]==0){
    ans[w]=tmp;
    q.push(w);
    }
    }
    }
    } int main() {
    ios::sync_with_stdio(false);cin.tie(0);
    cin>>n>>m;
    for(int i=1;i<=m;++i){
    cin>>u>>v;
    s[u].pb(v);
    s[v].pb(u);
    }
    bfs(); for(int i=1;i<=n;++i){
    if(ans[i]==0){
    puts("No");
    return 0;
    }
    }
    puts("Yes");
    for(int i=2;i<=n;++i) printf("%d\n",ans[i]); return 0;
    }

最新文章

  1. JavaScript标准参考教材(alpha)--笔记
  2. mha配置参数详解
  3. zendStudio 10.5破解
  4. MyEclipse + Tomcat 热部署问题
  5. Delphi thread exception mechanism
  6. MYSQL内存
  7. 在Eclipse中使用Github(EGit)
  8. spoj 1557 GSS3 - Can you answer these queries III 线段树
  9. 将sublimeText添加到鼠标右键菜单栏
  10. poj 2945 Find the Clones
  11. Linux编程 21 shell编程(环境变量,用户变量,命令替换)
  12. 2019.02.21 bzoj2829: 信用卡凸包(凸包)
  13. WebFrom 小程序【分页功能 】
  14. 深入云存储系统Swift核心组件:Ring实现原理剖析
  15. Paper Reading - Im2Text: Describing Images Using 1 Million Captioned Photographs ( NIPS 2011 )
  16. linnx常用命令学习
  17. SQLServer -- 竟然默认不区分大小写
  18. 牧场行走(LCA)
  19. 【转】 Pro Android学习笔记(五四):调试和分析(2):View层次结构
  20. ckeditor添加日历控件

热门文章

  1. 基于numpy.einsum的张量网络计算
  2. 利用Numpy求解投资内部收益率IRR
  3. Linux—curl命令讲解
  4. React 入门-redux 和 react-redux
  5. linux中的虚拟环境工具
  6. 理解js闭包9大使用场景
  7. Go - httpclient 常用操作
  8. ElasticSearch-IK分词器和集成使用
  9. Redis二进制安全
  10. 如何应对C语言内存泄露! 华为开发者社区 2020-09-29