zoukankan      html  css  js  c++  java
  • [NOIp2010提高组]关押罪犯

    OJ题号:洛谷1525

    思路:贪心。

    先将所有的人按怨气值从大到小排一下,然后依次尝试将双方分入两个不同的监狱,如果失败(即已分入相同的监狱),则输出这个怨气值。

     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<functional>
     4 struct Edge {
     5     int u,v,w;
     6     bool operator >(const Edge &x) const {
     7         return this->w>x.w;
     8     }
     9 };
    10 const int N=20001;
    11 class UnionFindSet {
    12     private:
    13         int anc[N<<1];
    14         int Find(int x) {
    15             return (x==anc[x])?x:(anc[x]=Find(anc[x]));
    16         }
    17     public:
    18         UnionFindSet(const int n) {
    19             for(int i=1;i<=n<<1;i++) anc[i]=i;
    20         }
    21         bool isConnected(const int x,const int y) {
    22             return Find(x)==Find(y);
    23         }
    24         void Union(const int x,const int y) {
    25             anc[Find(x)]=Find(y);
    26         }
    27 };
    28 int main() {
    29     int n,m;
    30     scanf("%d%d",&n,&m);
    31     Edge e[m];
    32     for(int i=0;i<m;i++) {
    33         scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
    34     }
    35     std::sort(&e[0],&e[m],std::greater<Edge>());
    36     UnionFindSet s(n);
    37     for(int i=0;i<m;i++) {
    38         if(s.isConnected(e[i].u,e[i].v)) {
    39             printf("%d
    ",e[i].w);
    40             return 0;
    41         }
    42         s.Union(e[i].u,e[i].v+n);
    43         s.Union(e[i].v,e[i].u+n);
    44     }
    45     puts("0");
    46     return 0;
    47 }
  • 相关阅读:
    第五篇:常用模块
    第三篇:函数
    第七篇:面向对象高级
    第二篇:流程控制、数据类型、字符编码、文件处理
    第一篇:python入门
    Python-计算机硬件基础
    PYTHON-UDP
    PYTHON-TCP 粘包
    PYTHON-网络通信 TCP
    English Voice of <<Something just like this>>
  • 原文地址:https://www.cnblogs.com/skylee03/p/6985911.html
Copyright © 2011-2022 走看看