NOIP2015模拟4

传送门

http://218.62.22.209:8080/problem.php?id=3181
http://218.62.22.209:8080/problem.php?id=3182
http://218.62.22.209:8080/problem.php?id=3183

T1 瓷片项链

题解

  • NOI2000
  • 枚举的话精度会有问题
  • max=0.3viv0i=0.3viv0i2
  • 我们发现根号下是个二次函数,其顶点为当i= v2v0 时最小,若i小数部分为0.5,输出0
var
 i,v,v0:longint;
 ans:real;
begin
 readln(v); readln(v0);
 ans:=v/v0/2;
 if ans-trunc(ans)=0.5 then writeln(0)
 else writeln(ans:0:0);
end.

T3 裁枝剪叶

题目大意

给你一个无根树,找出若干个联通的点,使所有点的权值和最大

题解

dp[i]=dp[son[i]]  dp[son[i]]>0
如果全负,注意特判
什么树形DP,DFS好吗…
无向边,居然按单向算的数组大小,QAQ

var
 w:array[0..50005,1..2]of longint;
 y,x:array[0..16005]of longint;
 dp:array[0..16005,0..1]of longint;
 i,j,k:longint;
 n,a,b,len,ans,min:longint;
function max(a,b:longint):longint;
begin
 if a>b then exit(a) else exit(b);
end;

procedure dfs(a:longint);
var tt:longint;
begin
 y[a]:=1; dp[a,1]:=x[a];
 if (w[a,1]=w[a,2])and(y[w[w[a,1],1]]=1)
 then begin dp[a,0]:=0; exit; end;
 tt:=w[a,2];
 while tt<>0 do
  begin
   if y[w[tt,1]]=0 then begin
    dfs(w[tt,1]);
    if dp[w[tt,1],1]>0 then inc(dp[a,1],dp[w[tt,1],1]);
    dp[a,0]:=max(dp[w[tt,1],1],dp[w[tt,1],0]);
   end;
   tt:=w[tt,2];
  end;
end;

procedure init(a,b:longint);
begin
 w[len,1]:=b;
 if w[a,2]=0
 then w[a,2]:=len else w[w[a,1],2]:=len;
 w[a,1]:=len; inc(len);
end;

begin
 readln(n); len:=n+1; min:=0; k:=0;
 for i:=1 to n do
  begin read(x[i]); min:=max(min,x[i]); if x[i]<>0 then k:=1; end;
 for i:=1 to n-1 do
  begin readln(a,b); init(a,b); init(b,a); end;
 fillchar(y,sizeof(y),0);
 dfs(1);
 ans:=max(dp[1,0],dp[1,1]);
 if (k=1)and(ans=0)
 then ans:=min;
 writeln(ans);
end.

你可能感兴趣的:(NOIP2015模拟4)