Atcoder Beginner Contest 168 D - .. (Double Dots) (BFS)
2024-09-02 05:40:37
题意:有\(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;
}
最新文章
- JavaScript标准参考教材(alpha)--笔记
- mha配置参数详解
- zendStudio 10.5破解
- MyEclipse + Tomcat 热部署问题
- Delphi thread exception mechanism
- MYSQL内存
- 在Eclipse中使用Github(EGit)
- spoj 1557 GSS3 - Can you answer these queries III 线段树
- 将sublimeText添加到鼠标右键菜单栏
- poj 2945 Find the Clones
- Linux编程 21 shell编程(环境变量,用户变量,命令替换)
- 2019.02.21 bzoj2829: 信用卡凸包(凸包)
- WebFrom 小程序【分页功能 】
- 深入云存储系统Swift核心组件:Ring实现原理剖析
- Paper Reading - Im2Text: Describing Images Using 1 Million Captioned Photographs ( NIPS 2011 )
- linnx常用命令学习
- SQLServer -- 竟然默认不区分大小写
- 牧场行走(LCA)
- 【转】 Pro Android学习笔记(五四):调试和分析(2):View层次结构
- ckeditor添加日历控件