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(二分图判定)的更多相关文章

  1. UVa 11396 爪分解(二分图判定)

    https://vjudge.net/problem/UVA-11396 题意: 给出n个结点的简单无向图,每个点的度数均为3.你的任务是判断能否把它分解成若干爪.每条边必须属于一个爪,但同一个点可以 ...

  2. poj2942 Knights of the Round Table,无向图点双联通,二分图判定

    点击打开链接 无向图点双联通.二分图判定 <span style="font-size:18px;">#include <cstdio> #include ...

  3. CF687A. NP-Hard Problem[二分图判定]

    A. NP-Hard Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. COJ 0578 4019二分图判定

    4019二分图判定 难度级别: B: 编程语言:不限:运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 给定一个具有n个顶点(顶点编号为0,1,… ...

  5. hdoj 3478 Catch(二分图判定+并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3478 思路分析:该问题需要求是否存在某一个时刻,thief可能存在图中没一个点:将该问题转换为图论问题 ...

  6. HDU2444(KB10-B 二分图判定+最大匹配)

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  7. DFS的运用(二分图判定、无向图的割顶和桥,双连通分量,有向图的强连通分量)

    一.dfs框架: vector<int>G[maxn]; //存图 int vis[maxn]; //节点访问标记 void dfs(int u) { vis[u] = ; PREVISI ...

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

    二分图一•二分图判定 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 大家好,我是小Hi和小Ho的小伙伴Nettle,从这个星期开始由我来完成我们的Weekly. 新年回 ...

  9. 【二分图判定】hdu3478 Catch

    详细的题解:http://www.docin.com/p-517243379.html 一个图是二分图 等价于 其至少有两个节点且没有奇环. 二分图判定的方法:从任意点出发进行一次dfs黑白染色,若某 ...

随机推荐

  1. git clone cm source &amp; cm vs android version

    $ git clone https://github.com/CyanogenMod/android_packages_apps_DeskClock.git -b cm-9.0.0 CM4代表(And ...

  2. ASP.NET - URL中参数加密解密操作

    效果: 代码: using System; using System.Text; using System.IO; using System.Security.Cryptography; public ...

  3. 九度OnlineJudge之1018:统计同成绩学生人数

    题目描述: 读入N名学生的成绩,将获得某一给定分数的学生人数输出. 输入:                        测试输入包含若干测试用例,每个测试用例的格式为 第1行:N 第2行:N名学生的 ...

  4. spring Jdbc自己主动获取主键。

    学习了下springjdbc,感觉挺有用的,相对来说springjdbc 扩展性相当好了 package com.power.dao; import java.lang.reflect.Paramet ...

  5. [置顶] 单键模式的C++描述

    设计模式-单键(Signelton):其实单键的设计模式说来很简单,说的直白一点就是程序运行过程中保证只有一个实例在运行而已.在软件系统中,经常有这样一些特殊的类,必须保证它们在系统中只存在一个实例, ...

  6. poj 1018 Communication System 枚举 VS 贪心

    Communication System Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21631   Accepted:  ...

  7. linux shell 正则表达式(BREs,EREs,PREs)差异比较

    linux shell 正则表达式(BREs,EREs,PREs)差异比较 则表达式:在计算机科学中,是指一个用来描述或者匹配一系列符合某个句法规则的字符 串的单个字符串.在很多文本编辑器或其他工具里 ...

  8. kgdb调试注意事项

    0    首先提下注意事项的背景:    kgdb和printk共用一个串口 1    设置波特率:    //最高支持460800波特率    arm-eabi-gdb  ./vmlinux    ...

  9. TsFltMgr.sys其原因是,该系统蓝屏QQ计算机管理器!

    同事一WindowsXP系统,正常执行,关闭后,第二天无法启动.详细症状为: (1)安全模式以及带网络功能的安全模式都能够进入. (2)正常模式.还没出现WindowXP滚动栏就開始重新启动: (3) ...

  10. Hongwei Xi

    Hongwei Xi Hongwei Xi Hongwei Xi's Curriculum Vita Hongwei Xi