(离线处理+BFS) poi Tales of seafaring

Tales of seafaring

Memory limit: 128 MB

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

Input

In the first line of the standard input, there are three integers, , and  (). 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  lines that follow specify the waterways. A single waterway‘s description consists of a single line that contains two integers,  and  (), separated by a single space; these specify the numbers of ports at the two ends of this particular waterway.

The  lines that follow specify the tales that Bytensson has heard. A single tale‘s description consists of a single line with three integers, , and  (), separated by single spaces. These indicate that the tale‘s protagonist set sail from port no. , ended the journey in port no. , and sailed exactly  times through various waterways.

In tests worth 50% of the total points, the additional condition  holds.

Output

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

Example

For the input data:

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

the correct result is:

TAK
NIE
TAK
NIE

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
int n,m,k;
vector<int> e[5005];
struct node
{
    int id,s,t,w,ans;
}query[1000010];
bool vis[5005];
int dist[5005][2];
bool cmp1(node a,node b)
{
    return a.s<b.s;
}
bool cmp2(node a,node b)
{
    return a.id<b.id;
}
void bfs(int s)
{
    memset(dist,-1,sizeof(dist));
    dist[s][0]=0;
    queue<int> q;
    q.push(s),q.push(0);
    while(!q.empty())
    {
        int x,flag;
        x=q.front(),q.pop();
        flag=q.front(),q.pop();
        for(int i=0;i<e[x].size();i++)
        {
            int v=e[x][i];
            if(dist[v][flag^1]==-1)
            {
                dist[v][flag^1]=dist[x][flag]+1;
                q.push(v);
                q.push(flag^1);
            }
        }
    }
}
int main()
{
    scanf("%d%d%d",&n,&m,&k);
    for(int i=1;i<=m;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        e[x].push_back(y);
        e[y].push_back(x);
        vis[x]=vis[y]=1;
    }
    for(int i=1;i<=k;i++)
    {
        query[i].id=i;
        int s,t,w;
        scanf("%d%d%d",&s,&t,&w);
        query[i].s=s,query[i].t=t,query[i].w=w;
    }
    sort(query+1,query+1+k,cmp1);
    for(int i=1;i<=k;i++)
    {
        int temp=i;
        bfs(query[i].s);
        while(i<=k&&query[i].s==query[temp].s)
        {
            if(query[i].s==query[i].t&&!vis[query[i].s])
                query[i].ans=0;
            else
            {
                if(query[i].w&1)
                {
                    if(query[i].w>=dist[query[i].t][1]&&dist[query[i].t][1]!=-1)
                        query[i].ans=1;
                    else
                        query[i].ans=0;
                }
                else
                {
                    if(query[i].w>=dist[query[i].t][0]&&dist[query[i].t][0]!=-1)
                        query[i].ans=1;
                    else
                        query[i].ans=0;
                }
            }
            i++;
        }
        i--;
    }
    sort(query+1,query+1+k,cmp2);
    for(int i=1;i<=k;i++)
    {
        if(query[i].ans)
            printf("TAK\n");
        else
            printf("NIE\n");
    }
    return 0;
}

  

Sample grading tests:

  • 1ocen, each pair of ports connected, a million random tales, each with answer TAK;
  • 2ocen, journeys possible between ports of the same parity, a million random tales, half with answer TAK, half with answer NIE.
时间: 2024-10-07 01:27:39

(离线处理+BFS) poi Tales of seafaring的相关文章

【BZOJ3417】Poi2013 Tales of seafaring 分层图BFS

[BZOJ3417]Poi2013 Tales of seafaring Description 一个n点m边无向图,边权均为1,有k个询问 每次询问给出(s,t,d),要求回答是否存在一条从s到t的路径,长度为d 路径不必是简单路(可以自交) 2<=N<=5000,1<=M<=5000,1<=K<=1000000,1<=d<=1000000000 Sample Input 8 7 4 1 2 2 3 3 4 5 6 6 7 7 8 8 5 2 3 1 1

ACM/ICPC 之 BFS(离线)+康拓展开 (HDU1430-魔板)

魔板问题,一道经典的康拓展开+BFS问题,为了实现方便,我用string类来表示字符串,此前很少用string类(因为不够高效,而且相对来说我对char数组的相关函数比较熟),所以在这里也发现了很多容易被忽视的问题. 对于康拓展开不太熟系的可以先参看一篇博客:http://blog.csdn.net/zhongkeli/article/details/6966805 关于sting类,大家要注意,在赋值的时候,其赋值位置不能与首位置间存在未赋值部分. 题目需要转换思路的地方是: 我们需要将起始魔

Educational Codeforces Round 1(D. Igor In the Museum) (BFS+离线访问)

题目链接:http://codeforces.com/problemset/problem/598/D 题意是 给你一张行为n宽为m的图 k个询问点 ,求每个寻问点所在的封闭的一个上下左右连接的块所能看到的壁画有多少(大概这样吧). 我的做法是bfs(dfs也可以)这个为'.'的点,要是遇到上下左右其中有'*'的话就加起来.要是每次询问然后bfs一下肯定超时,所以我用一个ans[1005][1005]记录每个点的值,ok[1005][1005]数组判断是否访问过,初始化为false.然后开始遍历

Web GIS离线解决方案

1.背景 在离线环境下(局域网中)的GIS系统中如何使用地图?这里的地图主要指的是地图底图,有了底图切片数据,我们就可以看到地图,在上面加上自己的业务数据图层,进行相关操作. 要在离线环境下看到GIS地图,就要有底图切片数据,地图的底图切片数据在一定时间内是不会变化的,可以使用一些地图下载器下载地图切片,如这个地图下载器. 在CS系统中可以基于GMap.Net来做,参考<百度谷歌离线地图解决方案>. 下面介绍下Web系统如何使用GIS切片数据,开发web GIS系统. 2.使用GeoWebCa

[BZOJ 3531] [Sdoi2014] 旅行 【离线+LCT】

题目链接:BZOJ - 3531 题目分析 题目询问一条路径上的信息时,每次询问有某种特定的文化的点. 每个点的文化就相当于一种颜色,每次询问一条路径上某种颜色的点的信息. 可以使用离线算法, 类似于“郁闷的小 J ” 那道题目.将各种操作和询问按照颜色为第一关键字,时间为第二关键字排序. 那么修改颜色的操作就相当于在原颜色中是删点,在新颜色中是加点. 处理完一种颜色的操作后,要将这个颜色的点都做一次删除操作,这样,对于处理下一种颜色,树就又是空的了. 这种题,思考的时候有点晕,写代码的时候非常

[原博客] POI系列(1)

正规.严谨.精妙. -POI 发现POI(波兰信息学奥赛)的题都很有意思.于是开刷bzoj上的poi题目(按ac人数降序..).顺手写一写题解,加深印象. 为了防止一篇文章过于长,打算每五道题另起一篇文章. BZOJ 1103 : [POI2007]大都市meg 给一棵树,每次可以把树上的一些边标记了,问一个点与根之间需要走多少没有标记的边. 这个可以把这颗树遍历得到一个dfs序每第一次经过一个点的时候记录为+1,第二次经过一个点的时候记录为-1,然后记录每个点第一次经过时在序列里的位置f[i]

WHYZOJ-#95 大逃亡(二分+BFS)(好题!!!)

[题目描述]: 给出数字N,X,Y,代表有N个敌人分布一个X行Y列的矩阵上,矩形的行号从0到X-1,列号从0到Y-1再给出四个数字x1,y1,x2,y2,代表你要从点(x1,y1)移到(x2,y2).在移动的过程中你当然希望离敌人的距离的最小值最大化,现在请求出这个值最大可以为多少,以及在这个前提下,你最少要走多少步才可以回到目标点.注意这里距离的定义为两点的曼哈顿距离,即某两个点的坐标分为(a,b),(c,d),那么它们的距离为|a-c|+|b-d|. [输入描述]: 第一行给出数字N,X,Y

框架 day50 BOS项目 4 批量导入(ocupload插件,pinyin4J)/POI解析Excel/Combobox下拉框/分区组合条件分页查询(ajax)/分区数据导出(Excel)

知识点: 批量导入(ocupload插件,pinyin4J /POI解析Excel(apache POI) /区域分页查询 /Combobox下拉框 /分区组合条件分页查询(ajax) /分区数据导出(Excel下载) BOS项目笔记第4天 1.    区域批量导入功能 *Ajax不支持文件上传. *上传并且不刷新上传页面原理: Target到一个0,0,0的隐藏iframe里,造成一个没有刷新的假象 <form target="myIframe" action="ab

bzoj1878: [SDOI2009]HH的项链(主席树/离线+BIT)

 这题有离线和在线两种做法.  离线:将查询区间按左端点排序,预处理出所有数下一次的出现位置,一开始将所有第一次出现的数a[i]++,之后当扫到这个数的时候a[next[i]]++,相当于差分,给之后的位置答案+1,因为查询区间左端点排序了,所以再也查不到当前点,这个数对答案有贡献的区间只有右端点在这个数下一次出现的位置右边的区间,当扫到查询区间左端点时当前答案为sum(r)-sum(l-1). #include<iostream> #include<cstdlib> #inclu