UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#184666#2054. 小G的树gispzjz1004ms5064kbC++112.4kb2023-09-09 15:09:522023-09-09 15:09:54

answer

//============================================================================
// Author       : Sun YaoFeng
//============================================================================

//#pragma 	comment(linker, "/STACK:100240000,100240000")
//#include	<cstdio>
//#include	<cstdlib>
//#include	<cstring>
//#include	<algorithm>

#include	<bits/stdc++.h>

using	namespace	std;

#define DB		long double
#define	lf		else if
#define I64		long long
#define	Rd()	(rand()<<15|rand())
#define For(i,a,b)	for(int i=a,lim=b;i<=lim;i++)
#define Rep(i,a,b)	for(int i=a,lim=b;i>=lim;i--)

#define	fi	first
#define se	second
#define MK	make_pair
#define PA	pair<int, int>

//#define	min(a,b)	((a)<(b)?(a):(b))
//#define	max(a,b)	((a)<(b)?(b):(a))

#define	CH	(ch=getchar())
int		IN()	{
		int x= 0, f= 0, ch;
		for	(; CH < '0' || ch > '9';)	f= (ch == '-');
		for	(; ch >= '0' && ch <= '9'; CH)	x= x*10 + ch -'0';
		return	f? -x : x;
}

#define n	105
#define m	205

int		N, D, st[n], Max[n];

DB		F[n][m][m], G[2][m][m][m];

struct	Lin{
		int v, next;
}E[m];

void	Link(int u, int v){
		E[++ D]= (Lin){v, st[u]};	st[u]= D;
		E[++ D]= (Lin){u, st[v]};	st[v]= D;
}

void	DFS(int u, int f){
		for (int i= st[u], v; i; i= E[i].next)
			if	((v= E[i].v) != f)	DFS(v, u);
		
		For(i, 0, 2*N)	G[0][0][0][i]= 1.;
		
		int p= 0, q= 1;
		for (int i= st[u], v; i; i= E[i].next)
		if	((v= E[i].v) != f)	{
			For(x, 0, Max[v])	For(y, 1, 2*N)	F[v][x][y]+= F[v][x][y-1];
			
			p^= 1;
			q^= 1;
			
			For(x, 0, Max[u])	For(y, 0, x)	For(z, 0, 2*N){
				DB val= G[q][x][y][z];
				G[q][x][y][z]= 0;
				
				For(l, 0, Max[v])	For(t, 1, 2)	{
					DB	tmp= F[v][l][z] * .5;
					
					if	(l+t > x)	G[p][l+t][x][z]+= val*tmp;
					lf	(l+t > y)	G[p][x][l+t][z]+= val*tmp;
						else	G[p][x][y][z]+= val*tmp;
				}
			}
			
			Max[u]= max(Max[u], Max[v] + 2);
		}
		
		For(x, 0, Max[u])	For(y, 0, x)	Rep(z, 2*N, 0){
			DB	t= G[p][x][y][z] - (z ? G[p][x][y][z-1] : 0);
			G[p][x][y][z]= 0;
	
			if	(x+y <= 2*N)	F[u][x][max(x+y, z)]+= t;
		}
}

class	DiameterOfRandomTree{
	public:
		double	getExpectation(){
			N= IN();
			For(i, 2, N)	Link(IN(), IN());
			
			DFS(1, 0);
			
			DB	Ans= 0;
			For(i, 0, Max[1])	For(j, 0, 2*N)	Ans+= j*F[1][i][j];
			
			return	Ans;
		}
}GTW;

int		main(int argc, char* argv[]){
	
		printf("%.12lf\n", GTW.getExpectation());
		
		return	0;
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 2ms
memory: 2236kb

input:

15
1 2
2 3
3 4
4 5
5 6
6 7
7 8
4 9
8 10
1 11
8 12
5 13
13 14
1 15

output:

14.000000000000

result:

ok single line: '14.000000000000'

Test #2:

score: 10
Accepted
time: 0ms
memory: 3364kb

input:

15
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15

output:

21.000000000000

result:

ok single line: '21.000000000000'

Test #3:

score: 10
Accepted
time: 1ms
memory: 1680kb

input:

15
1 2
1 3
3 4
2 5
2 6
4 7
1 8
4 9
8 10
3 11
10 12
1 13
9 14
3 15

output:

10.546875000000

result:

ok single line: '10.546875000000'

Test #4:

score: 10
Accepted
time: 1ms
memory: 1604kb

input:

15
1 2
1 3
1 4
2 5
4 6
1 7
1 8
3 9
2 10
4 11
10 12
4 13
4 14
12 15

output:

9.593750000000

result:

ok single line: '9.593750000000'

Test #5:

score: 10
Accepted
time: 0ms
memory: 1652kb

input:

15
1 2
1 3
1 4
1 5
1 6
1 7
6 8
4 9
8 10
5 11
5 12
4 13
6 14
10 15

output:

9.593750000000

result:

ok single line: '9.593750000000'

Test #6:

score: 10
Accepted
time: 0ms
memory: 3064kb

input:

20
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
4 11
1 12
10 13
3 14
2 15
14 16
12 17
3 18
11 19
2 20

output:

18.000000000000

result:

ok single line: '18.000000000000'

Test #7:

score: 10
Accepted
time: 0ms
memory: 5064kb

input:

20
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20

output:

28.500000000000

result:

ok single line: '28.500000000000'

Test #8:

score: 10
Accepted
time: 0ms
memory: 1832kb

input:

20
1 2
2 3
3 4
2 5
5 6
5 7
4 8
4 9
6 10
3 11
3 12
11 13
6 14
11 15
5 16
9 17
9 18
10 19
7 20

output:

12.304687500000

result:

ok single line: '12.304687500000'

Test #9:

score: 10
Accepted
time: 0ms
memory: 2248kb

input:

20
1 2
2 3
3 4
1 5
4 6
3 7
1 8
1 9
6 10
6 11
7 12
6 13
13 14
8 15
9 16
15 17
9 18
14 19
10 20

output:

15.078125000000

result:

ok single line: '15.078125000000'

Test #10:

score: 10
Accepted
time: 0ms
memory: 1560kb

input:

20
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
7 11
5 12
6 13
11 14
5 15
13 16
2 17
1 18
10 19
13 20

output:

9.368103027344

result:

ok single line: '9.368103027344'