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的用法
写在前面 所谓异常处理,即让一个程序运行时遇到自己无法处理的错误时抛出一个异常,希望调用者可以发现处理问题. 异常处理的基本思想是简化程序的错误代码,为程序键壮性提供一个标准检测机制. 也许我们已经使 ...
随机推荐
- eclipse show view失效的解决办法
今天打开eclipse,发现console窗口没有了,然后使用show view也无法打开,上网查找办法,找到了方法试了一下,窗口重置(Windows-->Perspective-->Re ...
- 图像格式转换之BMP格式转换为JPG格式
// bmp2jpg.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include "jpeglib.h" #inc ...
- 使用pt-ioprofile监控数据库io文件读写情况
我们在做IO密集型的应用程序的时候,比如MySQL数据库,通常系统的表现取决于workload的类型. 比如我们要调优,我们就必须非常清楚的知道数据的访问规律,收集到足够的数据,用来做调优的依据. 有 ...
- bzoj 1528 [POI2005]sam-Toy Cars 堆维护+贪心
1528: [POI2005]sam-Toy Cars Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 716 Solved: 306[Submit][S ...
- Rabbitmq -- direct
一.前言 RabbitMQ还支持根据关键字发送,即:队列绑定关键字,发送者将数据根据关键字发送到消息exchange.direct类型的Exchange路由规则也很简单,它会把消息路由到那些bindi ...
- 分块+lazy 或者 线段树+lazy Codeforces Round #254 (Div. 2) E
E. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Shiro实战教程(二)
http://www.jianshu.com/p/6786ddf54582/ https://www.cnblogs.com/ealenxie/p/10610741.html
- ASP.Net Cache(缓存)—ASP.NET细枝末节(2)
概述 1.意义 把数据放到Cache中,在指定的时间内,可以直接从Cache中获取,避免对数据库等的压力. 2.做法 设置: HttpRuntime.Cache.Insert(CacheKey, ob ...
- Unity 添加鼠标右键事件
把此类放到 Editor下使用就OK using UnityEngine; using System.Collections; using System.Collections.Generic; us ...
- perf + 火焰图分析程序性能
1.perf命令简要介绍 性能调优时,我们通常需要分析查找到程序百分比高的热点代码片段,这便需要使用 perf record 记录单个函数级别的统计信息,并使用 perf report 来显示统计结果 ...