1651. Shortest Subchain(bfs)
终于A了
看这题容易想到最短路 看到错的很多 还特意注意了好几处
后来发现 必须按给出的顺序出边 想了想 这不就是BFS
然后就是各种细节 i->i+1ori->j(a[i]==a[j])
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<queue>
#include<vector>
using namespace std;
#define N 10010
#define INF 0xfffffff
vector<int>ed[N];
vector<int>::iterator it;
int vis[N*],pa[N*],a[N*],o[N*],f[N*];
int n,ff[N*];
struct node
{
int x,num;
};
void bfs(int s,int e)
{
int i,oo,minz = INF;
queue<node>q;
node st,te;
st.x = s;
st.num = ;
pa[s] = s;
q.push(st);
while(!q.empty())
{
te = q.front();
int u = te.x;
q.pop();
if(te.num>minz)
continue;
if(a[u]==e)
{
minz = te.num;
oo = u;
continue;
}
if(ff[u]&&!vis[ff[u]])
{
int v = ff[u];
if(!vis[v])
{
vis[v] = ;
st.x = v;
st.num = te.num;
q.push(st);
pa[v] = pa[u];
}
}
if(!vis[u+]&&u<n)
{
vis[u+] = ;
st.x = u+;
st.num = te.num+;
q.push(st);
pa[u+] = u;
}
}
int x = oo,g=;
o[g++] = a[oo];
while(x!=s)
{
x = pa[x];
o[g++] = a[x];
}
for(i = g- ; i >= ; i--)
if(o[i]!=o[i+])
printf("%d ",o[i]);
puts("");
}
int main()
{
int i;
scanf("%d",&n);
for(i =; i <= n ; i++)
{
scanf("%d",&a[i]);
if(f[a[i]])
{
ff[f[a[i]]] = i;
f[a[i]] = i;
}
else
f[a[i]] = i;
}
bfs(,a[n]);
return ;
}
1651. Shortest Subchain(bfs)的更多相关文章
- [Algorithm] BFS vs DFS
//If you know a solution is not far from the root of the tree: BFS, because it is faster to get clos ...
- algorithm@ find the shortest path in a graph using BFS
Finding Shortest Paths By BFS
- [LeetCode] 821. Shortest Distance to a Character_Easy tag: BFS
Given a string S and a character C, return an array of integers representing the shortest distance f ...
- Codeforces-A. Shortest path of the king(简单bfs记录路径)
A. Shortest path of the king time limit per test 1 second memory limit per test 64 megabytes input s ...
- Leetcode之深度+广度优先搜索(DFS+BFS)专题-934. 最短的桥(Shortest Bridge)
Leetcode之广度优先搜索(BFS)专题-934. 最短的桥(Shortest Bridge) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary ...
- LeetCode934.shortest bridge【dfs+bfs】
一.题面 在给定的二维二进制数组 A 中,存在两座岛.(岛是由四面相连的 1 形成的一个最大组.) 现在,我们可以将 0 变为 1,以使两座岛连接起来,变成一座岛. 返回必须翻转的 0 的最小数目.( ...
- cf 843 D Dynamic Shortest Path [最短路+bfs]
题面: 传送门 思路: 真·动态最短路 但是因为每次只加1 所以可以每一次修改操作的时候使用距离分层的bfs,在O(n)的时间内解决修改 这里要用到一个小技巧: 把每条边(u,v)的边权表示为dis[ ...
- [Leetcode]847. Shortest Path Visiting All Nodes(BFS|DP)
题解 题意 给出一个无向图,求遍历所有点的最小花费 分析 1.BFS,设置dis[status][k]表示遍历的点数状态为status,当前遍历到k的最小花费,一次BFS即可 2.使用DP 代码 // ...
- [LeetCode] Shortest Distance from All Buildings 建筑物的最短距离
You want to build a house on an empty land which reaches all buildings in the shortest amount of dis ...
随机推荐
- Build Settings
Add Open Scenes 选择一个关卡,使其处于打开状态,在菜单栏选择 File -> Build Settings 打开Build Settings窗口.选择 Add Open Scen ...
- php开发过程中用什么方法来加快页面的加载速度
1,数据库优化;2,php缓存;3,使用zend引擎(其它框架);4,分布式部署;5,静态
- Wireshark 入门
1.过滤目的地是百度的IP包. 百度的ip: 命令:ip.src eq 61.135.169.125 过滤ip来源是61.135.169.125 ip.dst eq 61.135.169.125 过滤 ...
- 什么是 block
什么是 block 1.提前准备好的一段可以执行的代码 2.block 可以当做参数传递 3.在需要的时候执行 block 4,block 中使用 self 时肯产生循环引用 block 做网络异步耗 ...
- CoreLocation MKMapView 地图
系统自带地图 框架: CoreLocation MapKit CLLocationManager --> 定位管理者 CLGeocoder --> 地理编码器 MKMapView -- ...
- hihocoder #1300 : 展胜地的鲤鱼旗 dp
题目链接: http://hihocoder.com/problemset/problem/1300 题解: 先用栈预处理出每个‘)’匹配的‘(’的位子,放在pos数组中. dp[i]表示以i结尾的合 ...
- SQL SERVER字符串函数
本篇文章还是学习<程序员的SQL金典>内容的记录,此次将讲解的是SQL SERVER的字符串函数. 其实数据库跟程序语言库一样,都会集成很多可以使用的API.这些API,如果你熟悉的话,将 ...
- STL中的stack(堆栈)
转载:http://blog.csdn.net/morewindows/article/details/6950881 栈(statck)这种数据结构在计算机中是相当出名的.栈中的数据是先进后出的(F ...
- Apache Options Indexes FollowSymLinks之讲解
禁止显示Apache目录列表-Indexes FollowSymLinks 如何修改目录的配置以禁止显示 Apache 目录列表. 缺省情况下如果你在浏览器输入地址: http://localhost ...
- PHPStorm 3.0 与服务器端代码同步配置
首先打开PhpStrom->Toolbar->Settings(工具栏的倒数第三行) 进入Settings后的界面如下 单击进入左边Deployment目录,在右边配置信息里面进行配置 填 ...