CSU 1356 Catch
原题链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1356
题目需要我们判断给定图在某一步是否会有可能出现在所有节点。首先,我们不妨假设给定图是一条单链,那么无论何时,都是“NO”的情况,因为某一时刻总是奇点或偶点(因为点是同步的,奇点相邻总是偶点,偶点相邻总是奇点)。同理,假设给定图示一个偶圈,同样得出是“NO”的情况;最终,我们得出得出只有是寄圈的情况下才满足“YES”的情况。
所以我们的任务就是判断寄圈,这里我用的是染色法,直接对图进行深搜同时标记颜色,应该是最通用的求寄圈的方法了。
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <queue>
#include <cassert>
using namespace std;
//#pragma comment(linker, "/STACK:102400000,102400000")
#define pii pair<int,int>
#define clr(a) memset((a),0,sizeof (a))
#define rep(i,a,b) for(int i=(a);i<=(int)(b);i++)
#define per(i,a,b) for(int i=(a);i>=(int)(b);i--)
#define inf (0x3f3f3f3f)
#define eps 1e-6
#define N 100005
#define M 4000005
#define MODN 1000000007
#define RI(x) scanf("%d", &x)
#define RII(x,y) scanf("%d%d", &x, &y)
#define RIII(x,y,z) scanf("%d%d%d", &x, &y, &z)
#define debug puts("reach here");
typedef long long LL; int n, m, s;
int a, b;
int ne;
int head[N];
int color[N]; struct node
{
int v;
int next;
}E[M]; void add_edge(int u, int v)
{
E[ne].v = v;
E[ne].next = head[u];
head[u] = ne++;
} void init()
{
memset(head, -, sizeof head);
memset(color, , sizeof color);
ne = ;
} bool dfs(int u)
{
for(int i = head[u]; i != -; i = E[i].next)
{
int v = E[i].v;
if(color[v] == )
{
color[v] = color[u] % + ;
if(dfs(v))
return true;
}
else
{
if(((color[u] + color[v]) % ) == )
return true;
}
}
return false;
} int main()
{
int t;
RI(t);
rep(cas,,t)
{
RIII(n, m, s);
init();
rep(i,,m-)
{
RII(a, b);
add_edge(a, b);
add_edge(b, a);
}
color[s] = ;
printf("Case %d: ", cas);
if(dfs(s)) puts("YES");
else puts("NO"); }
return ;
}
CSU 1356 Catch的更多相关文章
- csu 1356 Catch bfs(vector)
1356: Catch Time Limit: 2 Sec Memory Limit: 128 MBSubmit: 96 Solved: 40[Submit][Status][Web Board] ...
- CSU - 1356 Catch(dfs染色两种写法,和hdu4751比较)
Description A thief is running away! We can consider the city to N–. The tricky thief starts his esc ...
- IOS开发之--异常处理--使用try 和 catch 来捕获错误。
一个搞java的老板问我会不会try catch 我说不会 学这么久也没听周围朋友用这个 因为苹果控制台本来就可以打印异常 特此研究一下. 1.try catch: 是捕获异常代码段 特点:对 ...
- iOS try catch
最近看一些第三方的代码有@try,一副看不懂的样子,真心没用过,于是查了些资料收集在这里,以后遇到就不会再蒙比了.其实这东西确实不怎么用,下文有解释.Objective-C 异常机制 :-- 作用 : ...
- CSU - 2031 Barareh on Fire (两层bfs)
传送门: http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2031 Description The Barareh village is on f ...
- catch socket error
whois_handler.dart import 'dart:io'; import 'package:async/async.dart'; import 'dart:convert'; class ...
- SQLServer如何添加try catch
在.net中我们经常用到try catch.不过在sqlserver中我们也可以使用try catch捕捉错误,在这里把语法记录下来和大家分享一下, --构建存储过程CREATE PROCEDURE ...
- try...catch..finally
try..catch..finally try{ 代码块1 }catch(Exception e){ 代码块2 }finally{ 代码块3 } catch是抓取代码块1中的异常 代码块2是出异常后的 ...
- C++异常处理:try,catch,throw,finally的用法
写在前面 所谓异常处理,即让一个程序运行时遇到自己无法处理的错误时抛出一个异常,希望调用者可以发现处理问题. 异常处理的基本思想是简化程序的错误代码,为程序键壮性提供一个标准检测机制. 也许我们已经使 ...
随机推荐
- CDN公共库、前端开发常用插件一览表(VendorPluginLib)
=======================================================================================前端CDN公共库===== ...
- bzoj1912【Apio2010】patrol 巡逻
题解: 显然需要分类讨论了,首先理解k==0即原图时按照dfs序来说 , 每条边至少走两次: k==1,相当于可以省去dfs回溯时第二次走过某条路径的浪费,所以答案是k==0的答案-直径 : k==2 ...
- IT(然而其实是。。hdu5244?)
Time Limit: 3000 ms Memory Limit: 256 MB Description IT = Inverse Transform 两个长度为 \(2^n\) 的序列 \(a,b\ ...
- struts的status属性
struts2 <s:iterator> status属性 转载▼ iterator标签主要是用于迭代输出集合元素,如list set map 数组等,在使用标签的时候有三个属性值得我 ...
- 「Django」学习之路,持续更改
一.setting设置 1.设置 局域网可以部署连接 ALLOWED_HOSTS = ['*.besttome.com','192.168.1.100'] 2.static配置 STATIC_URL ...
- python 获取文件md5
def GetFileMd5(filename): if not os.path.isfile(filename): return myhash = hashlib.md5() f = file(fi ...
- nova-virt与libvirt
源码版本:H版 nova通过nova/virt/driver.py中的ComputeDriver对底层虚拟化技术进行抽象,不同的虚拟化技术在nova/virt下有不同的目录,里面均有driver.py ...
- 使用RVM轻松部署Ruby环境
Ruby用得不多,但发现有业务需要部署指定的版本和插件.起初找了一些Fedora的src.rpm重新打包,发现依赖问题比较多,最终还是费劲的把el6的包编出来了. 不巧今天又有业务要求el5的包,原本 ...
- web启动@Autowired不能自动注入
使用struts2,下面为action代码 Java代码 package com.edar.web.platform; import org.apache.struts2.convention.ann ...
- Bargaining Table
Bargaining Table time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...