• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Sherlockyk
博客园    首页    新随笔    联系   管理    订阅  订阅

并查集——关押罪犯

关押罪犯

Time Limit:1000MS  Memory Limit:65536K

Description

Input

Output

Sample Input

4 6
1 4 2534
2 3 3512
1 2 28351
1 3 6618
2 4 1805
3 4 12884

Sample Output

3512

Hint

 

//具体详见以下题解
1
#include<cstdio> 2 #include<algorithm> 3 #define MAXN 100100 4 using namespace std; 5 struct node{ 6 int x,y,w; 7 }e[MAXN]; 8 int father[MAXN<<1]; 9 int n,m; 10 bool cmp(node a,node b){ 11 return a.w>b.w; 12 } 13 int find(int x){ 14 if(father[x]!=x)father[x]=find(father[x]); 15 return father[x]; 16 } 17 int main(){ 18 int i; 19 scanf("%d%d",&n,&m); 20 for(i=1;i<=m;++i){ 21 scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].w); 22 } 23 for(i=1;i<=n<<1;++i){ 24 father[i]=i; 25 } 26 sort(e+1,e+m+1,cmp); 27 for(i=1;i<=m;++i){ 28 int rx=find(e[i].x); 29 int ry=find(e[i].y); 30 if(rx==ry){ 31 printf("%d",e[i].w); 32 return 0; 33 } 34 father[rx]=find(e[i].y+n); 35 father[ry]=find(e[i].x+n); 36 }printf("0"); 37 return 0;}

 

 

 

posted @ 2018-03-17 11:20  Sherlockyk  阅读(153)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3