当前位置:网站首页>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
边栏推荐
- Precautions for cjson memory leakage
- Alibaba cloud award winning experience: how to mount NAS file system through ECS
- 程序猿赚的那点钱算个P啊!
- 解决使用uni-app MediaError MediaError ErrorCode -5
- 论文解读(ValidUtil)《Rethinking the Setting of Semi-supervised Learning on Graphs》
- Apifox 接口一体化管理新神器
- Implement secondary index with Gaussian redis
- 使用 BR 恢复 Azure Blob Storage 上的备份数据
- awk处理JSON处理
- Micro service remote debug, nocalhost + rainbow micro service development second bullet
猜你喜欢
Codesonar Webinar
95年专注安全这一件事 沃尔沃未来聚焦智能驾驶与电气化领域安全
Codesonar enhances software reliability through innovative static analysis
Details of C language integer and floating-point data storage in memory (including details of original code, inverse code, complement, size end storage, etc.)
如何满足医疗设备对安全性和保密性的双重需求?
Make this crmeb single merchant wechat mall system popular, so easy to use!
使用高斯Redis实现二级索引
Onespin | solve the problems of hardware Trojan horse and security trust in IC Design
上海交大最新《标签高效深度分割》研究进展综述,全面阐述无监督、粗监督、不完全监督和噪声监督的深度分割方法
AADL inspector fault tree safety analysis module
随机推荐
取两个集合的交集
Apifox 接口一体化管理新神器
图扑数字孪生煤矿开采系统,打造采煤“硬实力”
[MySQL - Basic] transactions
使用枚举实现英文转盲文
How to implement safety practice in software development stage
使用高斯Redis实现二级索引
Implement secondary index with Gaussian redis
使用camunda做工作流设计,驳回操作
I wrote a markdown command line gadget, hoping to improve the efficiency of sending documents by garden friends!
2022如何评估与选择低代码开发平台?
《数字图像处理原理与实践(MATLAB版)》一书之代码Part2[通俗易懂]
Intelligent software analysis platform embold
机械臂速成小指南(十二):逆运动学分析
Phoenix JDBC
Solve the problem that the executable file of /bin/sh container is not found
Prometheus remote_write InfluxDB,unable to parse authentication credentials,authorization failed
Update iteration summary of target detection based on deep learning (continuous update ing)
VMWare中虚拟机网络配置
使用 BR 恢复 Azure Blob Storage 上的备份数据