您的位置:首页 > 其它

【codevs 1069】关押罪犯

2016-10-30 00:00 316 查看
贪心
按影响排序
然后恩恩
如果same(x,y +n),说明x和y不在同一个监狱里
如果在合并之前,已经same(x,y)就输出答案

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAXN = 200000 + 5;
int n,m,p;
struct ChouHen
{
int f,t,v;
bool operator < (const ChouHen &b)const
{
return v > b.v;
}
}ch[MAXN];
int fa[MAXN];
int rank[MAXN];
void init()
{
for(int i = 0;i <= n + n;i ++)
fa[i] = i;
memset(rank,0,sizeof(rank));
return;
}
int find(int x)
{
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
void merge(int x,int y)
{
x = find(x);
y = find(y);
if(rank[x] > rank[y])
swap(x,y);
fa[x] = y;
if(rank[x] == rank[y])
rank[y] ++;
return;
}
bool same(int x,int y)
{
return find(x) == find(y);
}
int f,t;
int main()
{
scanf("%d %d",&n,&m);
init();
for(int i = 1;i <= m;i ++)
scanf("%d %d %d",&ch[i].f,&ch[i].t,&ch[i].v);
sort(ch + 1,ch + m + 1);
for(int i = 1;i <= m;i ++)
{
if(same(ch[i].f,ch[i].t))
{
printf("%d\n",ch[i].v);
return 0;
}
merge(ch[i].f,ch[i].t + n);
merge(ch[i].t,ch[i].f + n);
}
puts("0");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: