1356: Catch

Time Limit: 2 Sec  Memory Limit: 128 MB
Submit:
96  Solved: 40
[Submit][Status][Web
Board
]

Description

A thief is running away!
We can consider
the city where he locates as an undirected graph in which nodes stand for
crosses and edges stand for streets. The crosses are labeled from 0 to N–1.

The tricky thief starts his escaping from cross S. Each moment he moves to
an adjacent cross. More exactly, assume he is at cross u at the moment t. He may
appear at cross v at moment t + 1 if and only if there is a street between cross
u and cross v. Notice that he may not stay at the same cross in two consecutive
moment.
The cops want to know if there’s some moment at which it’s possible
for the thief to appear at any cross in the city.

Input

The input contains multiple test cases:
In
the first line of the input there’s an integer T which is the number of test
cases. Then the description of T test cases will be given.
For any test
case, the first line contains three integers N (≤ 100 000), M (≤ 500 000), and
S. N is the number of crosses. M is the number of streets and S is the index of
the cross where the thief starts his escaping.
For the next M lines, there
will be 2 integers u and v in each line (0 ≤ u, v < N). It means there’s an
undirected street between cross u and cross v.

Output

For each test case, output one line to tell
if there’s a moment that it’s possible for the thief to appear at any cross.
Look at the sample output for output format.

Sample Input

2
3 3 0
0 1
0 2
1 2
2 1 0
0 1

Sample Output

Case 1: YES
Case 2: NO

HINT

For the first case, just look at the table below. (YES means the thief may appear at the cross at that moment)

For the
second input, at any moment, there’s at least one cross that the thief can’t
reach.

 
题意:T组测试数据
n,m  ,S   代表n个节点 下表0~n-1 ;  m条边。s代表起始点。
有个人,在s点走,记为moment 0 ,此时只可能出现在s点。
下一刻,只能到其相邻的点,不能在原地不动。
 
问是否满足,有一时刻,他可能出现在任何一点。
 
思路:对于某一个,如果它能在奇数时间,和偶是时间到达的话,就满足条件。
        问题转化为寻找一个奇数环,来延迟时间。切换奇偶时间。
 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std; vector<int>Q[];
int num[];
int vis[];
bool use[];
queue<int>H; void add(int x,int y)
{
num[x]++;
Q[x].push_back(y);
}
bool bfs(int start)
{
int i,cur,now,x;
H.push(start);
use[start]=true;
vis[start]=;
while( !H.empty() )
{
x=H.front();
H.pop();
cur=vis[x];
if(cur==) cur=;
else if(cur==) cur=; for(i=;i<num[x];i++)
{
now=Q[x][i];
if(use[now]==true)
{
if( (vis[now]==&&cur== ) || ( vis[now]== && cur== ))
{// 我很奇怪,为什么if( vis[now]!=cur)就错了,
//照理说,我的vis[now]的值已经被修改过的,不是-1.
return true;
}
continue;
}
vis[now]=cur;
use[now]=true;
H.push(now);
}
}
return false;
}
int main()
{
int T,n,m,s,t;
int i,x,y;
scanf("%d",&T);
for(t=;t<=T;t++)
{
scanf("%d%d%d",&n,&m,&s);
memset(num,,sizeof(num));
memset(vis,-,sizeof(vis));
memset(use,false,sizeof(use));
for(i=;i<=n;i++)
Q[i].clear();
for(i=;i<=m;i++)
{
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
printf("Case %d: ",t);
if( bfs(s) ==true)
printf("YES\n");
else printf("NO\n");
}
return ;
}

csu 1356 Catch bfs(vector)的更多相关文章

  1. STL--向量(vector)

    STL的组成 标准模板库STL关注的重点是泛型数据结构和算法,其关键组成部分是容器(containers).算法(algorithms).迭代器(iterators).函数对象(Function Ob ...

  2. R语言编程艺术# 数据类型向量(vector)

    R语言最基本的数据类型-向量(vector) 1.插入向量元素,同一向量中的所有的元素必须是相同的模式(数据类型),如整型.数值型(浮点数).字符型(字符串).逻辑型.复数型等.查看变量的类型可以用t ...

  3. STL标准模板库 向量容器(vector)

    向量容器使用动态数组存储.管理对象.因为数组是一个随机访问数据结构,所以可以随机访问向量中的元素.在数组中间或是开始处插入一个元素是费时的,特别是在数组非常大的时候更是如此.然而在数组末端插入元素却很 ...

  4. List接口的实现类(Vector)(与ArrayList相似,区别是Vector是重量级的组件,使用使消耗的资源比较多。)

      LinkedList提供以下方法:(ArrayList无此类方法) addFirst();    removeFirst();   addLast();   removeLast(); 在堆栈中, ...

  5. R语言编程艺术#01#数据类型向量(vector)

    R语言最基本的数据类型-向量(vector) 1.插入向量元素,同一向量中的所有的元素必须是相同的模式(数据类型),如整型.数值型(浮点数).字符型(字符串).逻辑型.复数型等.查看变量的类型可以用t ...

  6. 对ArrayList(Vector)的排序

    当需要对ArrayList(Vector)里面的元素进行简单的排序时,可以使用Collections.sort();这个方法 import java.util.ArrayList; import ja ...

  7. Silverlight & Blend动画设计系列十:Silverlight中的坐标系统(Coordinate System)与向量(Vector)运动

    如果我们习惯于数学坐标系,那么对于Silverlight中的坐标系可能会有些不习惯.因为在Silverlight中的坐标系与Flash中的坐标系一样,一切都的颠倒的.在标准的数学坐标系中,X轴表示水平 ...

  8. 获取一个数组(vector)与查找值(value)的差最小绝对值的成员索引的算法

    代码如下: 函数作用:传递进来一个数组(vector),和一个需要查找的值(value),返回与value的差值绝对值最小的vector成员索引,若value不在vector范围中,则返回-1: in ...

  9. BFS(二):数的变换

    [例1]整数变换(POJ 3278 “Catch That Cow”) 给定两个整数a和b(0 ≤a,b≤100,000),要求把a变换到b.变换规则为:(1)当前数加1:(2)当前数减1:(3)当前 ...

随机推荐

  1. MongoDB 数据自动同步到 ElasticSearch

    我们产品中需要全文检索的功能,后端数据存储主要使用了 MySQL + MongoDB,而其中需要检索的内容是在 MongoDB 中的. MongoDB 本身是自带文本索引功能的,但是,不支持中文.术业 ...

  2. 1.HTML练习(二)

    一.表格练习: 1.<table>标签:声明一个表格,它的常用属性如下: border属性             定义表格的边框,设置值是数值 cellpadding属性     定义单 ...

  3. Python 资源大全

    我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列的资源整理.awesome-python 是 vinta 发起维护的 Python 资源列表,内容包括:Web框架.网络 ...

  4. iOS开发~制作同时支持armv7,armv7s,arm64,i386,x86_64的静态库.a以及 FrameWork 的创建

    armv7,armv7s,arm64,i386,x86_64 详解 一.概要 平时项目开发中,可能使用第三方提供的静态库.a,如果.a提供方技术不成熟,使用的时候就会出现问题,例如: 在真机上编译报错 ...

  5. Java多线程——对象组合

    我们不希望对每一次的内存访问都进行分析以确保程序是线程安全的,而是希望将一些现有的线程安全组件组合为更大规模的组件或者程序,这里介绍一些组合模式,这些组合模式能够使一个类更容易成为线程安全的,并且在维 ...

  6. [原创] Trie树 php 实现敏感词过滤

    目录 背景 简介 存储结构 PHP 其他语言 字符串分割 示例代码 php 优化 缓存字典树 常驻服务 参考文章 背景 项目中需要过滤用户发送的聊天文本, 由于敏感词有将近2W条, 如果用 str_r ...

  7. 了解到的Web攻击技术

    (1)XSS(Cross-Site Scripting,跨站脚本攻击): 指通过存在安全漏洞的Web网站注册用户的浏览器内运行非法的HTML标签或者JavaScript进行的一种攻击. (2)SQL注 ...

  8. 牛客Wannafly挑战赛26E 蚂蚁开会(树链剖分+线段树)

    传送门 题面描述 一颗n个节点的树,m次操作,有点权(该节点蚂蚁个数)和边权(相邻节点的距离). 三种操作: 操作1:1 i x将节点i的点权修改为x.(1 <= i <= n; 1 &l ...

  9. Go语言管道

    Channel概念 Channel 是Go中的一个核心类型,你可以把它看成一个管道.Channel是引用类型,操作符是箭头 <- . Channel 是 CSP 模式的具体实现,用于多个 gor ...

  10. java基本编译

    1.java语言严格区分大小写:1个源文件其中最多只能有一个public类:源文件必须和public类同名:每个类对应一个class字节码,且同名. 2.编译.   javac -d dir Hell ...