当前位置:网站首页>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
边栏推荐
- Optimization cases of complex factor calculation: deep imbalance, buying and selling pressure index, volatility calculation
- Flask1.1.4 werkzeug1.0.1 source code analysis: Routing
- One click deployment of any version of redis
- 目前股票开户安全吗?可以直接网上开户吗。
- Details of C language integer and floating-point data storage in memory (including details of original code, inverse code, complement, size end storage, etc.)
- 理财产品要怎么选?新手还什么都不懂
- Tensorflow2.x下如何运行1.x的代码
- Micro service remote debug, nocalhost + rainbow micro service development second bullet
- I wrote a markdown command line gadget, hoping to improve the efficiency of sending documents by garden friends!
- 使用高斯Redis实现二级索引
猜你喜欢

网络原理(1)——基础原理概述

最新版本的CodeSonar改进了功能安全性,支持MISRA,C ++解析和可视化

VMWare中虚拟机网络配置

目标:不排斥 yaml 语法。争取快速上手

H3C S7000/S7500E/10500系列堆叠后BFD检测配置方法

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

Optimization cases of complex factor calculation: deep imbalance, buying and selling pressure index, volatility calculation
Klocwork 代码静态分析工具

Measure the height of the building

Static analysis of software defects codesonar 5.2 release
随机推荐
[paper reading] maps: Multi-Agent Reinforcement Learning Based Portfolio Management System
开发那些事儿:Go加C.free释放内存,编译报错是什么原因?
SQL注入报错注入函数图文详解
测量楼的高度
理财产品要怎么选?新手还什么都不懂
程序猿赚的那点钱算个P啊!
Make this crmeb single merchant wechat mall system popular, so easy to use!
Optimization cases of complex factor calculation: deep imbalance, buying and selling pressure index, volatility calculation
95年专注安全这一件事 沃尔沃未来聚焦智能驾驶与电气化领域安全
恶魔奶爸 C
Update iteration summary of target detection based on deep learning (continuous update ing)
H3C S7000/S7500E/10500系列堆叠后BFD检测配置方法
Network principle (1) - overview of basic principles
ISO 26262 - 基于需求测试以外的考虑因素
万字总结数据存储,三大知识点
备份 TiDB 集群到持久卷
写了个 Markdown 命令行小工具,希望能提高园友们发文的效率!
Phoenix JDBC
Guava multithreading, futurecallback thread calls are uneven
取两个集合的交集