1. 程式人生 > >Luogu P1525 關押罪犯

Luogu P1525 關押罪犯

str style blank oid bsp can urn http ++

傳送門

技術分享圖片
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 20005;
int fa[maxn],q[maxn];
int n,m;
struct abc {
    int a,b,c;
} p[200005];

int cmp(abc x,abc y) {
    return x.c > y.c;
}

int getfather(int x) {
    if(x == fa[x])return x;
    fa[x] = getfather(fa[x]);
    
return fa[x]; } void add(int x,int y){ x = getfather(x); y = getfather(y); fa[x] = y; } int main() { scanf("%d%d",&n,&m); for(int i = 1;i <= n;i++) fa[i] = i; for(int i = 1; i <= m; i++) scanf("%d%d%d",&p[i].a,&p[i].b,&p[i].c); sort(p
+1,p+m+1,cmp); for(int i = 1; i <= m; i++) { if(getfather(p[i].a) == getfather(p[i].b)){ printf("%d",p[i].c); return 0; } if(!q[p[i].a])q[p[i].a] = p[i].b; else add(q[p[i].a],p[i].b); if(!q[p[i].b])q[p[i].b] = p[i].a; else
add(q[p[i].b],p[i].a); } printf("0"); return 0; }
View Code

Luogu P1525 關押罪犯