二分图的判定hihocoder1121 and hdu3478
这两个题目都是二分图的判定,用dfs染色比较容易写。
算法流程:
选取一个没有染色的点,然后将这个点染色,那么跟他相连的所有点一定是不同颜色的,所以,如果存在已经染过颜色的,如果和这个颜色相同的话,就说明不是二分图,否则,继续从这个点往下染。
hihocoder
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std; const int maxn = ;
vector<int> mp[maxn];
int color[maxn];
void init(int n)
{
memset(color, -, sizeof(color));
for (int i = ; i <= n; i++) mp[i].clear();
}
bool dfs(int u, int c)//用0和1分别表示两种颜色
{
color[u] = c;
for (int i = ; i < mp[u].size(); i++)
{
if (color[mp[u][i]] == c) return false;
if (color[mp[u][i]] == - && !dfs(mp[u][i], c ^ )) return false;
}
return true;
}
int main()
{
int T, n, m;
scanf("%d", &T);
while (T--)
{
int u, v;
scanf("%d%d", &n, &m);
init(n);
for (int i = ; i < m; i++)
{
scanf("%d%d", &u, &v);
mp[u].push_back(v);
mp[v].push_back(u);
}
bool ans = true;
for (int i = ; i <= n; i++)
{
if (color[i] == - && !dfs(i, ))
{
ans = false;
break;
}
}
printf("%s\n", ans ? "Correct" : "Wrong");
}
return ;
}
hdu3478
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std; const int maxn = ;
vector<int> mp[maxn];
int color[maxn];
void init(int n)
{
memset(color, -, sizeof(color));
for (int i = ; i < n; i++) mp[i].clear();
}
bool dfs(int u, int c)
{
color[u] = c;
for (int i = ; i < mp[u].size(); i++)
{
if (color[mp[u][i]] == c) return false;
if (color[mp[u][i]] == - && !dfs(mp[u][i], c ^ )) return false;
}
return true;
}
int main()
{
int T, n, m, S, kase = ;
scanf("%d", &T);
while (T--)
{
scanf("%d%d%d", &n, &m, &S);
if (n == )
{
printf("Case %d: YES\n", ++kase);
continue;
}
init(n);
int u, v, cnt = ;
for (int i = ; i < m; i++)
{
scanf("%d%d", &u, &v);
mp[u].push_back(v);
mp[v].push_back(u);
}
bool flag = dfs(S, );
if (flag)
printf("Case %d: NO\n", ++kase);
else
printf("Case %d: YES\n", ++kase);
} return ;
}
二分图的判定hihocoder1121 and hdu3478的更多相关文章
- hdu_2444The Accomodation of Students(二分图的判定和计算)
hdu_2444The Accomodation of Students(二分图的判定和计算) 标签:二分图匹配 题目链接 题意: 问学生是否能分成两部分,每一部分的人都不相认识,如果能分成的话,两两 ...
- POJ:2492-Bug's Life(二分图的判定)
Bug's Life Time Limit: 10000MS Memory Limit: 65536K Description Background Professor Hopper is resea ...
- 双栈排序(洛谷P1155)二分图的判定+思维贪心
题目:戳这里 题目大意: 给你一个数列,问能否通过两个栈的push与pop把它输出成一个升序序列(每个数只能入队并出队一次) 不能的话输出0,能的话输出操作方法 主要思路: 1.判断是否可以成功输出升 ...
- 点的双联通+二分图的判定(poj2942)
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 10804 Acce ...
- CF862B Mahmoud and Ehab and the bipartiteness 二分图染色判定
\(\color{#0066ff}{题目描述}\) 给出n个点,n-1条边,求再最多再添加多少边使得二分图的性质成立 \(\color{#0066ff}{输入格式}\) The first line ...
- Divide Groups 二分图的判定
Divide Groups Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- CodeForces - 1093D:Beautiful Graph(二分图判定+方案数)
题意:给定无向图,让你给点加权(1,2,3),使得每条边是两端点点权和维奇数. 思路:一个连通块是个二分图,判定二分图可以dfs,并查集,2-sat染色. 这里用的并查集(还可以带权并查集优化一下,或 ...
- HihoCoder 1121二分图一•二分图判定
背景: 个名字,表示这两个人有一场相亲.由于姑姑年龄比较大了记性不是太好,加上相亲的人很多,所以姑姑一时也想不起来其中有些人的性别.因此她拜托我检查一下相亲表里面有没有错误的记录,即是否把两个同性安排 ...
- HDU2819:Swap(二分图匹配)
Swap Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
随机推荐
- VS2005 检测内存泄漏的方法(转载)
一.非MFC程序可以用以下方法检测内存泄露: 1.程序开始包含如下定义: #ifdef _DEBUG #define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __F ...
- git stash的使用
https://git-scm.com/docs/git-stash 在git svn的时候使用,提交记录的时候,有部分文件的修改不需要commit. 在向svn进行git svn dcommit的时 ...
- 使用 Puppet 在 Windows Azure 中配备 Linux 和 Windows 环境
发布于 2013-12-11 作者 Ross Gardler 微软开放技术有限公司 (MS Open Tech) 很高兴地宣布发行新的 Windows Azure Puppet 模块.通过这个模 ...
- Delphi TcxTreeList 怎么设置分组
Delphi 的TcxTreeList控件设置按种类分组,操作如下: 1. 在TcxTreeList控件中双击,打开 Bands 属性,在这里面建需要分的组,在Captions->Text 输入 ...
- JSP控制select不可再选择
首先分析下disable ,display和readonly: 1,Readonly只针对input(text / password)和textarea有效,而disabled对于所有的表单元素都有效 ...
- 怎么通过IE连接本机oracle数据库
这个目录下D:\oracle\product\10.2.0\db_study\install ,有个reademe.txt文件,里面记录着你访问数据库的网址和端口.
- First Adventures in Google Closure -摘自网络
Contents Introduction Background Hello Closure World Dependency Management Making an AJAX call with ...
- iPhone/Mac Objective-C内存管理教程和原理剖析
http://www.cocoachina.com/bbs/read.php?tid-15963.html 版权声明 此文版权归作者Vince Yuan (vince.yuan#gmail.com)所 ...
- python面向对象(二)——类成员
Python面向对象 类成员 1.字段 普通字段 属于对象 静态字段 属于类 2.方法 普通方法 触发者是对象 括号里至少一个参数 se ...
- [置顶] 在Ubuntu下实现一个简单的Web服务器
要求: 实现一个简单的Web服务器,当服务器启动时要读取配置文件的路径.如果浏览器请求的文件是可执行的则称为CGI程序,服务器并不是将这个文件发给浏览器,而是在服务器端执行这个程序,将它的标准输出发给 ...