CSU - 1356 Catch(dfs染色两种写法,和hdu4751比较)
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 to N–.
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 + 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 (≤ ), M (≤ ), 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 integers u and v in each line ( ≤ 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
Sample Output
Case : YES
Case : 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.
题意:给出一个起始点,一些边,有人从这个起始点开始随意走,问在某一个时候,它是否可以处于任意位置。
思路:思考下,就可以明白,只要是一个联通图,并且存在奇数点形成的环,那么在某一个时候就可以处于任意位置。
如何判断存在一个奇数点形成的环?
染色法:就是给每个点进行标号,标为0,1如果存在一条边连接的两个点标号相同,那么就是存在一个奇数环......
第一种写法,和hdu4751比较
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 100006
#define inf 1e12
int n,m,s;
vector<int> g[N];
int color[N];
bool dfs(int u,int c){
color[u]=c;
for(int i=;i<g[u].size();i++){
int next=g[u][i];
if(color[next]!=-){
if(color[next]==c){
return true;
}
continue;
}
if(dfs(next,!c)) return true;
}
return false;
}
int main()
{
int t;
int ac=;
scanf("%d",&t);
while(t--){
for(int i=;i<N;i++){
g[i].clear();
}
scanf("%d%d%d",&n,&m,&s);
for(int i=;i<m;i++){
int u,v;
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
memset(color,-,sizeof(color));
printf("Case %d: ",++ac);
if(dfs(s,)){
printf("YES\n");
}else{
printf("NO\n");
}
}
return ;
}
第二种写法:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
#define N 100006
int n,m,s;
vector<int>g[N];
int flag;
int color[N];
void dfs(int u,int c,int fa){
if(color[u]!=-){
if(color[u]!=c){
flag=;
}
return;
}
if(flag) return;
color[u]=c;
for(int i=;i<g[u].size();i++){
int next=g[u][i];
if(next!=fa){
dfs(next,!c,u);
}
}
}
int main()
{
int t;
int ac=;
scanf("%d",&t);
while(t--){ scanf("%d%d%d",&n,&m,&s);
for(int i=;i<=n;i++){
g[i].clear();
}
for(int i=;i<m;i++){
int u,v;
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
memset(color,-,sizeof(color));
flag=;
dfs(s,,-);
printf("Case %d: ",++ac);
if(flag){
printf("YES\n");
}else{
printf("NO\n");
}
}
return ;
}
CSU - 1356 Catch(dfs染色两种写法,和hdu4751比较)的更多相关文章
- Sql语句模糊查询字符串的两种写法
Sql语句模糊查询有两种写法,一种是在jdbcTemplate的查询方法参数里拼接字符串%,一种是在Sql语句里拼接%字符串. public class IsNameDaoImpl implement ...
- ORACLE 查询一个数据表后通过遍历再插入另一个表中的两种写法
ORACLE 查询一个数据表后通过遍历再插入另一个表中的两种写法 语法 第一种: 通过使用Oracle语句块 --指定文档所有部门都能查看 declare cursor TABLE_DEPT and ...
- EF架构~linq模拟left join的两种写法,性能差之千里!
回到目录 对于SQL左外连接我想没什么可说的,left join将左表数据都获出来,右表数据如果在左表中不存在,结果为NULL,而对于LINQ来说,要实现left join的效果,也是可以的,在进行j ...
- 运算符关键字。数据区别大小写。日期范围。判空的两种写法。NOT IN的两种写法。IN范围可含NULL,但NOT IN值范围不能含NULL。
比较:>,<,=,>=,<=,<>(!=) 逻辑:AND,OR,NOT 范围:BETWEEN...AND... 范围:IN,NOT IN 判空:IS NULL, I ...
- 快速排序partition过程常见的两种写法+快速排序非递归实现
这里不详细说明快速排序的原理,具体可参考here 快速排序主要是partition的过程,partition最常用有以下两种写法 第一种: int mypartition(vector<int& ...
- java 路径分隔符File.separator 以及 路径两种写法"/"和"\\"
一.File.separator File file=new File(); 这句是新建一个文件.file.separator这个代表系统目录中的间隔符,说白了就是斜线,不过有时候需要双线,有时候是单 ...
- iOS中表视图单元格事件用nib和storyboard的两种写法总结
从ios6开始,苹果公司推出了storyborad技术取代了nib的写法,这样代码量确实少写了很多,也比较简洁.但是,从学习的角度来说,阿堂认为 用nib的写法,虽然多了些代码,但是对于掌握知识和原理 ...
- linq和ef关于group by取最大值的两种写法
LINQ: var temp = from p in db.jj_Credentials group p by p.ProfessionID into g select new { g.Key, Ma ...
- ThinkPHP中Widget的两种写法及调用
Widget扩展一般用于页面组件的扩展,在页面根据需要输出不同的内容,下面介绍一下ThinkPHP中Widget的两种写法及调用 写法一: ArticlWidget.class.php文件: clas ...
随机推荐
- GO的GDB调试
GoLang语言,学了很久,一直觉得它单步调试有较多问题,最近才知道自已对它了解得太少了.原来GO语言对GDB的版本是至少为gdb7以上,才能比较好的打印任意变量,如果低于这个版本,则才会出一些问题. ...
- 14.3.5.1 Interaction of Table Locking and Transactions 表锁和事务的相互作用
14.3.5.1 Interaction of Table Locking and Transactions 表锁和事务的相互作用 LOCK TABLES 和UNLOCK TABLES 交互实用事务如 ...
- 在MFC下实现图像放大镜
当我们想仔细观察某个细微的东西时,一般都会使用放大镜.而要看清显示在计算机屏幕上的图片或文字时通常也可以借助于Windows操作系统附带的放大程序来实现.但该程序只能以固定的放大倍数去进行观看,有时并 ...
- IOS设备设计完整指南(转载)
http://blog.sina.com.cn/s/blog_6cc9af670102vq12.html
- HDU 4122 Alice's mooncake shop
单调队列,裸的!!坑死了,说好的“All the orders are sorted by the time in increasing order. 呢,我就当成严格上升的序列了,于是就各种错.测试 ...
- 【Android Training UI】创建自定义Views(Lesson 0 - 章节概览)
发表在我的独立网站http://kesenhoo.github.io/blog/2013/06/30/android-training-ui-creating-custom-views-lesson- ...
- jQuery提交form表单
<form id="search_form" name="search_form" method="post"> <inp ...
- 通过WebService跨平台上传大文件到服务器
好长时间没写博客了,对最近工作中遇到的大文件上传和下载的代码贴出来和大家分享一下. 大文件的上传的和下载是C++的平台通过调用WebService实现文件上传和下载到服务器 /// <summa ...
- 在一个apk中调用另外一个apk中的activity
今天忽然想到如果要在一个activity中调用另外一个activity该怎么办呢? 感觉这个应该比较简单,应为activity的启动方式就两种:显式启动.隐式启动: 显式启动的话肯定不行,那就只能使用 ...
- 06JS高级创建对象使用原型共享对象方法
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...