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 ...
随机推荐
- Linux文件系统学习笔记-1
在Linux中, 一切皆文件,不论是目录,设备,套接字等都可以看成文件,而且每一个文件对应一个inode号,这是一一对应的关系. [root@oracle ~]# ls -il 总用量 2624 ...
- linux grep练习
1.显示/proc/meminfo文件中以不区分大小的s开头的行: 2.显示/etc/passwd中以nologin结尾的行; 3.显示/etc/inittab中以#开头,且后面跟一个或多个空白字符, ...
- 电子科大POJ "任意阶矩阵相乘"
任意阶矩阵的乘法 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) C-sourc ...
- 可执行文件(ELF)格式的理解
摘自http://www.cnblogs.com/xmphoenix/archive/2011/10/23/2221879.html 可执行文件(ELF)格式的理解 ELF(Executable an ...
- HTML5开发 BUG解决
1.点透Q:元素A上定位另外一个元素B,点击元素B,如果元素A有事件或链接,会触发元素A上的事件或链接,即点透A:在元素B的touchend中增加ev.preventDefault();阻止默认事件即 ...
- RMAN备份之丢失数据文件及控制文件的恢复
About Recovery with a Backup Control FileIf all copies of the current control file are lost or damag ...
- 【最小费用最大流】【HDU1533】【Going Home】
题意 给你一个类似这样的图 ...H.... ...H.... ...H.... mmmHmmmm ...H.... ...H.... ...H.... 问所有H移动到所有m上花费最少的步数 以所有H ...
- ChartControl简单的圆柱案例
由于工作需要,最近学习了DevExpress控件中的ChartControl,并做了简单尝试,通过程序动态添加数据源到chartControl控件中,绘制了如下的条形图. 条形图的颜色等外观可在cha ...
- 关于rem自适应的一点研究
参考地址:http://m.ctrip.com/html5/ https://www.amazon.cn/ rem是相对于html根元素的一个单位.rem是px的16倍,即1rem = 16px;除了 ...
- 二、fragment使用
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/8881711 我们都知道,Android上的界面展示都是通过Activity实现的, ...