当前位置:网站首页>AcWing 257. Explanation of prisoner detention (bipartite picture)
AcWing 257. Explanation of prisoner detention (bipartite picture)
2022-07-19 13:08:00 【Mr. Qiao Da】
AcWing 257. Detain criminals
Their thinking : Use two points to find the smallest mid value , The two points of the edge larger than this value need to be divided into two parts of the bipartite graph , That is to dye into two different colors . All points are dyed , Judge whether it is caused by conflict , If any, increase mid, If there is no conflict, we can continue to reduce mid, Because a bipartite graph with reduced edges must still be a bipartite graph
#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; // What is required in the question mid The minimum value of , When mid establish ,mid The values on the right are also true , but mid-1 Not necessarily , therefore r=mid
else l = mid + 1; // If this value does not hold , Then we need to find a larger value
}
printf("%d\n", r);
return 0;
}
边栏推荐
- JVM self study summary
- Amino metal organic framework material Fe MOF, fe-mil-88nh2 | Zr based metal organic framework catalyst (pt-uio-66) | Qiyue biology
- [C language programming 8] branch predictor
- A general memory management driver code is sorted out
- Visual ETL tool kettle concept, installation and practical cases
- Common bug precautions of audio control
- Using the case statement will produce a latch condition
- Atmospheric non isohalo effect
- 音频常见端子剖析图---再也不会搞错了
- Mysql的知识梳理
猜你喜欢

Li Kou 413 division of equal difference sequence dynamic programming

jvm自学总结

2022 global developer salary exposure: China ranks 19th, with an average annual salary of $23790

C语言进阶——字符函数和字符串函数
[email protected] (FE) composite nan"/>Metal organic framework material / polymer composite zif-8/p (TDA co HDA) | zinc oxide [email protected] (FE) composite nan

Preparation Notes: Matplotlib learning notes a

关于TCP/IP协议漏洞的安全措施

XML文件解析

Arbitrum Nova release! Create a low-cost and high-speed dedicated chain in the game social field

Return to risk ratio: the most important indicator of investment opportunities 2020-03-14
随机推荐
Brief analysis of circuit fault
关于“抄底”心态,让你筋疲力尽 2020-03-15
全球金融危机来袭,如何科学理性投资?2020-03-17
Ultrasonic sensor (chx01) learning notes Ⅲ - I2C reading and writing operation
Common bug precautions of audio control
力扣64-最小路径和——动态规划入门题型
市场“不确定性”中的投资逻辑 2020-03-18
Learning record: call TFTLCD
2022 global developer salary exposure: China ranks 19th, with an average annual salary of $23790
XML文件解析
XML modeling (easy to learn)
Growth of operation and maintenance Xiaobai - week 6 of Architecture
可视化ETL工具Kettle概念、安装及实战案例
RingBuffer
Cloud health management system based on STM32 (using Alibaba cloud Internet of things platform)
Qiyue supplies cumof nanocrystals loaded with methylene blue | femof nanosheets grown in situ on foam nickel | oxide nanowires /zif MOFs sugar gourd like Composites
About the "bottom reading" mentality, it makes you exhausted 2020-03-15
The latest Jilin construction safety officer simulation question bank and answers in 2022
Preparation Notes: Matplotlib learning notes a
运维小白成长记—架构第6周