二分图的判定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 ...
随机推荐
- http://rogerdudler.github.io/git-guide/index.zh.html
http://rogerdudler.github.io/git-guide/index.zh.html
- ANDROID_MARS学习笔记_S04_001_OAuth简介
一.OAuth简介
- 在html中嵌入markdown
在博客园网页里写markdown的时候, 某些特殊内容想加上自定义的css, 于是用<div class="xxx">包裹起来, 但是发现该<div>中的m ...
- A WCF-WPF Chat Application
http://www.codeproject.com/Articles/25261/A-WCF-WPF-Chat-Application
- 利用if else 求已发奖金总数
class Program { static void Main(string[] args) { while (true) ...
- Mac 下配置XAMPP
1:去官方下载 2:安装dmg 3:安装完成后, 网页上提示, 要设置相应的密码, 设置完成. 4:打开对应的app程序, 把 mysql Database运行起来, 不然, 网页上看到的就是未运行状 ...
- linux下阅读源代码的工具
说来真是惭愧呀.一直在用VIM 做开发.却不知道VI 里还有这么好使的工具.以前一直都是用: find -type f -print | xargs grep -i **** 在源代码里查找. 原来L ...
- 在Eclipse中安装和使用TFS插件
在Eclipse中安装插件的方法其实都一样,安装TFS的步骤如下: 下载TFS插件.你可以到微软的下载中心,下载TFS插件TFSEclipsePlugin-UpdateSiteArchive-10.0 ...
- Linux学习笔记32——select()函数分析【转】
Select在Socket编程中还是比较重要的,可是对于初学Socket的人来说都不太爱用Select写程序,他们只是习惯写诸如 connect.accept.recv或recvfrom这样的阻塞程序 ...
- [NOIP2005]采药
2005年NOIP全国联赛普及组 [题目描述 Description] 辰辰是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师.为此,他想拜附近最有威望的医师为师.医师为了判断他的资质,给他出了一个 ...