USACO 4.3 Street Race

题意:就不告诉你。

分析:一道很水的图论题。第一问只要枚举把每个点删掉后用dfs判断起点能否走到终点就好了。第二问,很明显一个中间点一定满足第一问的要求,所以就在第一问找到的点中枚举就好了。

代码:

{
ID: ymwbegi1
PROG: race3
LANG: PASCAL
}

var
  x,i,s,t,e1,u1,tot,n:longint;
  a:array[1..50,0..50] of longint;
  v:array[1..50] of boolean;
  e,u:array[1..50] of longint;
  flag:boolean;

procedure dfs(x,y:longint);
var
  i:longint;
begin
  v[x]:=false;
  inc(tot);
  if x=t then
  begin
    flag:=false;
    exit;
  end;
  for i:=1 to a[x,0] do
    if (v[a[x,i]])and(a[x,i]<>y) then dfs(a[x,i],y);
end;

begin
  assign(input,'race3.in');
  assign(output,'race3.out');
  reset(input);
  rewrite(output);
  read(x);
  fillchar(v,sizeof(v),true);
  while x<>-1 do
  begin
    inc(n);
    if x=-2 then t:=n;
    while x<>-2 do
    begin
      inc(a[n,0]);
      a[n,a[n,0]]:=x+1;
      v[x+1]:=false;
      read(x);
    end;
    read(x);
  end;
  for i:=1 to n do
    if v[i] then
    begin
      s:=i;
      break;
    end;
  if s=0 then s:=1;
  for i:=1 to n do
    if (i<>s)and(i<>t) then
    begin
      flag:=true;
      fillchar(v,sizeof(v),true);
      dfs(s,i);
      if flag then
      begin
        inc(u1);
        u[u1]:=i;
      end;
    end;
  write(u1);
  for i:=1 to u1 do
    write(' ',u[i]-1);
  writeln;
  for i:=1 to u1 do
  begin
    tot:=0;
    fillchar(v,sizeof(v),true);
    dfs(s,u[i]);
    fillchar(v,sizeof(v),true);
    dfs(u[i],0);
    if tot=n then
    begin
      inc(e1);
      e[e1]:=u[i];
    end;
  end;
  write(e1);
  for i:=1 to e1 do
    write(' ',e[i]-1);
  writeln;
  close(input);
  close(output);
end.


你可能感兴趣的:(USACO 4.3 Street Race)