Catch---hdu3478(染色法判断是否含有奇环)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3478
题意:有n个路口,m条街,一小偷某一时刻从路口 s 开始逃跑,下一时刻都跑沿着街跑到另一路口,问是否存在某一时刻出,小偷可能出现在任意路口;
如果小偷能走一个环,如果这个环是偶数个节点,那么某个节点只能在偶数时刻或者奇数时刻到达;
但是如果这个环是奇数个节点,他既可以在奇数时刻到达又可以在偶数时刻到达;所以这道题就是求是否存在一个奇环;如果存在输出YES,否则NO;
由于二分图中不能含有奇环,所以我们只需用染色法判断是否是二分图即可;
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <math.h> using namespace std; #define met(a, b) memset(a, b, sizeof(a))
#define N 100003
#define INF 0x3f3f3f3f
const int MOD = 1e9+; typedef long long LL; vector<vector<int> >G; int c[N], n, m, s; int flag; void dfs(int u)
{
int len = G[u].size();
for(int i=; i<len; i++)
{
int v = G[u][i];
if(c[v] == -)
{
c[v] = c[u]^;
dfs(v);
}
else if(c[v] == c[u])
{
flag = ;
return;
}
}
return ;
} int main()
{
int T, t = ;
scanf("%d", &T);
while(T--)
{
scanf("%d %d %d", &n, &m, &s);
G.clear();
G.resize(n+);
for(int i=; i<=m; i++)
{
int u, v;
scanf("%d %d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
met(c, -);
flag = ;
printf("Case %d: ", t++);
dfs(s);
if(flag)puts("YES");
else puts("NO");
}
return ;
}
dfs
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <math.h> using namespace std; #define met(a, b) memset(a, b, sizeof(a))
#define N 100003
#define INF 0x3f3f3f3f
const int MOD = 1e9+; typedef long long LL; vector<vector<int> >G; int c[N], n, m, s; int bfs()
{
met(c, -);
queue<int>Q;
Q.push(s);
c[s] = ;
while(!Q.empty())
{
int p = Q.front();Q.pop();
int len = G[p].size(), q;
for(int i=; i<len; i++)
{
q = G[p][i];
if(c[q] == -)
{
c[q] = c[p]^;
Q.push(q);
}
else if(c[q] == c[p])
return ;///说明存在奇环,不是二分图;
}
}
return ;
} int main()
{
int T, t = ;
scanf("%d", &T);
while(T--)
{
scanf("%d %d %d", &n, &m, &s);
G.clear();
G.resize(n+);
for(int i=; i<=m; i++)
{
int u, v;
scanf("%d %d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
printf("Case %d: ", t++);
if(bfs())puts("YES");
else puts("NO");
}
return ;
}
bfs
Catch---hdu3478(染色法判断是否含有奇环)的更多相关文章
- Wrestling Match---hdu5971(2016CCPC大连 染色法判断是否是二分图)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5971 题意:有n个人,编号为1-n, 已知X个人是good,Y个人是bad,m场比赛,每场比赛都有一个 ...
- 染色法判断是否是二分图 hdu2444
用染色法判断二分图是这样进行的,随便选择一个点, 1.把它染成黑色,然后将它相邻的点染成白色,然后入队列 2.出队列,与这个点相邻的点染成相反的颜色 根据二分图的特性,相同集合内的点颜色是相同的,即 ...
- 【交叉染色法判断二分图】Claw Decomposition UVA - 11396
题目链接:https://cn.vjudge.net/contest/209473#problem/C 先谈一下二分图相关: 一个图是二分图的充分必要条件: 该图对应无向图的所有回路必定是偶环(构成该 ...
- hdu 2444(染色法判断二分图+最大匹配)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- 【01染色法判断二分匹配+匈牙利算法求最大匹配】HDU The Accomodation of Students
http://acm.hdu.edu.cn/showproblem.php?pid=2444 [DFS染色] #include<iostream> #include<cstdio&g ...
- poj 2942 求点双联通+二分图判断奇偶环+交叉染色法判断二分图
http://blog.csdn.net/lyy289065406/article/details/6756821 http://www.cnblogs.com/wuyiqi/archive/2011 ...
- HDU - 3478 Catch(判奇环/二分图)
http://acm.hdu.edu.cn/showproblem.php?pid=3478 题意 给一个无向图和小偷的起点,小偷每秒可以向相邻的点出发,问有没有一个时间点小偷可能出现在任何点. 分析 ...
- [cf557d]Vitaly and Cycle(黑白染色求奇环)
题目大意:给出一个 n 点 m 边的图,问最少加多少边使其能够存在奇环,加最少边的情况数有多少种. 解题关键:黑白染色求奇环,利用数量分析求解. 奇环:含有奇数个点的环. 二分图不存在奇环.反之亦成立 ...
- [转载]HDU 3478 判断奇环
题意:给定n个点,m条边的无向图(没有重边和子环).从给定点出发,每个时间走到相邻的点,可以走重复的边,相邻时间不能停留在同一点,判断是否存在某个时间停留在任意的n个点. 分析: (1)首先,和出发点 ...
随机推荐
- 雷达波Shader
OSG版本: vert #version varying out vec3 v; void main() { gl_FrontColor = gl_Color; gl_Position = ftran ...
- Linux误删文件后恢复数据
在Linux下,基于开源的数据恢复工具有很多,常见的有debugfs.R-Linux.ext3grep.extundelete等,比较常用的有ext3grep和extundelete,这两个工具的恢复 ...
- socket.io websocket
不能不知道的事: 在Http协议中,客户端向服务器端发送请求,服务器端收到请求再进行回应,整个过程中,服务器端是被动方,客户端是主动方: websoket是H5的一种基于TCP的新通信协议,它与Htt ...
- STL——仿函数(函数对象)
一.仿函数(也叫函数对象)概观 仿函数的作用主要在哪里?从第6章可以看出,STL所提供的各种算法,往往有两个版本,其中一个版本表现出最常用(或最直观)的某种运算,第二个版本则表现出最泛化的演算流程,允 ...
- 安装windows7/8/10到U盘或移动硬盘
https://jingyan.baidu.com/article/e52e36156f6ad240c60c518c.html jpg改rar
- Node.js- sublime搭建node的编译环境
自动配置: 1.安装package control(见 http://www.cnblogs.com/padding1015/p/7763014.html) 2.sublime编辑器中,按快捷键:ct ...
- dos 下如何查看环境变量
使用命令:echo %path%
- Internet Message Access Protocol --- IMAP协议
https://en.wikipedia.org/wiki/Internet_Message_Access_Protocol Internet Message Access Protocol
- 在ubuntu中安装rpm包
Ubuntu的软件包格式是deb,如果要安装rpm的包,则要先用alien把rpm转换成deb. sudo apt-get install alien #alien默认没有安装,所以首先要安装它 su ...
- laravel 查看sql
方法一: 我们有时候想测试一段代码生产的 SQL 语句,比如: 我们想看 App\User::all(); 产生的 SQL 语句,我们简单在 routes.php 做个实验即可: //app/Http ...