水水的dp。拿pascal玩玩边表。然后莫名其妙地发现在main上先是ce然后全wa。下到数据一测就过了,交bzoj还是过了。不开心。
必定是一个中间点然后连三条出去。所以直接枚举中间点然后bfs就好了。相当于每条边被转移了两次所以还是O(n^2)的。最初想错了想成三方了把自己吓了一跳。
pascal啊。
type
edge = ^edger;
edger = record
t: longint;
next: edge;
end;
const
maxn = 5009;
//ONLINE_JUDGE = false;
ONLINE_JUDGE = true;
var
n, i, j, u, v: longint;
ans: int64;
d, cd, sd, td: array [0 .. maxn] of longint;
head: array [0 .. maxn] of edge;
ei: edge;
procedure addEdge(u, v: longint);
var
ep: edge;
begin
new(ep);
ep^. t := v;
ep^. next := head[u];
head[u] := ep;
end;
procedure bfs(p0: longint);
var
hd, tl, p: longint;
q: array [0 .. maxn] of longint;
e: edge;
begin
fillchar(cd, sizeof(cd), 0);
hd := 0;
tl := 1;
q[0] := p0;
d[p0] := 2;
while hd < tl do begin
p := q[hd];
inc(hd);
inc(cd[d[p]]);
e := head[p];
while e <> NIL do begin
if d[e^. t] = 0 then begin
d[e^. t] := d[p] + 1;
q[tl] := e^. t;
inc(tl);
end;
e := e^. next;
end;
end;
end;
begin
if not ONLINE_JUDGE then begin
assign(input, 'in.txt');
reset(input);
end;
readln(n);
for i := 2 to n do begin
readln(u, v);
addEdge(u, v);
addEdge(v, u);
end;
ans := 0;
for i := 1 to n do begin
fillchar(d, sizeof(d), 0);
fillchar(sd, sizeof(sd), 0);
fillchar(td, sizeof(td), 0);
d[i] := 1;
ei := head[i];
while ei <> nil do begin
bfs(ei^. t);
for j := 2 to n do begin
inc(ans, td[j] * cd[j]);
inc(td[j], sd[j] * cd[j]);
inc(sd[j], cd[j]);
end;
ei := ei^. next;
end;
end;
writeln(ans);
close(input);
end.