题目描述

Young Bytensson loves to hang out in the port tavern, where he often listens to the sea dogs telling their tales of seafaring. Initially, he believed them all, however incredible they sounded. Over time though, he became suspicious. He has decided to write a program that will verify if there may be any grain of truth in those tall stories. Bytensson reasoned that while he cannot tell if the sailors indeed weathered all those storms, he can at least find out if their travel itineraries make sense. This is a task for a programmer, which Bytensson, unfortunately, is not. Help him out!
There are   ports and   waterways connecting them in the waters frequented by the sailors Bytensson listened to. If there is a waterway between two ports, then sailing from one to the other is possible. Any waterway can be sailed in both directions.
Bytensson got to know K seafaring tales. Each tells of a sailor who began his journey in one port, sailed a number of waterways, and ended up in another port, which may have been the one he initially set sail from. The sailor in question may have sailed through the same waterway many times, each time in any direction.

一个n点m边无向图,边权均为1,有k个询问

每次询问给出(s,t,d),要求回答是否存在一条从s到t的路径,长度为d

路径不必是简单路(可以自交)

输入

In the first line of the standard input, there are three integers, N,M and K (2<=N<=5000,1<=M<=5000,1<=K<=1000000) These denote, respectively: the number of ports in the waters frequented by the sailors who told Bytensson their stories, the number of waterways, and the number of tales.
The M lines that follow specify the waterways. A single waterway's description consists of a single line that contains two integers, a and b (1<=a,b<=N,a<>b) separated by a single space; these specify the numbers of ports at the two ends of this particular waterway.
The K lines that follow specify the tales that Bytensson has heard. A single tale's description consists of a single line with three integers, s,t  and d (1<=S,T<=N,1<=d<=1000000000) separated by single spaces. These indicate that the tale's protagonist set sail from port no. s, ended the journey in port no. t, and sailed exactly d times through various waterways.

输出

Your program should print exactly K lines to the standard output; the i-th of them should contain the word TAK (Polish for yes) if the journey described in the i-th tale (in input order) could have taken place. If it could not, then the line should contain the word NIE(Polish for no).

样例输入

8 7 4
1 2
2 3
3 4
5 6
6 7
7 8
8 5
2 3 1
1 4 1
5 5 8
1 8 10

样例输出

TAK
NIE
TAK
NIE

提示

 
 
因为不必是简单路径,所以可以走最短路到达终点后在最后一条边来回走。
但最后来回走一条路的路径长是偶数,因此要维护每个点的奇偶最短路。
因为询问次数较多,但点数比较小,离线以每个点为起点bfs求一下最短路即可。
注意询问的s和t可能相同,需要判一下这个点有没有出度。
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
int n,m,k;
int head[5002];
int to[100010];
int next[100010];
int f[5002][2];
int x,y;
vector<int>v[5002];
int ans[1000010];
struct node
{
int s;
int t;
int d;
}a[1000010];
int tot;
queue< pair<int,int> >q;
void add(int x,int y)
{
tot++;
next[tot]=head[x];
head[x]=tot;
to[tot]=y;
}
int main()
{
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
for(int i=1;i<=k;i++)
{
scanf("%d%d%d",&a[i].s,&a[i].t,&a[i].d);
v[a[i].s].push_back(i);
}
for(int i=1;i<=n;i++)
{
if(v[i].size())
{
memset(f,-1,sizeof(f));
f[i][0]=0;
q.push(make_pair(i,0));
while(!q.empty())
{
int now=q.front().first;
int dis=q.front().second;
q.pop();
for(int j=head[now];j;j=next[j])
{
if(f[to[j]][dis^1]==-1)
{
f[to[j]][dis^1]=f[now][dis]+1;
q.push(make_pair(to[j],dis^1));
}
}
}
int len=v[i].size();
for(int j=0;j<len;j++)
{
if(f[a[v[i][j]].t][a[v[i][j]].d&1]!=-1&&f[a[v[i][j]].t][a[v[i][j]].d&1]<=a[v[i][j]].d&&head[i]!=0)
{
ans[v[i][j]]=1;
}
}
}
}
for(int i=1;i<=k;i++)
{
ans[i]==1?printf("TAK\n"):printf("NIE\n");
}
}

BZOJ3417[Poi2013]Tales of seafaring——BFS的更多相关文章

  1. BZOJ3417 : Poi2013 Tales of seafaring

    若x到y走k步可行,那么走k+2步也可行 以每个点为起点,BFS处理出到每个点走了奇数步.偶数步的最短路 对于一次询问,如果d不小于相应奇偶性的最短路,则可行 特判:对于孤立点,无论怎么走都不可行 # ...

  2. 【BZOJ3417】Poi2013 Tales of seafaring 分层图BFS

    [BZOJ3417]Poi2013 Tales of seafaring Description 一个n点m边无向图,边权均为1,有k个询问 每次询问给出(s,t,d),要求回答是否存在一条从s到t的 ...

  3. bzoj3417:[POI2013]MOR-Tales of seafaring

    传送门 这个题比较水,很容易看出 1.最短路小于d,直接看奇偶性就好了 2,最短路大于d,puts("NIE\n"); 主要就是判奇偶性的问题,将每个点拆成奇点和偶点跑bfs就行了 ...

  4. 【BZOJ3417】[POI2013]MOR-Tales of seafaring (最短路SPFA)

    [POI2013]MOR-Tales of seafaring 题目描述 一个n点m边无向图,边权均为1,有k个询问 每次询问给出(s,t,d),要求回答是否存在一条从s到t的路径,长度为d 路径不必 ...

  5. [POI2013]MOR-Tales of seafaring

    题目 思博题,发现一旦路径太长我们可以来回走最后一条边,但是这样并不能改变路径长度的奇偶性 所以求一下所有点之间奇最短路和偶最短路就好了,直接暴力\(BFS\)即可 有一个烦人的特判 代码 #incl ...

  6. POI2013题解

    POI2013题解 只做了BZ上有的\(13\)道题. 就这样还扔了两道神仙构造和一道计算几何题.所以只剩下十道题了. [BZOJ3414][Poi2013]Inspector 肯定是先二分答案,然后 ...

  7. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  8. [POI2013]Morskie opowieści

    [POI2013]Morskie opowieści 题目大意: 一个\(n(n\le5000)\)点\(m(m\le5000)\)边无向图,边权均为\(1\),有\(k(k\le10^6)\)个询问 ...

  9. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

随机推荐

  1. ESP NVS

    简介:NVS的主要功能是:存储键值(存在flash上面): NVS利用spi_flash_{read|write|erase}这些API来操作数据在内存上的删改写,内存上data类型nvs子类型所代表 ...

  2. TCP/IP协议---广播和多播及IGMP协议

    老板找某个高层谈话,这是一对一形式.当老板叫来所有高层谈话,那么就变为了一对多.计算机网络中也是如此,当一个主机需要和更多机器对话时,就有了广播和多播这种形式. 广播和多播仅应用于UDP,它们对需将报 ...

  3. 使用yield返回IEnumber<T>集合

    yield是对一种复杂行为的简化,就是将一段代码简化为一种简单的形式. 先看一下常规的写法,下面例子中,把找出字符串阵列中,某些元素包含有某些字符的元素. class Bi { public stri ...

  4. LiveCharts文档-2FAQ

    原文:LiveCharts文档-2FAQ LiveCharts文档-2FAQ 原文链接 LiveCharts基于的平台有WPF,UWP,WinForms:语言是C#, FAQ: 我怎么转换一个char ...

  5. Jmeter(三十五)_精确实现网页爬虫

    Jmeter实现了一个网站文章的爬虫,可以把所有文章分类保存到本地文件中,并以文章标题命名 它原理就是对网页提交一个请求,然后把返回的所有值提取出来,利用ForEach控制器去实现遍历.下面来介绍一下 ...

  6. ExtJS初探:了解 Ext Core

    Ext Core是一款和jQuery媲美的轻型JS库,基于MIT许可.对于Dom的操作,我个人还是比较喜欢用jQuery.当然如果项目中用的是ExtJS框架,也就没必要多引用一个jQuery,Ext ...

  7. Git版本控制器使用总结性梳理

    Git为何物?Git 是什么?大家肯定会说不就是版本控制器嘛,是的Git是目前世界上最先进的分布式版本控制系统(没有之一).1)那什么是版本控制器?举个简单的例子,比如我们用Word写文章,那你一定有 ...

  8. SQLServer 中发布与订阅

    在对数据库做迁移的时候,会有很多方法,用存储过程,job,也可以用开源工具kettle,那么今天这些天变接触到了一种新的方法,就是SqlServer中自带的发布与订阅. 首先说明一下数据复制的流程.如 ...

  9. 第三周作业————————word count

    #include <stdio.h> void main() { FILE *fp; , str, word, pu, ch; int g; str = ; word = ; pu = ; ...

  10. Linux内核分析(第七周)

    可执行程序的装载 一.预处理.编译.链接和目标文件的格式 1.可执行程序怎么来的? 预处理: gcc -E -o hello.cpp hello.c -m32 *负责把include的文件包含进来及宏 ...