原题链接: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的更多相关文章

  1. csu 1356 Catch bfs(vector)

    1356: Catch Time Limit: 2 Sec  Memory Limit: 128 MBSubmit: 96  Solved: 40[Submit][Status][Web Board] ...

  2. CSU - 1356 Catch(dfs染色两种写法,和hdu4751比较)

    Description A thief is running away! We can consider the city to N–. The tricky thief starts his esc ...

  3. IOS开发之--异常处理--使用try 和 catch 来捕获错误。

    一个搞java的老板问我会不会try catch  我说不会 学这么久也没听周围朋友用这个 因为苹果控制台本来就可以打印异常 特此研究一下. 1.try catch:  是捕获异常代码段   特点:对 ...

  4. iOS try catch

    最近看一些第三方的代码有@try,一副看不懂的样子,真心没用过,于是查了些资料收集在这里,以后遇到就不会再蒙比了.其实这东西确实不怎么用,下文有解释.Objective-C 异常机制 :-- 作用 : ...

  5. CSU - 2031 Barareh on Fire (两层bfs)

    传送门: http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2031 Description The Barareh village is on f ...

  6. catch socket error

    whois_handler.dart import 'dart:io'; import 'package:async/async.dart'; import 'dart:convert'; class ...

  7. SQLServer如何添加try catch

    在.net中我们经常用到try catch.不过在sqlserver中我们也可以使用try catch捕捉错误,在这里把语法记录下来和大家分享一下, --构建存储过程CREATE PROCEDURE ...

  8. try...catch..finally

    try..catch..finally try{ 代码块1 }catch(Exception e){ 代码块2 }finally{ 代码块3 } catch是抓取代码块1中的异常 代码块2是出异常后的 ...

  9. C++异常处理:try,catch,throw,finally的用法

    写在前面 所谓异常处理,即让一个程序运行时遇到自己无法处理的错误时抛出一个异常,希望调用者可以发现处理问题. 异常处理的基本思想是简化程序的错误代码,为程序键壮性提供一个标准检测机制. 也许我们已经使 ...

随机推荐

  1. Codeforces Round #416 (Div. 2)A B C 水 暴力 dp

    A. Vladik and Courtesy time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  2. Codeforces 938.C Constructing Tests

    C. Constructing Tests time limit per test 1 second memory limit per test 256 megabytes input standar ...

  3. Nginx多个配置文件共用location配置

    一.应用情况 很多时候我们在一台服务器上部署了不止 一个项目,我们通过Nginx来代理,为了方便管理往往会将各个项目的配置分开写到不同的配置文件中,如: 在nginx.conf 文件中加上  incl ...

  4. vue 父子组件相互传递数据

    例子一 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta ...

  5. Bootstrap3和Bootsrap4的区别

    Bootstrap3和Bootstap4的区别 1.Bootsrap4 css文件减少了40%以上 2.Bootsrap4已经不支持IE8以及IOS 6的支持 3.多了些类好组件

  6. XFire搭建WebService和客户端访问程序

    开发环境:myeclipse8.6+jdk1.6.0_29+tomcat6.0.37 JAX-WS搭建webservice:http://www.cnblogs.com/gavinYang/p/352 ...

  7. java项目转换依赖等问题

    最近接手了一个原始的java项目,其实很久没有做了,自从两年前用maven,建立web项目,java project基本上就没有弄个,突然的接手,发现自己好多不足,可能对于一开始就做这样的容易,但是对 ...

  8. Codeforces 807 C. Success Rate

    http://codeforces.com/problemset/problem/807/C C. Success Rate time limit per test 2 seconds memory ...

  9. C++ Arithmetic Exception

    运算异常错误,比如除零错误,浮点数取余等等.

  10. 【洛谷 P2216】 [HAOI2007]理想的正方形(二维ST表)

    题目链接 做出二维\(ST\)表,然后\(O(n^2)\)扫一遍就好了. #include <cstdio> #include <cstring> #include <a ...