您的位置:首页 > 其它

csu 1356 Catch bfs(vector)

2014-03-10 21:59 411 查看

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[100002];
int num[100002];
int vis[100002];
bool use[100002];
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]=0;
while( !H.empty() )
{
x=H.front();
H.pop();
cur=vis[x];
if(cur==0) cur=1;
else if(cur==1) cur=0;

for(i=0;i<num[x];i++)
{
now=Q[x][i];
if(use[now]==true)
{
if(  (vis[now]==1&&cur==0 ) || (  vis[now]==0 && cur==1 ))
{// 我很奇怪,为什么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=1;t<=T;t++)
{
scanf("%d%d%d",&n,&m,&s);
memset(num,0,sizeof(num));
memset(vis,-1,sizeof(vis));
memset(use,false,sizeof(use));
for(i=0;i<=n;i++)
Q[i].clear();
for(i=1;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 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: