1 条题解
-
0
C++ :
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; struct AngryInt { int a,b; long long c; }ang[100001]; int N,M,h[40001]; bool flag; int Find(int x) { if(h[x]==x) return x; return h[x]=Find(h[x]); } bool comp(AngryInt x,AngryInt y) { return x.c<y.c; } int main() { cin>>N>>M; for(int i=1;i<=N*2;i++) h[i]=i; for(int i=1;i<=M;i++) scanf("%d%d%lld",&ang[i].a,&ang[i].b,&ang[i].c); sort(ang+1,ang+M+1,comp); for(int i=M;i>=1;i--) { int x=Find(ang[i].a),y=Find(ang[i].b); if(x==y) { printf("%lld",ang[i].c); flag=true; break; } else { h[x]=Find(ang[i].b+N); h[y]=Find(ang[i].a+N); } } if(!flag) printf("0"); return 0; }
Pascal :
program prison; var a,b,c:array[1..100000]of longint; f,h:array[1..20000]of longint; m,n,i,j,t:longint; procedure qs(l,r:longint); var i,j,m:longint; begin i:=l; j:=r; m:=c[(i+j) div 2]; repeat while c[i]>m do inc(i); while c[j]<m do dec(j); if i<=j then begin t:=c[i]; c[i]:=c[j]; c[j]:=t; t:=a[i]; a[i]:=a[j]; a[j]:=t; t:=b[i]; b[i]:=b[j]; b[j]:=t; inc(i); dec(j); end; until i>j; if j>l then qs(l,j); if i<r then qs(i,r); end; procedure init; begin readln(n,m); for i:= 1 to n do f[i]:=i; for i:= 1 to m do readln(a[i],b[i],c[i]); qs(1,m); end; function get(i:longint):longint; begin if i=f[i] then exit(i); f[i]:=get(f[i]); exit(f[i]); end; procedure union(a,b:longint); var x,y:longint; begin x:=get(a); y:=get(b); if x<>y then f[x]:=y; end; procedure main; var x,y,i,j:longint; begin for i:= 1 to m do begin x:=get(a[i]); y:=get(b[i]); if x=y then begin writeln(c[i]); halt; end; if (h[a[i]]=0)and(h[b[i]]=0) then begin h[a[i]]:=b[i]; h[b[i]]:=a[i]; continue; end; if h[a[i]]=0 then h[a[i]]:=b[i]; if h[b[i]]=0 then h[b[i]]:=a[i]; union(b[i],h[a[i]]); union(a[i],h[b[i]]); end; writeln(0); end; begin init; main; end.
- 1
信息
- ID
- 282
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者