Submission #1134986
Source Code Expand
using System;
using System.Text;
using System.Collections.Generic;
class Solve{
int N;
List<int>[] T;
bool[] b;
long[,] DP;
public Solve(){}
StringBuilder sb;
public static int Main(){
new Solve().Run();
return 0;
}
void Run(){
sb = new StringBuilder();
Calc();
Console.Write(sb.ToString());
}
void Calc(){
N = int.Parse(Console.ReadLine());
T = new List<int>[N];
for(int i=0;i<N;i++){
T[i] = new List<int>();
}
for(int i=0;i<N-1;i++){
string[] str = Console.ReadLine().Split(' ');
int a = int.Parse(str[0])-1;
int b = int.Parse(str[1])-1;
T[a].Add(b);
T[b].Add(a);
}
int center1;
int center2;
int diamater;
{
Tree G = new Tree(T);
G.Center();
center1 = G.center1;
center2 = G.center2;
diamater = G.diamater;
}
if(center2 == -1){
long count = 0;
b = new bool[N];
DP = new long[N,2*N+2];
dfs(center1,diamater/2,N+1);
for(int i=0;i<2*N+2;i++){
count = (count + DP[center1,i])%Define.mod;
}
sb.Append(count+"\n");
}
else{
long count = 0;
b = new bool[N];
DP = new long[N,2*N+2];
b[center2] = true;
dfs(center1,diamater/2+1,N);
dfs(center2,diamater/2,N);
for(int i=1;i<2*N+1;i++){
count = (count + DP[center1,i]*(DP[center2,i-1]+DP[center2,i+1]))%Define.mod;
}
b = new bool[N];
DP = new long[N,2*N+2];
b[center2] = true;
dfs(center1,diamater/2,N+1);
dfs(center2,diamater/2+1,N+1);
for(int i=1;i<2*N+1;i++){
count = (count + DP[center1,i]*(DP[center2,i-1]+DP[center2,i+1]))%Define.mod;
}
b = new bool[N];
DP = new long[N,2*N+2];
b[center2] = true;
dfs(center1,diamater/2,N);
dfs(center2,diamater/2,N+1);
for(int i=1;i<2*N+1;i++){
count = (count + 2*(Define.mod-DP[center1,i])*(DP[center2,i-1]+DP[center2,i+1]))%Define.mod;
}
sb.Append(count+"\n");
}
}
void dfs(int v,int l,int Z){
b[v] = true;
for(int i=Z-l;i<=Z+l;i++){
DP[v,i] = 1;
}
for(int i=0;i<T[v].Count;i++){
int t = T[v][i];
if(!b[t]){
dfs(t,l-1,Z);
for(int j=Z-l;j<=Z+l;j++){
DP[v,j] = DP[v,j]*(DP[t,j+1]+DP[t,j-1])%Define.mod;
}
}
}
}
}
public class Tree{
List<int>[] T;
bool[] b;
int N;
int temp1;
int temp2;
public int center1;
public int center2;
public int diamater;
public Tree(List<int>[] G){
T = G;
N = G.Length;
}
void calcLongestPath(int v){
b = new bool[N];
temp1 = 0;
temp2 = 0;
dfs1(v,0);
}
public int LongestPathDistance(int v){
calcLongestPath(v);
return temp1;
}
public int LongestPath(int v){
calcLongestPath(v);
return temp2;
}
public void Diamater(){
int x1 = LongestPath(0);
int x2 = LongestPath(x1);
diamater = temp1;
}
public void Center(){
int x1 = LongestPath(0);
int x2 = LongestPath(x1);
diamater = temp1;
if(diamater % 2 == 0){
center1 = Search(x1,x2,diamater/2);
center2 = -1;
}
else{
center1 = Search(x1,x2,diamater/2);
center2 = Search(x1,x2,diamater/2+1);
}
}
public int Search(int s,int t,int l){
b = new bool[N];
dfs2(s,t,0,l);
return temp1;
}
void dfs1(int v,int l){
b[v] = true;
for(int i=0;i<T[v].Count;i++){
int t = T[v][i];
if(!b[t]){
dfs1(t,l+1);
}
}
if(l >= temp1){
temp1 = l;
temp2 = v;
}
}
bool dfs2(int v,int o,int l,int d){
b[v] = true;
bool x = v == o;
for(int i=0;i<T[v].Count;i++){
int t = T[v][i];
if(!b[t]){
x |= dfs2(t,o,l+1,d);
}
}
if(x && l == d){
temp1 = v;
}
return x;
}
}
public static class Define{
public const long mod = 1000000007;
}
Submission Info
Submission Time |
|
Task |
D - Oriented Tree |
User |
leign |
Language |
C# (Mono 4.6.2.0) |
Score |
1800 |
Code Size |
4834 Byte |
Status |
AC |
Exec Time |
70 ms |
Memory |
39100 KB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
1800 / 1800 |
Status |
|
|
Set Name |
Test Cases |
Sample |
0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt |
All |
0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt, 1_24.txt, 1_25.txt, 1_26.txt, 1_27.txt, 1_28.txt |
Case Name |
Status |
Exec Time |
Memory |
0_00.txt |
AC |
23 ms |
13268 KB |
0_01.txt |
AC |
22 ms |
9172 KB |
0_02.txt |
AC |
22 ms |
11220 KB |
0_03.txt |
AC |
23 ms |
11220 KB |
1_00.txt |
AC |
23 ms |
11220 KB |
1_01.txt |
AC |
39 ms |
26080 KB |
1_02.txt |
AC |
70 ms |
39100 KB |
1_03.txt |
AC |
26 ms |
23648 KB |
1_04.txt |
AC |
33 ms |
38916 KB |
1_05.txt |
AC |
27 ms |
23520 KB |
1_06.txt |
AC |
34 ms |
38384 KB |
1_07.txt |
AC |
27 ms |
25568 KB |
1_08.txt |
AC |
35 ms |
36804 KB |
1_09.txt |
AC |
35 ms |
38240 KB |
1_10.txt |
AC |
28 ms |
25568 KB |
1_11.txt |
AC |
39 ms |
38752 KB |
1_12.txt |
AC |
30 ms |
24032 KB |
1_13.txt |
AC |
50 ms |
37372 KB |
1_14.txt |
AC |
34 ms |
22496 KB |
1_15.txt |
AC |
37 ms |
23904 KB |
1_16.txt |
AC |
38 ms |
25952 KB |
1_17.txt |
AC |
26 ms |
23648 KB |
1_18.txt |
AC |
27 ms |
25568 KB |
1_19.txt |
AC |
26 ms |
23520 KB |
1_20.txt |
AC |
33 ms |
38664 KB |
1_21.txt |
AC |
27 ms |
25696 KB |
1_22.txt |
AC |
26 ms |
23520 KB |
1_23.txt |
AC |
27 ms |
25568 KB |
1_24.txt |
AC |
27 ms |
25568 KB |
1_25.txt |
AC |
26 ms |
23648 KB |
1_26.txt |
AC |
26 ms |
25440 KB |
1_27.txt |
AC |
27 ms |
23520 KB |
1_28.txt |
AC |
26 ms |
25568 KB |