zoukankan      html  css  js  c++  java
  • [noip2010]关押罪犯 并查集

    第一次看的时候想到了并查集,但是不知道怎么实现;

    标解,f[i]表示i所属的集合,用f[i+n]表示i所属集合的补集,实现的很巧妙,可以当成一个使用并查集的巧妙应用;

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cstdlib>
     5 #include<map>
     6 #include<ctime>
     7 #include<vector>
     8 #include<set>
     9 #include<cmath>
    10 #include<algorithm>
    11 using namespace std;
    12 const int maxn=20200;
    13 struct node{
    14     int x,y,v;
    15     bool operator<(const node& b)const{return v>b.v;}
    16 }e[120000];
    17 int f[maxn<<1],n,m;
    18 void init(){
    19     scanf("%d%d",&n,&m);
    20     for(int i=1;i<=m;i++)scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].v);
    21     sort(e+1,e+m+1);
    22 }
    23 void print(int x){cout<<x<<endl;exit(0);}
    24 int getfather(int x){return f[x]==x?x:f[x]=getfather(f[x]);}
    25 void work(){
    26     for(int i=1;i<=(n<<1);i++)f[i]=i;
    27     int x,y;
    28     for(int i=1;i<=m;i++){
    29         x=getfather(e[i].x);y=getfather(e[i].y);
    30         if(x==y)print(e[i].v);
    31         else {f[x]=getfather(e[i].y+n),f[y]=getfather(e[i].x+n);}
    32     }
    33     print(0);
    34 }
    35 int main(){
    36     init();
    37     work();
    38 }
    View Code
  • 相关阅读:
    安装tomcat
    sed
    a'w'k
    dwr??
    tomcat-性能?
    windows清理命令
    markdown超链接怎么写?
    ※剑指offer系列19:二叉搜索树与双向链表
    剑指offer系列17:二叉搜索树的后序遍历序列
    剑指offer系列18:二叉树中和为某一值得路径
  • 原文地址:https://www.cnblogs.com/chadinblog/p/5854497.html
Copyright © 2011-2022 走看看