NOIP2010提高组] CODEVS 1069 关押罪犯(并查集)
这道这么简单的题目还写了这么久..
将每个会发生冲突的两人的怒气进行排序,然后从怒气大到小,将两个人放到不同监狱中.假如两人都已经被放置且在同一监狱,这就是答案.
----------------------------------------------------------------------------------------
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#define rep(i,n) for(int i=0;i<n;i++)
#define clr(x,c) memset(x,c,sizeof(x))
using namespace std;
const int maxn=40000+5;
int p[maxn];
struct Edge {
int u,v,d;
Edge(int u,int v,int d):u(u),v(v),d(d) {}
bool operator < (const Edge &o) const { return d>o.d; }
};
vector<Edge> edges;
int find(int x) { return x==p[x] ? x : p[x]=find(p[x]); }
int main()
{
// freopen("test.in","r",stdin);
// freopen("test.out","w",stdout);
edges.clear();
int n,m;
scanf("%d%d",&n,&m);
rep(i,m) {
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
--a; --b;
edges.push_back( (Edge) {a,b,c} );
}
rep(i,n*2) p[i]=i;
sort(edges.begin(),edges.end());
rep(i,edges.size()) {
Edge &e=edges[i];
}
int ans=0;
rep(i,edges.size()) {
Edge &e=edges[i];
int x=find(e.u),y=find(e.v);
if(x==y) { ans=e.d; break; }
else { p[x]=find(e.v+n); p[y]=find(e.u+n); }
}
printf("%d\n",ans);
return 0;
}
----------------------------------------------------------------------------------------