当前位置:网站首页>AcWing 257. 关押罪犯 题解(二分图)
AcWing 257. 关押罪犯 题解(二分图)
2022-07-17 09:48:00 【乔大先生】
AcWing 257. 关押罪犯
解题思路:用二分找到最小的mid值,大于这个值的边的两个点需要分到二分图的两部分中,也就是染成两个不同的颜色。所有点都进行染色,判断是否由冲突,如果有的话就增大mid,如果没有冲突就可以继续减小mid,因为一个二分图减小一些边一定还是二分图
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 20010, M = 200010;
int n, m;
int h[N], e[M], w[M], ne[M], idx;
int color[N];
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}
bool dfs(int u, int c, int mid)
{
color[u] = c;
for (int i = h[u]; ~i; i = ne[i])
{
int j = e[i];
if (w[i] <= mid) continue;
if (color[j])
{
if (color[j] == c) return false;
}
else if (!dfs(j, 3 - c, mid)) return false;
}
return true;
}
bool check(int mid)
{
memset(color, 0, sizeof color);
for (int i = 1; i <= n; i ++ )
if (!color[i])
if (!dfs(i, 1, mid))
return false;
return true;
}
int main()
{
scanf("%d%d", &n, &m);
memset(h, -1, sizeof h);
while (m -- )
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c), add(b, a, c);
}
int l = 0, r = 1e9;
while (l < r)
{
int mid = l + r >> 1;
if (check(mid)) r = mid; //题中要求的mid的最小值,当mid成立,mid右侧的值也都成立,但mid-1不一定成立,所以r=mid
else l = mid + 1; //如果这个值不成立,那就要找一个更大的值
}
printf("%d\n", r);
return 0;
}
边栏推荐
- Case sharing | build a one-stop data development platform for hehe information based on linkis+dss
- 小说里的编程 【连载之十三】元宇宙里月亮弯弯
- Google play app store may delete the overview of APP permissions and use a new combination of data security information
- Zero basic C language
- MySql数据是如何存储在磁盘上存储的?
- Day 6 training
- Jsp+Ajax+Servlet+Mysql实现增删改查(一)
- 【CTF】pwn2_ sctf_ two thousand and sixteen
- Programming in the novel [serial 11] the moon bends in the yuan universe
- LabVIEW用了多线程,程序是不是会跑的更快些
猜你喜欢
![[untitled]](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[untitled]

二、品达通用权限系统__项目搭建

freeswitch的话单模块

Set the ID field to increase automatically when creating tables in SQL Server (Navicat demo)

MySQL 视图

Dynamic memory management

How is MySQL data stored on disk?
分库分表
![[paper notes] visual detection and capture method based on deep learning](/img/bd/3a204ad3341814cb1ef29b7fc8f324.png)
[paper notes] visual detection and capture method based on deep learning

Simple third-party component log desensitization
随机推荐
mysql 初始化修改密码问题
静态路由!!静态路由!!静态路由!!
After working hard, I found that there were so many messes around
Jsp+servlet+mysql case
【论文笔记】基于深度学习的视觉检测及抓取方法
QT serial communication
二进制安装 mysql 初始化密码问题
【CTF】pwn2_ sctf_ two thousand and sixteen
Jsp+Ajax+Servlet+Mysql实现增删改查(一)
软件工程师视角的项目合同分析
简易的第三方组件日志脱敏
DEDECMS织梦保存当前栏目更改时失败的解决方法
How long does software testing take?
C——指针
shell-笔记
KNN分類器
Target detection model size calculation, model complexity (parameter conversion formula)
C# - this 的用法
Programming in the novel [serial 14] the moon bends in the yuan universe
L1-088 静静的推荐(测试点1)