当前位置:网站首页>UVA 11080 – Place the Guards(二分图判定)
UVA 11080 – Place the Guards(二分图判定)
2022-07-07 18:44:00 【全栈程序员站长】
大家好,又见面了,我是全栈君。
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;}发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116454.html原文链接:https://javaforall.cn
边栏推荐
猜你喜欢

使用camunda做工作流设计,驳回操作

Details of C language integer and floating-point data storage in memory (including details of original code, inverse code, complement, size end storage, etc.)

网络原理(1)——基础原理概述
![[paper reading] maps: Multi-Agent Reinforcement Learning Based Portfolio Management System](/img/76/b725788272ba2dcdf866b28cbcc897.jpg)
[paper reading] maps: Multi-Agent Reinforcement Learning Based Portfolio Management System
MySQL约束之默认约束default与零填充约束zerofill

Network principle (1) - overview of basic principles

C语言 整型 和 浮点型 数据在内存中存储详解(内含原码反码补码,大小端存储等详解)

Nebula importer data import practice

Ubuntu安装mysql8遇到的问题以及详细安装过程

CodeSonar如何帮助无人机查找软件缺陷?
随机推荐
目前股票开户安全吗?可以直接网上开户吗。
【网络原理的概念】
Phoenix JDBC
Dachang classic pointer written test questions
How to choose fund products? What fund is suitable to buy in July 2022?
[award publicity] issue 22 publicity of the award list in June 2022: Community star selection | Newcomer Award | blog synchronization | recommendation Award
npm uninstall和rm直接删除的区别
恶魔奶爸 A3阶段 近常速语流初接触
When easygbs cascades, how to solve the streaming failure and screen jam caused by the restart of the superior platform?
Implement secondary index with Gaussian redis
万字总结数据存储,三大知识点
Read PG in data warehouse in one article_ stat
使用camunda做工作流设计,驳回操作
How to implement safety practice in software development stage
Make this crmeb single merchant wechat mall system popular, so easy to use!
复杂因子计算优化案例:深度不平衡、买卖压力指标、波动率计算
网络原理(1)——基础原理概述
ISO 26262 - 基于需求测试以外的考虑因素
Validutil, "Rethinking the setting of semi supervised learning on graphs"
Solve the problem that the executable file of /bin/sh container is not found