[题目链接]

http://uoj.ac/problem/19

[算法]

首先,在反向图上从终点广搜,求出每个点是否可以在答案路径中

然后在正向图中求出源点至终点的最短路,同样可以使用广搜

时间复杂度 : O(N)

[代码]

#include<bits/stdc++.h>
using namespace std;
#define MAXN 10010
#define MAXM 200010 struct edge
{
int to,nxt;
} e[MAXM << ]; int i,n,m,u,v,s,t,tot;
int head[MAXN],rhead[MAXN],deg[MAXN];
bool flag[MAXN]; namespace IO
{
template <typename T> inline void read(T &x)
{
int f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar())
{
if (c == '-') f = -f;
}
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
template <typename T> inline void write(T x)
{
if (x < )
{
putchar('-');
x = -x;
}
if (x > ) write(x / );
putchar(x % + '');
}
template <typename T> inline void writeln(T x)
{
write(x);
puts("");
}
} ;
inline void addedge(int u,int v)
{
tot++;
e[tot] = (edge){v,head[u]};
head[u] = tot;
}
inline void addredge(int u,int v)
{
tot++;
e[tot] = (edge){v,rhead[u]};
rhead[u] = tot;
}
inline void bfs1(int s)
{
int i,u,v,l,r;
static int q[MAXN],cnt[MAXN];
static bool visited[MAXN];
for (i = ; i <= n; i++) cnt[i] = ;
q[l = r = ] = s;
flag[s] = true;
while (l <= r)
{
u = q[l];
l++;
for (i = rhead[u]; i; i = e[i].nxt)
{
v = e[i].to;
if (!visited[v])
{
visited[v] = true;
q[++r] = v;
}
cnt[v]++;
}
}
for (i = ; i <= n; i++) flag[i] = (cnt[i] == deg[i]);
}
inline int bfs2(int s)
{
int i,u,v,l,r;
static int q[MAXN],dist[MAXN];
static bool visited[MAXN];
if (!flag[s]) return -;
for (i = ; i <= n; i++) visited[i] = false;
q[l = r = ] = s;
dist[s] = ;
visited[s] = true;
while (l <= r)
{
u = q[l];
l++;
for (i = head[u]; i; i = e[i].nxt)
{
v = e[i].to;
if (!visited[v] && flag[v])
{
visited[v] = true;
dist[v] = dist[u] + ;
q[++r] = v;
}
}
}
return dist[t] > ? dist[t] : -;
} int main()
{ IO :: read(n); IO :: read(m);
for (i = ; i <= m; i++)
{
IO :: read(u); IO :: read(v);
addedge(u,v);
addredge(v,u);
deg[u]++;
}
IO :: read(s); IO :: read(t);
bfs1(t);
IO :: writeln(bfs2(s)); return ; }

[NOIP 2014] 寻找道路的更多相关文章

  1. [Luogu P2296][NOIP 2014]寻找道路

    emmm交了第8次才过. 这道题目测一道单源最短路问题,因此dijkstra或者spfa板子先准备好.因为题中对最短路有限定: 路径上的所有点的出边所指向的点都直接或间接与终点连通. 在满足条件1的情 ...

  2. NOIp 2014 寻找道路【图的遍历/最短路】By cellur925

    题目传送门 比较裸的图论,结果自己还是没做出来,我真菜. 我们根据题意,只要把不能通向终点的点求出,然后再分别以这些点为起点,求出它们能到达的点,这些点也不能在路径上. 之后跑一个最短路即可. 注意以 ...

  3. niop 2014寻找道路

    /* 乍一看就是个最短路 SFPA 但是要保证路径上的所有点的出边所指向的点都直接或间接与终点连通. 这一点就蛋疼了0.0 开始想的是正着跑一边 每一个点的所有边都能符合条件 那这个点就符合条件0.0 ...

  4. Codevs 3731 寻找道路 2014年 NOIP全国联赛提高组

    3731 寻找道路 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 在有向图G中,每条边的长度均为1,现给定起点和终点,请你在图中找 ...

  5. noip寻找道路

    题目描述 在有向图G 中,每条边的长度均为1 ,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 1 .路径上的所有点的出边所指向的点都直接或间接与终点连通. 2 .在满足条 ...

  6. NOIP2014 寻找道路

    2.寻找道路 (road.cpp/c/pas) [问题描述] 在有向图G中,每条边的长度均为1,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 1.路径上的所有点的出边所指 ...

  7. NOIP 2014 提高组 题解

    NOIP 2014 提高组 题解 No 1. 生活大爆炸版石头剪刀布 http://www.luogu.org/problem/show?pid=1328 这是道大水题,我都在想怎么会有人错了,没算法 ...

  8. NOIP 2014

    Prob.1 生活大爆炸版 石头剪刀布 模拟.代码: #include<cstdio> #include<cstring> #include<iostream> u ...

  9. Luogu 1351 NOIP 2014 联合权值(贪心,计数原理)

    Luogu 1351 NOIP 2014 联合权值(贪心,计数原理) Description 无向连通图 G 有 n 个点,n-1 条边.点从 1 到 n 依次编号,编号为 i 的点的权值为 Wi, ...

随机推荐

  1. JS高级——词法作用域

    作用域 1.js中没有块级作用域 2.如果有块级作用域,那么下面代码将会是undefined undefined <script> for (var i = 0; i < 10; i ...

  2. 史上巨坑: vim的"set foldmethod=syntax"设置竟然是导致ctrl+p(ctrl+n)补全在文件稍大时光标位于中间位置补全效率变慢的元凶!

    最近我的vim又让我闹心了. 问题出现在supertab的补全速度上, 有时候按下tab键半天才弹出补全列表, 即便是弹出了列表在列表上下移动也变得的相当缓慢, 这让我的很是蛋疼. 在完全无法接受这个 ...

  3. privot函数使用

    语法: table_source PIVOT( 聚合函数(value_column) FOR pivot_column IN(<column_list>) ) 将列转化为行 写个小示例 : ...

  4. CDR如何使用钢笔工具进行完美抠图?【6·18特惠倒计时!】

    不要以为抠图只能在图像处理软件中实现,矢量图形绘制软件CorelDRAW一样可以,而且方法很多,文章介绍使用CDR钢笔工具抠图的方法. 提示说明: 首先说明一下,CDR中的钢笔工具和其他平面设计软件中 ...

  5. 入口文件 index.php

    一. 运行流程 The index.php serves as the front controller, initializing the base resources needed to run ...

  6. codevs 3385 拯救Oier(一) Save Oier—first

    3385 拯救Oier(一) Save Oier—first 传送门  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 青铜 Bronze 题解       题目描述 Descr ...

  7. sql 区分大小写查询

    sql 区分大小写查询 select * FROM [Users] where userName collate Chinese_PRC_CS_AS='ADMIN'

  8. Android实现ViewPager无限循环滚动回绕

     Android实现ViewPager无限循环滚动回绕 Android系统提供的ViewPager标准方式是左右可以自由滑动,但是滑动到最左边的极限位置是第一个page,滑动到最右边的位置是最后一 ...

  9. EntityFramewordCore 2.2 DBFirst简单使用

    如何用EF Core连接数据库并且生成实体类? 1.通过Nuget安装依赖 Install-package Microsoft.EntityFrameworkCore Install-package ...

  10. UVa - 11452 - Dancing the Cheeky-Cheeky

    先上题目: F. Dancing the Cheeky-Cheeky  Context The Cheeky-Cheeky is a new song. They dance it in Mula, ...