这两个题目都是二分图的判定,用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的更多相关文章

  1. hdu_2444The Accomodation of Students(二分图的判定和计算)

    hdu_2444The Accomodation of Students(二分图的判定和计算) 标签:二分图匹配 题目链接 题意: 问学生是否能分成两部分,每一部分的人都不相认识,如果能分成的话,两两 ...

  2. POJ:2492-Bug's Life(二分图的判定)

    Bug's Life Time Limit: 10000MS Memory Limit: 65536K Description Background Professor Hopper is resea ...

  3. 双栈排序(洛谷P1155)二分图的判定+思维贪心

    题目:戳这里 题目大意: 给你一个数列,问能否通过两个栈的push与pop把它输出成一个升序序列(每个数只能入队并出队一次) 不能的话输出0,能的话输出操作方法 主要思路: 1.判断是否可以成功输出升 ...

  4. 点的双联通+二分图的判定(poj2942)

    Knights of the Round Table Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 10804   Acce ...

  5. CF862B Mahmoud and Ehab and the bipartiteness 二分图染色判定

    \(\color{#0066ff}{题目描述}\) 给出n个点,n-1条边,求再最多再添加多少边使得二分图的性质成立 \(\color{#0066ff}{输入格式}\) The first line ...

  6. Divide Groups 二分图的判定

    Divide Groups Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  7. CodeForces - 1093D:Beautiful Graph(二分图判定+方案数)

    题意:给定无向图,让你给点加权(1,2,3),使得每条边是两端点点权和维奇数. 思路:一个连通块是个二分图,判定二分图可以dfs,并查集,2-sat染色. 这里用的并查集(还可以带权并查集优化一下,或 ...

  8. HihoCoder 1121二分图一•二分图判定

    背景: 个名字,表示这两个人有一场相亲.由于姑姑年龄比较大了记性不是太好,加上相亲的人很多,所以姑姑一时也想不起来其中有些人的性别.因此她拜托我检查一下相亲表里面有没有错误的记录,即是否把两个同性安排 ...

  9. HDU2819:Swap(二分图匹配)

    Swap Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

随机推荐

  1. 李洪强iOS开发之-cocopods安装

  2. HTML5的Server-Sent Events功能的使用

    客户端代码示例 //创建一个新的 EventSource 对象,然后规定发送更新的页面的 URL. var source = new EventSource("http://localhos ...

  3. C++ Socket UDP "Hello World!"

    这是C++ SOCKET网络程序中的C/S结构之UDP "Hello World !",共两个控制台工程: //////////////////////////////////// ...

  4. JAVA 内存泄漏与内存溢出

    一.Java内存回收机制 不论哪种语言的内存分配方式,都需要返回所分配内存的真实地址,也就是返回一个指针到内存块的首地址.Java中对象是采用new或者反射或者clone或者反序列化的方法创建的, 这 ...

  5. Android应用开发性能优化完全分析

    1 背景 其实有点不想写这篇文章的,但是又想写,有些矛盾.不想写的原因是随便上网一搜一堆关于性能的建议,感觉大家你一总结.我一总结的都说到了很多优化注意事项,但是看过这些文章后大多数存在一个问题就是只 ...

  6. tar 解压命令

    1.tar.gz文件的解压 tar zxvf  *.tar.gz 2.bz2属性的解压 tar jxvf  *.bz2

  7. android报错——The import android.util cannot be resolved

    Eclipse导入外部Android工程时,总会遇到The import android.util cannot be resolved 错误,解决方法如下: 首先检查project.properti ...

  8. pow(x,n) leecode

    https://oj.leetcode.com/problems/powx-n/ 提交地址 快速幂的使用,可以研究一下 public class Solution { public double po ...

  9. vijosP1629 八

    vijosP1629 八 链接:https://vijos.org/p/1629 [思路] 暴力容斥(看他们都这么叫=_=)+精度选择. 总体思路是先统计LR区间内满足是8倍数的数目ans,再从ans ...

  10. 编译 Spring-framework的经验分享

    Spring-framework source codegit url: git clone git://github.com/SpringSource/spring-framework.git 导入 ...