BZOJ3417: Poi2013 Tales of seafaring

来源:互联网 发布:卫星电视接收软件 编辑:程序博客网 时间:2024/04/26 12:09

易水人去,明月如霜。

Description

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.


一个nm边无向图,边权均为1,有k个询问

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

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

Input

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.

Output

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).

Sample Input

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

Sample Output

TAK
NIE
TAK
NIE

HINT

思路:

对于两个点之间的最短距离假设是X,那么X+2就一定可以到达(考虑在两个结点之间来回走),同理X+4,X+。。。都可以啦

代码:

#include <cstdio>#include <cstring>#include <iostream>#include <cmath>#include <algorithm>#include <queue>#define maxn 50005*2using namespace std;int read(){    char ch;int s=0,f=1;ch=getchar();    while(ch>'9'||ch<'0') { if(ch=='-') f*=-1;ch=getchar(); }    while(ch>='0'&&ch<='9') {s=s*10+ch-48;ch=getchar(); }    return s*f;} int n,m,cnt; struct node {  int y,nxt; }e[maxn*2]; int head[maxn],q[maxn],d[maxn][2]; int lk[maxn]; int K; int ans[maxn*10]; struct edge{  int x,y,d,id; }a[maxn];  void insert(int u,int v)  {      cnt++;      e[cnt].y=v,e[cnt].nxt=head[u];      head[u]=cnt;  }  bool cmp(const edge &a1,const edge &b1)  {      return a1.x<b1.x;  }  int f[maxn];  void bfs(int x)  {      memset(d,-1,sizeof(d));      int head1=0,tail=1;      q[0]=x,f[0]=0,d[x][0]=0;      while(head1!=tail)      {          int now = q[head1]; int flag=f[head1++];          int to=flag^1;          for(int i=head[now];i;i=e[i].nxt)          {              if(d[e[i].y][to]==-1){d[e[i].y][to]=d[now][flag]+1;q[tail]=e[i].y;f[tail]=to;tail++;}          }      }  }int main(){ n=read(),m=read(),K=read(); for(int i=1;i<=m;i++) {     int u,v;u=read(),v=read();     insert(u,v),insert(v,u);     lk[u]=1,lk[v]=1; } for(int i=1;i<=K;i++) {     a[i].x=read(),a[i].y=read(),a[i].d=read(),a[i].id=i; } sort(a+1,a+1+K,cmp); int now=1;    for(int k=1;k<=K;k=now+1,now=k)    {while(a[now+1].x==a[k].x&&now<K)now++;bfs(a[now].x);for(int t=k;t<=now;t++){int x=a[t].x,y=a[t].y,v=a[t].d,id=a[t].id;if(x==y&&!lk[x])continue;int dis=d[y][v&1];if(dis!=-1&&v>=dis)ans[id]=1;}    }    for(int i=1;i<=K;i++)if(ans[i])puts("TAK");else puts("NIE"); return 0;}