• 题解【luoguP1525 NOIp提高组2010 关押罪犯】


    题目链接


    题解

    算法

    • 一个经典的并查集
    • 但是需要用一点贪心的思想

    做法

    • 先将给的冲突们按冲突值从大到小进行排序(这很显然)
    • 然后一个一个的遍历它们
    • 如果发现其中的一个冲突里的两个人在同一个集合里,直接输出当时的冲突值
    • 否则,第一个人不妨设其为(x),用一个(op[x])来保存上一个与他冲突的人。如果(op[x])(0),也就是还没有上一个冲突,直接将(op[x])赋值为当时的第二个人(不妨设其为(y));如果有冲突,则直接将(op[x])(y)合并。

    代码

    #include <cstdlib>
    #include <cstdio>
    #include <algorithm>
    #define MAXN 20005
    #define MAXM 100005
    using namespace std;
    
    int n, m;
    int p[MAXN];
    int op[MAXN];//保存冲突 
    int ans;
    struct fight
    {
        int x, y;
        int s;
    }l[MAXM];
    
    bool cmp(fight t1, fight t2)
    {
        return t1.s > t2.s;
    }//结构体排序 
    
    int find(int x)
    {
        if(p[x] < 0) return x;
        return p[x] = find(p[x]); 
    }//路径压缩 
    
    void Union(int x, int y)
    {
        x = find(x);
        y = find(y);
        if(x == y) return ;
        if(p[x] > p[y]) swap(x, y);
        p[x] += p[y];
        p[y] = x;
    }//合并 
    int main()
    {
        scanf("%d%d", &n, &m);
        for(int i = 1; i <= n; i++) p[i] = -1;
        for(int i = 1; i <= m; i++) scanf("%d%d%d", &l[i].x, &l[i].y, &l[i].s);
        sort(l + 1, l + m + 1, cmp);
        for(int i = 1; i <= m; i++)
        {
            int a = l[i].x;
            int b = l[i].y;
            int fa = find(a);
            int fb = find(b);
            if(fa == fb)
            {
                ans = l[i].s;
                break;
            }
            else
            {
                if(op[a] == 0) op[a] = b; //如果没有冲突,将冲突设置为b 
                else Union(find(b), find(op[a]));//有冲突,合并 
                
                if(op[b] == 0) op[b] = a;
                else Union(find(a), find(op[b]));//同上
                
                //主思路 
            }
        }
        printf("%d
    ", ans);
        return 0;
    }
    
    
    蒟蒻一枚
  • 相关阅读:
    ubuntu12.04.2上利用cmake安装opencv2.4.6
    微软无线鼠标3500滚轮问题
    Linux 安装 Tomcat
    Linux 安装 MySQL
    Linux 安装 JDK
    Linux 安装 Redis 及踩坑
    IDEA Git 撤销push(回退到指定版本)
    【Java】Ajax实现级联城市
    node.js更换镜像源
    【Java】数据库连接池的简单使用
  • 原文地址:https://www.cnblogs.com/acfunction/p/8857102.html
Copyright © 2020-2023  润新知