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 ...
随机推荐
- clion windows 开发配置
1.下载clion 并且安装. 地址 : http://download-cf.jetbrains.com/cpp/clion-1.0.1.exe 2.安装cygwin 地址: https://cy ...
- Wmware Center安装过程记录
前几天在服务器中安装了Vmware Esxi,以及在客户端中安装了Vmware Client,因此希望能够装一下Vmware vCenter Server试验一下,结果并不顺利,在此记录. 从官网 ...
- CSS基础知识学习笔记
1.css基本样式讲解 1.1 css背景background-attachment:背景图像是否固定或者随着页面的其余部分滚动background-color:设置元素的背景颜色background ...
- winform中的checkedListbox数据源绑定
首先看清楚一点 winform下该控件的名称叫做:checkedListbox webform下叫做CheckBoxList 不知道这样起名的用意何在,这个别管了,看看用法吧. web下很简单,直接设 ...
- Java Excel
http://www.cnblogs.com/mingforyou/archive/2013/08/26/3282922.html
- ***常见复杂SQL语句(含统计类SQL)
1.SQL统计某字段的出现次数 比如统计某个表中,姓名出现的次数:select name,count(*) from biao group by name having count(*) > 2 ...
- hdu 1796 How many integers can you find
容斥原理!! 这题首先要去掉=0和>=n的值,然后再使用容斥原理解决 我用的是数组做的…… #include<iostream> #include<stdio.h> #i ...
- Java 连接SQLite数据库
下载jar包: http://www.sqlite.com.cn/Upfiles/source/sqlitejdbc-v033-nested.tgz public class TestSQLite { ...
- 为什么Android 3.0如此罕见?
3.0(2011年2月)代号蜂巢,专用于android系统的平板电脑,不用于手机.4.0(2011年5月公布)的开发就是让平板电脑和手机能够共用一个版本的系统.4.0通用于平板电脑和手机.
- H5 移动Web框架集合
http://frozenui.github.io/ 一个简洁的h5前端框架 http://weui.github.io/weui/ 腾讯出的WebUI 风格是基于微信