当前位置:网站首页>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
边栏推荐
- Read PG in data warehouse in one article_ stat
- 使用 BR 恢复 Azure Blob Storage 上的备份数据
- 目前股票开户安全吗?可以直接网上开户吗。
- 使用 BR 备份 TiDB 集群数据到 Azure Blob Storage
- Micro service remote debug, nocalhost + rainbow micro service development second bullet
- 万字总结数据存储,三大知识点
- Validutil, "Rethinking the setting of semi supervised learning on graphs"
- 上海交大最新《标签高效深度分割》研究进展综述,全面阐述无监督、粗监督、不完全监督和噪声监督的深度分割方法
- Intelligent software analysis platform embold
- npm uninstall和rm直接删除的区别
猜你喜欢
Implement secondary index with Gaussian redis
微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
解决使用uni-app MediaError MediaError ErrorCode -5
Cantata9.0 | new features
OneSpin 360 DV新版发布,刷新FPGA形式化验证功能体验
How to meet the dual needs of security and confidentiality of medical devices?
[paper reading] maps: Multi-Agent Reinforcement Learning Based Portfolio Management System
AADL inspector fault tree safety analysis module
How does codesonar help UAVs find software defects?
Airiot helps the urban pipe gallery project, and smart IOT guards the lifeline of the city
随机推荐
使用 BR 恢复 Azure Blob Storage 上的备份数据
使用camunda做工作流设计,驳回操作
恶魔奶爸 B3 少量泛读,完成两万词汇量+
如何满足医疗设备对安全性和保密性的双重需求?
Guava multithreading, futurecallback thread calls are uneven
Is it safe to open a stock account at present? Can I open an account online directly.
【奖励公示】第22期 2022年6月奖励名单公示:社区明星评选 | 新人奖 | 博客同步 | 推荐奖
Helix QAC 2020.2新版静态测试工具,最大限度扩展了标准合规性的覆盖范围
Measure the height of the building
使用高斯Redis实现二级索引
Mrs offline data analysis: process OBS data through Flink job
【C语言】指针进阶---指针你真的学懂了吗?
FTP steps for downloading files from Huawei CE switches
刚开户的能买什么股票呢?炒股账户安全吗
guava多线程,futurecallback线程调用不平均
恶魔奶爸 指南帖——简易版
论文解读(ValidUtil)《Rethinking the Setting of Semi-supervised Learning on Graphs》
Mysql子查询关键字的使用方式(exists)
阿洛的烦恼
How to meet the dual needs of security and confidentiality of medical devices?