zoukankan      html  css  js  c++  java
  • luoguP1525 关押罪犯 题解(NOIP2010)(并查集反集)

    P1525 关押罪犯  题目

    #include<iostream>
    #include<cstdlib>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<iomanip>
    #include<algorithm>
    #include<queue>
    #include<ctime>
    #define ll long long
    #define rg register
    #define M 100050
    #define N 20050
    using namespace std;
    
    int n,m;
    struct war{
        int l,r,v;
    }ljl[M];
    int fa[N<<1];
    
    inline int read()
    {
        int s=0,m=1;char ch=getchar();
        while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
        if(ch=='-')m=-1,ch=getchar();
        while(ch>='0'&&ch<='9')s=(s<<3)+(s<<1)+ch-'0',ch=getchar();
        return s*m;
    }
    
    inline bool cmp(const war &a,const war &b)
    {
        return a.v>b.v;
    }
    
    int find_fa(int kk)
    {
        if(fa[kk]==kk)return kk;
        else
            {
                fa[kk]=find_fa(fa[kk]);
                return fa[kk];
            }
    }
    
    void add(int k1,int k2)
    {
        int fa1=find_fa(k1);
        int fa2=find_fa(k2);
        fa[fa1]=fa2;
    }
    
    int main()
    {
        n=read();m=read();
        for(int i=1;i<=m;++i)
            {
                ljl[i].l=read();ljl[i].r=read();ljl[i].v=read();
            }
        sort(ljl+1,ljl+m+1,cmp);
        for(int i=1;i<=n<<1;++i)
            {
                fa[i]=i;
            }
        for(int i=1;i<=m;++i)
            {
                int f1=find_fa(ljl[i].l);
                int f2=find_fa(ljl[i].r);
                if(f1==f2)
                    {
                        printf("%d
    ",ljl[i].v);
                        exit(0);
                    }
                else
                    {
                        add(ljl[i].l,ljl[i].r+n);
                        add(ljl[i].r,ljl[i].l+n);
                    }
            }
        puts("0");
        return 0;
    }
  • 相关阅读:
    Linux如何自动获取IP地址
    jq操作select集合
    UDP and TCP
    IPv6
    DHCP, NAT
    队列之顺序存储实现
    ARP, Fragmentation and Reassembly
    Classless Interdomain Routing (CIDR)
    Subnet Routing Examples
    Subnetting
  • 原文地址:https://www.cnblogs.com/cjoierljl/p/8722632.html
Copyright © 2011-2022 走看看