bawang51吧 关注:31贴子:3,169
  • 0回复贴,共1

【OI】【NOIP2010】prison

只看楼主收藏回复

var a,b,c,s,f:array[1..1000]of longint;i,n,m,sa,sb:longint;procedure com(a,b:longint);begin s[f[a]]:=(s[a]+1) mod 2; f[f[a]]:=b;end;function get(k:longint):longint;var y:longint;begin if f[k]<>k then begin y:=f[k]; f[k]:=get(f[k]); s[k]:=(s[k]+s[y]) mod 2; end; exit(f[k]);end;procedure swap(var p,q:longint);var w:longint;begin w:=p; p:=q; q:=w;end;procedure qsort(s,t:longint);var i,j,t1,x:longint;begin i:=s; j:=t; x:=c[i]; repeat while (i<j) and (x<=c[j]) do j:=j-1; if i<j then begin swap(a[i],a[j]); swap(b[i],b[j]); swap(c[i],c[j]); end; while (i<j) and (x>=c[i]) do i:=i+1; if i<j then begin swap(a[i],a[j]); swap(b[i],b[j]); swap(c[i],c[j]); end; until i=j; c[i]:=x; i:=i+1; j:=j-1; if i<t then qsort(i,t); if s<j then qsort(s,j);end;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]); qsort(1,m); for i:=m downto 1 do begin sa:=get(a[i]); sb:=get(b[i]); if sa=sb then begin if s[a[i]]=s[b[i]] then begin writeln(c[i]); exit; end; end else com(a[i],b[i]); end; writeln(0);end.


IP属地:上海1楼2011-08-18 21:17回复