洛谷 P2279 [HNOI2003]消防局的设立

来源:互联网 发布:网址域名价格查询 编辑:程序博客网 时间:2024/05/30 19:32

题目描述

2020年,人类在火星上建立了一个庞大的基地群,总共有n个基地。起初为了节约材料,人类只修建了n-1条道路来连接这些基地,并且每两个基地都能够通过道路到达,所以所有的基地形成了一个巨大的树状结构。如果基地A到基地B至少要经过d条道路的话,我们称基地A到基地B的距离为d。

由于火星上非常干燥,经常引发火灾,人类决定在火星上修建若干个消防局。消防局只能修建在基地里,每个消防局有能力扑灭与它距离不超过2的基地的火灾。

你的任务是计算至少要修建多少个消防局才能够确保火星上所有的基地在发生火灾时,消防队有能力及时扑灭火灾。

输入输出格式

输入格式:
输入文件名为input.txt。

输入文件的第一行为n (n<=1000),表示火星上基地的数目。接下来的n-1行每行有一个正整数,其中文件第i行的正整数为a[i],表示从编号为i的基地到编号为a[i]的基地之间有一条道路,为了更加简洁的描述树状结构的基地群,有a[i]< i。

输出格式:
输出文件名为output.txt

输出文件仅有一个正整数,表示至少要设立多少个消防局才有能力及时扑灭任何基地发生的火灾。

输入输出样例

输入样例#1:
6
1
2
3
4
5
输出样例#1:
2

分析:就是一个树形dp,从叶子节点往根节点搜,设f[i,0]为i点设了消防局的设立,f[i,1]为i其中一个子节点设了消防局,f[i,2]为i一个儿子的儿子设立消防局,则转移很简单.

代码:

var  fa,deep,cmt,temp:array[1..1000]of longint;  bj:array[1..1000]of boolean;  n,i,j,k,t,x,y,z,ans:longint;procedure find(x:longint);var  i,j,k:longint;begin  if x=1 then deep[x]:=1 else    begin      if deep[fa[x]]>0 then deep[x]:=deep[fa[x]]+1      else begin        find(fa[x]);        deep[x]:=deep[fa[x]]+1;      end;    end;end;function can(x,y:longint):boolean;begin  if x=fa[y] then exit(true);  if y=fa[x] then exit(true);  if x=fa[fa[y]]then exit(true);  if y=fa[fa[x]]then exit(true);  if fa[x]=fa[y]then exit(true);  exit(false);end;procedure work(x:longint);var  i,j,k:longint;begin  if bj[x] then exit;  inc(ans);  bj[x]:=true;bj[fa[x]]:=true;bj[fa[fa[x]]]:=true;  k:=fa[fa[x]];  for i:=1 to n do    if(not bj[i])and(can(k,i))then bj[i]:=true;end;begin  readln(n);  for i:=1 to n do fa[i]:=i;  for i:=2 to n do    begin      readln(x);      fa[i]:=x;    end;  fillchar(deep,sizeof(deep),0);  for i:=1 to n do find(i);  for i:=1 to n do temp[i]:=deep[i];  for i:=1 to n do    begin      x:=0;      for j:=1 to n do        if temp[j]>x then          begin            x:=temp[j];y:=j;          end;      cmt[i]:=y;temp[y]:=0;    end;  fillchar(bj,sizeof(bj),false);  ans:=0;  for i:=1 to n do    work(cmt[i]);  writeln(ans);end.var  fa,deep,cmt,temp:array[1..1000]of longint;  bj:array[1..1000]of boolean;  n,i,j,k,t,x,y,z,ans:longint;procedure find(x:longint);var  i,j,k:longint;begin  if x=1 then deep[x]:=1 else    begin      if deep[fa[x]]>0 then deep[x]:=deep[fa[x]]+1      else begin        find(fa[x]);        deep[x]:=deep[fa[x]]+1;      end;    end;end;function can(x,y:longint):boolean;begin  if x=fa[y] then exit(true);  if y=fa[x] then exit(true);  if x=fa[fa[y]]then exit(true);  if y=fa[fa[x]]then exit(true);  if fa[x]=fa[y]then exit(true);  exit(false);end;procedure work(x:longint);var  i,j,k:longint;begin  if bj[x] then exit;  inc(ans);  bj[x]:=true;bj[fa[x]]:=true;bj[fa[fa[x]]]:=true;  k:=fa[fa[x]];  for i:=1 to n do    if(not bj[i])and(can(k,i))then bj[i]:=true;end;begin  readln(n);  for i:=1 to n do fa[i]:=i;  for i:=2 to n do    begin      readln(x);      fa[i]:=x;    end;  fillchar(deep,sizeof(deep),0);  for i:=1 to n do find(i);  for i:=1 to n do temp[i]:=deep[i];  for i:=1 to n do    begin      x:=0;      for j:=1 to n do        if temp[j]>x then          begin            x:=temp[j];y:=j;          end;      cmt[i]:=y;temp[y]:=0;    end;  fillchar(bj,sizeof(bj),false);  ans:=0;  for i:=1 to n do    work(cmt[i]);  writeln(ans);end.
0 1
原创粉丝点击