二分图的判定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 ...
随机推荐
- Android ContentProvider和getContentResolver
安卓系统中的数据库SqlLite操作和java中mysql的数据库操作很不一样,造成这样的原因是因为在安卓中数据库是属于进程的不存在数据库客户端,也不存在数据库服务器. 关于SqlLite数据库的文章 ...
- Windows下Redis中RedisQFork位置调整
redis-server.exe redis.windows.conf 使用上面命令启动redis服务的时候报了以下错误信息: The Windows version of Redis allocat ...
- ruby面向对象class
ruby对象是严格封装的:只能通过定义的方法访问其内部状态.方法使用的成员变量在对象外部不能直接访问,不过可以通过getter.setter等访问器方法(accessor),使他们看起来好像是直接访问 ...
- bzoj1487
还是仙人掌,和1023一样的考虑方法比1023简单但比1040难环形dp的处理方法和1040一样 type node=record po,next:longint; end; ..,..] of lo ...
- [转]使用Xcode和Instruments调试解决iOS内存泄露
虽然iOS 5.0版本之后加入了ARC机制,由于相互引用关系比较复杂时,内存泄露还是可能存在.所以了解原理很重要. 这里讲述在没有ARC的情况下,如何使用Instruments来查找程序中的内存泄露, ...
- (转载)SQL联合查询中的关键语法
(转载)http://www.cnblogs.com/zhangliyu/archive/2009/03/21/1418215.html 联合查询效率较高.以下例子来说明联合查询的好处 t1表结构(用 ...
- android报错——findViewById报错
通過ID找到Layout的 VIEW控件.,比如你的控件Button ID為"@+id/button01" 就可以通過這樣Button btn=(Button)findView ...
- SQLServer的ISNULL函数和Mysql的IFNULL函数
SQL Serve的ISNULL函数: ISNULL(check_expression,replacement_value) 1.check_expression与replacement_value的 ...
- Bzoj 1391: [Ceoi2008]order 网络流,最大权闭合图
1391: [Ceoi2008]order Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1105 Solved: 331[Submit][Statu ...
- maven构建带版本号和日期的war包名
21166312 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !import ...