UVA 11080 - Place the Guards(二分图判定)
UVA 11080 - Place the Guards
题意:一些城市。之间有道路相连,如今要安放警卫,警卫能看守到当前点周围的边,一条边仅仅能有一个警卫看守,问是否有方案,假设有最少放几个警卫
思路:二分图判定,判定过程记录下白点和黑点个数,小的就是要安放的个数,注意假设是0,那么应该是加1
代码:
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std; const int N = 205; int color[N];
vector<int> g[N]; int b, w; int bipartite(int u) {
if (color[u] == 1) b++;
if (color[u] == 2) w++;
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (color[u] == color[v]) return false;
if (!color[v]) {
color[v] = 3 - color[u];
if (!bipartite(v)) return false;
}
}
return true;
} int t, n, m; int solve() {
int ans = 0;
for (int i = 0; i < n; i++) {
if (!color[i]) {
color[i] = 1;
b = w = 0;
if (!bipartite(i)) return -1;
ans += max(1, min(b, w));
}
}
return ans;
} int main() {
scanf("%d", &t);
while (t--) {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) {
g[i].clear();
color[i] = 0;
}
int u, v;
while (m--) {
scanf("%d%d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
printf("%d\n", solve());
}
return 0;
}
UVA 11080 - Place the Guards(二分图判定)的更多相关文章
- UVa 11396 爪分解(二分图判定)
https://vjudge.net/problem/UVA-11396 题意: 给出n个结点的简单无向图,每个点的度数均为3.你的任务是判断能否把它分解成若干爪.每条边必须属于一个爪,但同一个点可以 ...
- poj2942 Knights of the Round Table,无向图点双联通,二分图判定
点击打开链接 无向图点双联通.二分图判定 <span style="font-size:18px;">#include <cstdio> #include ...
- CF687A. NP-Hard Problem[二分图判定]
A. NP-Hard Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- COJ 0578 4019二分图判定
4019二分图判定 难度级别: B: 编程语言:不限:运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 给定一个具有n个顶点(顶点编号为0,1,… ...
- hdoj 3478 Catch(二分图判定+并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3478 思路分析:该问题需要求是否存在某一个时刻,thief可能存在图中没一个点:将该问题转换为图论问题 ...
- HDU2444(KB10-B 二分图判定+最大匹配)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- DFS的运用(二分图判定、无向图的割顶和桥,双连通分量,有向图的强连通分量)
一.dfs框架: vector<int>G[maxn]; //存图 int vis[maxn]; //节点访问标记 void dfs(int u) { vis[u] = ; PREVISI ...
- HihoCoder 1121 二分图一•二分图判定
二分图一•二分图判定 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 大家好,我是小Hi和小Ho的小伙伴Nettle,从这个星期开始由我来完成我们的Weekly. 新年回 ...
- 【二分图判定】hdu3478 Catch
详细的题解:http://www.docin.com/p-517243379.html 一个图是二分图 等价于 其至少有两个节点且没有奇环. 二分图判定的方法:从任意点出发进行一次dfs黑白染色,若某 ...
随机推荐
- hdu1370-Biorhythms
http://acm.hdu.edu.cn/showproblem.php?pid=1370 中国剩余定理 已知(n+d)%23=a; (n+d)%28=b; (n+c)%33=i ...
- Windows Azure入门教学系列 (九):Windows Azure 诊断功能
本文是Windows Azure入门教学的第九篇文章. 本文将会介绍如何使用Windows Azure 诊断功能.跟部署在本地服务器上的程序不同,当我们的程序发布到云端之后,我们不能使用通常的调试方法 ...
- Spring Mobile是如何判断访问设备的类型的
Spring最近换域名了,去转转,发现了一个有意思的项目:spring mobile. http://projects.spring.io/spring-mobile/ 这个项目有很多实用的功能,如识 ...
- 基于visual Studio2013解决C语言竞赛题之1078打印日历
题目 解决代码及点评 /************************************************************************/ /* ...
- 运行tomcat6w.exe ,提示 指定的服务未安装 unable to open the service 'tomcat66'
错误:运行tomcat6w.exe ,提示 指定的服务未安装 unable to open the service 'tomcat6'(我用的是官网下载的解压版) 解决方法: 打开命令行提示符窗口=& ...
- ASP.NET - List<> 绑定 DropDownList
代码: //声明泛型 List<category> inof = new List<category>();//二级分类 //声明类使用的对象类 public class ca ...
- cPickle.so:: PyUnicodeUCS2_DecodeUTF8
cPickle.so:: PyUnicodeUCS2_DecodeUTF8错误 Python编译的参数,和Python module(mod_wsgi, pymodwsgi)编译参数不一,导致一些un ...
- 2015 Multi-University Training Contest 1
最近真是太废柴了,题没做几道,也没学什么新知识,多校做了三场也没总结~诶!好好学吧! 多校第一场感觉被完虐...orz... Hdu 5288 OO’s Sequence 题目链接:http://ac ...
- javascript 自己主动绑定JS callback 的方法函数
自己写的一个javascript 智能绑定callback 而且调用运行的函数.主要用于异步请求的 ajax中: <!DOCTYPE html> <html> <head ...
- 向大家推荐个android的游戏引擎——cocos2d-x
最近发现单单用android自带的功能函数来编写游戏,往往有很大的局限性,即耗时长,调试繁琐,没有一定的框架.所以博主发现了游戏引擎这个好东西,游戏引擎所拥有的架构和功能函数,使得游戏的编写更加得心应 ...