比赛 20120709 评测结果 EAAAAAEEEAAA
题目名称 聪明的推销员 最终得分 66
用户昵称 czp 运行时间 0.321 s
代码语言 Pascal 内存使用 91.78 MiB
提交时间 2012-07-09 11:49:45
显示代码纯文本
const mt:longint=15465666;
type px=^py;
py=record
 nx,ny:longint;
 n,ag:px;
 end;
var
 a,u:array[1..3000] of px;
 c:array[1..8000000] of px;
 h,vh:array[0..8000000] of longint;
 b,e,f:array[1..3000] of longint;
 ans,o:array[1..3000] of boolean;
 i,j,m,n,tot,r,x,y,d1,d2,mf,flow,q:longint;
 find:boolean;
 pp:px;
procedure add1(xx,yy:longint);
var pv:px;
begin
 new(pv);
 pv^.nx:=yy;
 pv^.n:=a[xx];
 a[xx]:=pv;
end;
procedure add2(xx,yy:longint);
var pv:px;
begin
 new(pv);
 pv^.nx:=yy;
 pv^.n:=u[xx];
 u[xx]:=pv;
end;
procedure add3(xx,yy,zz:longint);
var pv,po:px;
begin
 new(pv);
 pv^.nx:=yy;
 pv^.ny:=zz;
 pv^.n:=c[xx];
 c[xx]:=pv;
 new(po);
 po^.nx:=xx;
 po^.ny:=0;
 po^.n:=c[yy];
 c[yy]:=po;
 c[xx]^.ag:=c[yy];
 c[yy]^.ag:=c[xx];
end;
procedure dfs(vv:longint);
var j:longint;pt:px;
begin
 pt:=a[vv];
 while pt<>nil do
  begin
   j:=pt^.nx;
   if not o[j] then
    begin
     o[j]:=true;
     ans[j]:=true;
     add2(b[i],j);
     dfs(j);
    end;
   pt:=pt^.n;
  end;
end;
procedure sap(i:longint);
var mh,top,j:longint;pu:px;
begin
 mh:=tot-1;
 top:=mf;
 if i=tot then begin find:=true;inc(flow,mf); exit; end;
 pu:=c[i];
 while pu<>nil do
  begin
   j:=pu^.nx;
   if pu^.ny>0 then
    begin
     if h[i]=h[j]+1 then
      begin
       if mf>pu^.ny then mf:=pu^.ny;
       sap(j);
       if h[1]>=tot then exit;
       if find then break;
       mf:=top;
      end;
     if mh>h[j] then mh:=h[j];
    end;
   pu:=pu^.n;
  end;
 if not find then
  begin
   dec(vh[h[i]]);
   if vh[h[i]]=0 then h[1]:=tot;
   h[i]:=mh+1;
   inc(vh[h[i]]);
  end else begin
   dec(pu^.ny,mf);
   inc(pu^.ag^.ny,mf);
  end;
end;
begin
 assign(input,'salenet.in');reset(input);
 assign(output,'salenet.out');rewrite(output);
 readln(n);
 readln(q);
 for i:=1 to q do readln(b[i],e[i]);
 readln(r);
 for i:=1 to r do
  begin
   readln(x,y);
   add1(x,y);
  end;
 for i:=1 to q do
  begin
   fillchar(o,sizeof(o),0);
   o[b[i]]:=true;ans[b[i]]:=true;
   add2(b[i],b[i]);
   dfs(b[i]);
  end;
 for i:=1 to n do
  if not ans[i] then
   begin
    writeln('NO');
    writeln(i);
    close(input);close(output);
    halt;
   end;
 for i:=1 to n do begin f[i]:=i+1; add3(1,f[i],mt); end;
 tot:=n+1;
 for i:=1 to q do
  begin
   pp:=u[b[i]];
   inc(tot);
   d1:=tot;
   inc(tot);
   d2:=tot;
   add3(d1,d2,e[i]);
   while pp<>nil do
    begin
     j:=pp^.nx;
     add3(f[j],d1,mt);
     inc(tot);
     add3(d2,tot,mt);
     f[j]:=tot;
     pp:=pp^.n;
    end;
  end;
 inc(tot);
 for i:=1 to n do add3(f[i],tot,mt);
 vh[0]:=tot;
 while h[1]<tot do
  begin
   mf:=mt;
   find:=false;
   sap(1);
  end;
 writeln('YES');
 writeln(flow);
 close(input);close(output);
end.