题目

思博题,发现一旦路径太长我们可以来回走最后一条边,但是这样并不能改变路径长度的奇偶性

所以求一下所有点之间奇最短路和偶最短路就好了,直接暴力\(BFS\)即可

有一个烦人的特判

代码

#include<bits/stdc++.h>
#define mp std::make_pair
#define re register
inline int read() {
char c=getchar();int x=0;while(c<'0'||c>'9') c=getchar();
while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+c-48,c=getchar();return x;
}
const int maxn=5005;
struct E{int v,nxt;}e[maxn<<1];
struct Ask{int x,y,d,rk;}q[1000005];
int head[maxn],d[maxn][2],vis[maxn][2];
int n,m,num,Q,Ans[1000005],p[maxn];
std::queue< std::pair<int,int> > qt;
inline int cmp(const Ask &A,const Ask &B) {return A.x<B.x;}
inline void add(int x,int y) {e[++num].v=y;e[num].nxt=head[x];head[x]=num;}
inline void BFS(int s) {
memset(vis,0,sizeof(vis));
memset(d,20,sizeof(d));
vis[s][0]=1,d[s][0]=0;
qt.push(mp(s,0));
while(!qt.empty()) {
int k=qt.front().first,o=qt.front().second;
qt.pop();
for(re int i=head[k];i;i=e[i].nxt)
if(!vis[e[i].v][o^1]) {
vis[e[i].v][o^1]=1;
d[e[i].v][o^1]=d[k][o]+1;
qt.push(mp(e[i].v,o^1));
}
}
}
inline void solve(int L,int R) {
BFS(q[L].x);
for(re int i=L;i<=R;i++) Ans[q[i].rk]=(q[i].d>=d[q[i].y][q[i].d&1]);
for(re int i=L;i<=R;i++) if(q[i].x==q[i].y&&!p[q[i].x]&&q[i].d>0) Ans[q[i].rk]=0;
}
int main() {
n=read(),m=read();Q=read();
for(re int x,y,i=1;i<=m;i++)
x=read(),y=read(),add(x,y),add(y,x),p[x]=p[y]=1;
for(re int i=1;i<=Q;i++) q[i].x=read(),q[i].y=read(),q[i].d=read(),q[i].rk=i;
for(re int i=1;i<=Q;i++) if(q[i].x>q[i].y) std::swap(q[i].x,q[i].y);
std::sort(q+1,q+Q+1,cmp);
int l=1;
for(re int i=2;i<=Q;i++) if(q[i].x!=q[i-1].x) solve(l,i-1),l=i;
solve(l,Q);
for(re int i=1;i<=Q;i++) puts(Ans[i]?"TAK":"NIE");
return 0;
}

[POI2013]MOR-Tales of seafaring的更多相关文章

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

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

  2. BZOJ3417[Poi2013]Tales of seafaring——BFS

    题目描述 Young Bytensson loves to hang out in the port tavern, where he often listens to the sea dogs te ...

  3. BZOJ3417 : Poi2013 Tales of seafaring

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

  4. POI2013题解

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

  5. bzoj AC倒序

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

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

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

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

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

  8. [POI2013]Łuk triumfalny

    [POI2013]Łuk triumfalny 题目大意: 一棵\(n(n\le3\times10^5)\)个结点的树,一开始\(1\)号结点为黑色.\(A\)与\(B\)进行游戏,每次\(B\)能选 ...

  9. [POI2013]Polaryzacja

    [POI2013]Polaryzacja 题目大意: 给定一棵\(n(n\le250000)\)个点的树,可以对每条边定向成一个有向图,这张有向图的可达点对数为树上有路径从\(u\)到达\(v\)的点 ...

随机推荐

  1. NX二次开发-UFUN读取本地文本文档uc4514a

    1 NX9+VS2012 2 3 #include <uf.h> 4 #include <uf_cfi.h> 5 #include <uf_ui.h> 6 7 us ...

  2. 2018-2019-2-20175323 java实验五 网络编程与安全

    20175323 java实验五 网络编程与安全 任务一 ①编写MyBC.java实现中缀表达式转后缀表达式的功能 ②编写MyDC.java实现从上面功能中获取的表达式中实现后缀表达式求值的功能 基本 ...

  3. MFC-CString与int互相转化

    1. CString转int ; CString str = _T("123"); n = _ttoi(str); 2. int转CString ; CString str; st ...

  4. Python中的startswith和endswith函数使用实例

    Python中的startswith和endswith函数使用实例 在Python中有两个函数分别是startswith()函数与endswith()函数,功能都十分相似,startswith()函数 ...

  5. AsyncCallback IAsyncResult

    using System; using System.Threading; using System.Collections.Generic; using System.Windows.Forms; ...

  6. function attributes, MDK

    The keyword format is either of the following: __attribute__((attribute1, attribute2, ...)) __attrib ...

  7. 三极管NPN和PNP开关电路

    0. 总结 NPN适合做低端驱动,即PN结在下面(低端),发射极E接地. PNP适合做高端驱动,即PN结在上面(高端),发射极E接VCC. Tips:标箭头的PN结,中间的是基极B,外头是E极. 1. ...

  8. liunx crontab 参数代表含义

    * * * * * (下面的字体对应) 分钟 小时 几号 月份 星期几 星号(*):代表所有可能的值,例如month字段如果是星号,则表示在满足其它字段的制约条件后每月都执行该命令操作. 逗号(,): ...

  9. HashMap底层实现原理及面试问题

    ①HashMap的工作原理 HashMap基于hashing原理,我们通过put()和get()方法储存和获取对象.当我们将键值对传递给put()方法时,它调用键对象的hashCode()方法来计算h ...

  10. Activiti学习笔记6 — 变量与历史记录

    一. 变量的使用 1.创建流程引擎对象 private ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); ...