一开始用的是先按怨气值由大到小排序,再判断二分图的方法,但是超时,多次调试未果,只好上网找了题解,下面介绍一个博客上的方法:http://blog.sina.com.cn/s/blog_918b1f91010175i2.html

 

 program prison;
type
  node=record
    x,y,v:longint;
  end;
var
  n,m,i,fx,fy:longint;
  a:array[1..100000] of node;
  father:array[1..40000] of longint;
procedure quick(left,right:longint);
var
  i,j,temp:longint;
  k:node;
begin
  i:=left;
  j:=right;
  temp:=a[(i+j) div 2].v;
  while i<=j do
  begin
    while a[i].v>temp do inc(i);
    while a[j].v<temp do dec(j);
    if i<=j then
    begin
      k:=a[i];
      a[i]:=a[j];
      a[j]:=k;
      inc(i);
      dec(j);
    end;
  end;
  if left<j then quick(left,j);
  if i<right then quick(i,right);
end;
function findfather(k:longint):longint;
begin
  if father[k]=k then findfather:=k
    else
    begin
      father[k]:=findfather(father[k]);
      findfather:=father[k];
    end;
end;
begin
  assign(input,'prison.in');
  reset(input);
  assign(output,'prison.out');
  rewrite(output);
  fillchar(a,sizeof(a),0);
  fillchar(father,sizeof(father),0);
  readln(n,m);
  for i:=1 to m do
    readln(a[i].x,a[i].y,a[i].v);
  quick(1,m);
  for i:=1 to n*2 do
    father[i]:=i;
  for i:=1 to m do
  begin
    fx:=findfather(a[i].x);
    fy:=findfather(a[i].y);
    if fx=fy then
    begin
      writeln(a[i].v);
      close(input);
      close(output);
      halt;
    end;
    father[fy]:=findfather(a[i].x+n);
    father[fx]:=findfather(a[i].y+n);
  end;
  writeln('0');
  close(input);
  close(output);
end.

调试的时候发现如果把father[i]的初始值设为-1,把函数findfather中的if father[k]=k改为if father[k]=-1的话,会有4个点栈溢出,其原因未明,希望各位大牛指教