• 洛谷 P1525 关押罪犯


    传送门

    AcWing 257 关押罪犯

    #include <bits/stdc++.h>
    
    using namespace std;
    using ll = long long;
    using p = pair<int, int>;
    const int maxn(2e4 + 10);
    const int maxm(2e5 + 10);
    int ecnt, head[maxn];
    int color[maxn];    // 0: 未染色; 1: 染白色; 2: 染黑色
    
    struct edge {
        int to, wt, nxt;
    } edges[maxm];
    
    template<typename T = int>
    inline const T read()
    {
        T x = 0, f = 1;
        char ch = getchar();
        while (ch < '0' || ch > '9') {
            if (ch == '-') f = -1;
            ch = getchar();
        }
        while (ch >= '0' && ch <= '9') {
            x = (x << 3) + (x << 1) + ch - '0';
            ch = getchar();
        }
        return x * f;
    }
    
    template<typename T>
    inline void write(T x, bool ln)
    {
        if (x < 0) {
            putchar('-');
            x = -x;
        }
        if (x > 9) write(x / 10, false);
        putchar(x % 10 + '0');
        if (ln) putchar(10);
    }
    
    void addEdge(int u, int v, int w)
    {
        edges[ecnt].to = v;
        edges[ecnt].wt = w;
        edges[ecnt].nxt = head[u];
        head[u] = ecnt++;
    }
    
    bool dfs(int u, int c, int val)
    {
        color[u] = c;
        for (int i = head[u]; compl i; i = edges[i].nxt) {
            int v = edges[i].to, w = edges[i].wt;
            if (w <= val) {
                continue;
            }
            if (color[v] == c) {
                return false;
            } else if (not color[v] and not dfs(v, 3 - c, val)) {
                return false;
            }
        }
        return true;
    }
    
    bool judge(int n, int val)
    {
        memset(color, 0, sizeof color);
        for (int i = 1; i <= n; ++i) {
            if (not color[i] and not dfs(i, 1, val)) {
                return false;
            }
        }
        return true;
    }
    
    int main()
    {
    #ifdef ONLINE_JUDGE
    #else
        freopen("input.txt", "r", stdin);
    #endif
        ios::sync_with_stdio(false);
        memset(head, -1, sizeof head);
        int n = read(), m = read();
        while (m--) {
            int u = read(), v = read(), w = read();
            addEdge(u, v, w);
            addEdge(v, u, w);
        }
        int low = 0, high = 1e9;
        while (low < high) {
            int mid = (low + high) / 2;
            if (judge(n, mid)) {
                high = mid;
            } else {
                low = mid + 1;
            }
        }
        write(low, true);
        return 0;
    }
    
  • 相关阅读:
    ios专题 - CocoaPods - 初次体验
    ios专题 - CocoaPods - 安装
    Objective-C浅拷贝和深拷贝
    支付宝交互流程
    UITabBar的隐藏
    iOS-容易造成循环引用的三种场景
    FMDB 的基本操作
    Swap file ".Podfile.swp" already exists!
    将UIImage保存成JPG或PNG格式存储在本地
    UI常用控件的一些属性
  • 原文地址:https://www.cnblogs.com/singularity2u/p/13932739.html
Copyright © 2020-2023  润新知