记录编号 39384 评测结果 AAAAAAAAAAAA
题目名称 聪明的推销员 最终得分 100
用户昵称 Gravatarfuhao 是否通过 通过
代码语言 Pascal 运行时间 0.060 s
提交时间 2012-07-09 20:08:20 内存使用 34.63 MiB
显示代码纯文本
program Tarjan;
const maxn=3001;
var
 f,low,dfn,time,temp,stack,fa:array[0..maxn] of longint;
 a:array[0..maxn,0..maxn] of longint;
 vv,v,into:array[0..maxn] of boolean;
 ans,i,n,p,r,index,top,x,y,t,j:Longint;
 function floodfill:longint;
 var i,t,h:longint;
 begin
  t:=1; h:=0;
  fillchar(v,sizeof(v),#0);
  f[1]:=0; v[0]:=true;
  repeat
   inc(h);
   for i:=1 to a[f[h],0] do
    if not v[a[f[h],i]] then
     begin
      v[a[f[h],i]]:=true;
      inc(t); f[t]:=a[f[h],i];
     end;
  until h>=t;
  for i:=0 to n do
   if not v[i] then exit(i);
  floodfill:=0;
 end;
 procedure push(k:longint);
 begin
  inc(top); stack[top]:=k; vv[k]:=true;
 end;
 function pop:longint;
 begin
  vv[stack[top]]:=false; pop:=stack[top]; dec(top);
 end;
 procedure dfs(k:longint);
 var i,tot,mintime,j:longint;
 begin
   v[k]:=true;
   inc(index);
   low[k]:=index; dfn[k]:=index;
   push(k);
   for i:=1 to a[k,0] do
     if not v[a[k,i]] then
      begin
       dfs(a[k,i]);
       if low[k]>low[a[k,i]] then
        low[k]:=low[a[k,i]];
      end else
     if vv[a[k,i]] then
     if low[k]>dfn[a[k,i]] then
      low[k]:=dfn[a[k,i]];
   if dfn[k]=low[k] then
    begin
     tot:=0; mintime:=maxlongint;
     repeat
      inc(tot);
      temp[tot]:=pop;
      if mintime>time[temp[tot]] then
       mintime:=time[temp[tot]];
     until temp[tot]=k;
     time[k]:=mintime;
     for i:=1 to tot do fa[temp[i]]:=k;
    end;
 end;
begin
 assign(input,'salenet.in'); reset(input);
 assign(output,'salenet.out'); rewrite(output);
 readln(n);
 readln(p);
 for i:=1 to n do
  time[i]:=maxlongint;
 for i:=1 to p do
  begin
   read(x,y);
   inc(a[0,0]);
   a[0,a[0,0]]:=x;
   time[x]:=y;
  end;
 readln(r);
 for i:=1 to r do
  begin
   read(x,y);
   inc(a[x,0]);
   a[x,a[x,0]]:=y;
  end;
 t:=floodfill;
 index:=0;
 fillchar(v,sizeof(v),#0);
 if t=0 then
  begin
   dfs(0);
   for i:=1 to n do
    for j:=1 to a[i,0] do
    if fa[i]<>fa[a[i,j]] then
     into[fa[a[i,j]]]:=true;
   for i:=1 to n do
    if not into[fa[i]] then
     begin
      ans:=ans+time[fa[i]];
      into[fa[i]]:=true;
     end;
   writeln('YES');
   writeln(ans);
  end else
  begin
   writeln('NO');
   writeln(t);
  end;
 close(input); close(output);
end.