Submission #1134966
Source Code Expand
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
#include<cassert>
using namespace std;
#define y0 y0z
#define y1 y1z
#define yn ynz
#define j0 j0z
#define j1 j1z
#define jn jnz
#define tm tmz
#define buli(x) (__builtin_popcountll(x))
#define bur0(x) (__builtin_ctzll(x))
#define bul2(x) (63-__builtin_clzll(x))
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define fil(a,b) memset((a),(b),sizeof(a))
#define cl(a) fil(a,0)
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define rep(i,a,b) for (int i=(a),_ed=(b);i<_ed;i++)
#define per(i,a,b) for (int i=(b)-1,_ed=(a);i>=_ed;i--)
#define forg(i,gu) for (int i=gu;~i;i=e[i].next)
#define pw(x) ((ll(1))<<(x))
#define upmo(a,b) (((a)=((a)+(b))%mo)<0?(a)+=mo:(a))
#define mmo(a,b) (((a)=1ll*(a)*(b)%mo)<0?(a)+=mo:(a))
void getre(){int x=0;printf("%d\n",1/x);}
void gettle(){int res=1;while(1)res<<=1;printf("%d\n",res);}
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
template<typename T,typename S>inline bool upmin(T&a,const S&b){return a>b?a=b,1:0;}
template<typename T,typename S>inline bool upmax(T&a,const S&b){return a<b?a=b,1:0;}
template<typename N,typename PN>inline N flo(N a,PN b){return a>=0?a/b:-((-a-1)/b)-1;}
template<typename N,typename PN>inline N cei(N a,PN b){return a>0?(a-1)/b+1:-(-a/b);}
template<typename N>N gcd(N a,N b){return b?gcd(b,a%b):a;}
template<typename N>inline int sgn(N a){return a>0?1:(a<0?-1:0);}
#if ( ( _WIN32 || __WIN32__ ) && __cplusplus < 201103L)
#define lld "%I64d"
#else
#define lld "%lld"
#endif
inline void gn(long long&x){
int sg=1;char c;while(((c=getchar())<'0'||c>'9')&&c!='-');c=='-'?(sg=-1,x=0):(x=c-'0');
while((c=getchar())>='0'&&c<='9')x=x*10+c-'0';x*=sg;
}
inline void gn(int&x){long long t;gn(t);x=t;}
inline void gn(unsigned long long&x){long long t;gn(t);x=t;}
inline void gn(double&x){double t;scanf("%lf",&t);x=t;}
inline void gn(long double&x){double t;scanf("%lf",&t);x=t;}
inline void gs(char *s){scanf("%s",s);}
inline void gc(char &c){while((c=getchar())>126 || c<33);}
inline void pc(char c){putchar(c);}
#ifdef JCVB
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif
typedef long long ll;
typedef double db;
inline ll sqr(ll a){return a*a;}
inline db sqrf(db a){return a*a;}
const int inf=0x3f3f3f3f;
//const ll inf=0x3f3f3f3f3f3f3f3fll;
const db pi=3.14159265358979323846264338327950288L;
const db eps=1e-6;
const int mo=1e9+7;
//int qp(int a,ll b){int n=1;do{if(b&1)n=1ll*n*a%mo;a=1ll*a*a%mo;}while(b>>=1);return n;}
const int TREE_MAXV=3000+5;
struct edge{int v,next;}e[TREE_MAXV*2];int g[TREE_MAXV],etot;
int qu[TREE_MAXV],pre[TREE_MAXV];
int h[33333];
int n;
void ae(int u,int v){
e[etot].v=v;
e[etot].next=g[u];g[u]=etot++;
}
int bel[33333];
void bfs(int rt){
int p=0,q=0;
pre[rt]=0;
h[rt]=0;
qu[q++]=rt;
while(p!=q){
int u=qu[p++];
for (int i=g[u];~i;i=e[i].next)if(e[i].v!=pre[u]){
pre[e[i].v]=u;
bel[e[i].v]|=bel[u];
h[e[i].v]=h[u]+1;
qu[q++]=e[i].v;
}
}
}
void tree_init(){
static bool ini=0;
if(!ini){
ini=1;
memset(g,-1,sizeof(g));
}else{
for (int i=0;i<=n;i++)g[i]=-1;
}
etot=0;
}
void readedge(){
for (int i=1;i<n;i++){
int x,y;gn(x);gn(y);
ae(x,y);ae(y,x);
}
}
int boun[2222],to[2222];
const int zer=1111;
int f[1111][2222];
int dp(){
cl(f);
for (int i=n-1;i>=0;i--){
int u=qu[i];
for (int j=to[u]-boun[u];j<=to[u]+boun[u];j++){
f[u][j+zer]=1;
forg(t,g[u])if(e[t].v!=pre[u])mmo(f[u][j+zer],(f[e[t].v][j-1+zer]+f[e[t].v][j+1+zer]));
}
}
int ret=0;
rep(t,0,2222)upmo(ret,f[qu[0]][t]);
return ret;
}
int main()
{
#ifdef JCVB
//freopen("1.in","r",stdin);
//freopen("1.out","w",stdout);
int _time_jc=clock();
#endif
gn(n);
tree_init();
readedge();
static int dis1[2222];
bfs(1);
int ma=1;
rep(u,2,n+1)if(h[u]>h[ma])ma=u;
bfs(ma);
int mav=1;
rep(u,1,n+1)if(h[u]>h[mav])mav=u;
bfs(mav);
rep(i,1,n+1)dis1[i]=h[i];
bfs(ma);
int r;
r=cei(h[mav],2);
int rt=0,rt2=0;
if(h[mav]%2==0){
rep(i,1,n+1){
if(dis1[i]==h[i] && h[i]==h[mav]/2)rt=i;
}
}else{
rep(i,1,n+1){
if(dis1[i]==h[mav]/2 && h[i]==(h[mav]+1)/2)rt=i;
if(dis1[i]==(h[mav]+1)/2 && h[i]==(h[mav]+0)/2)rt2=i;
}
}
bel[rt2]=1;
bfs(rt);
int ans=0;
if(rt2==0){
rep(u,1,n+1)boun[u]=r-h[u],to[u]=0;
upmo(ans,dp());
}else{
rep(u,1,n+1){
int d;
if(!bel[u])d=r-1-h[u];else d=r-h[u];
if(bel[u]){
to[u]=0;
boun[u]=d;
}else{
to[u]=0;
boun[u]=d+1;
}
}
upmo(ans,dp());
/* rep(u,1,n+1){ */
/* int d; */
/* if(!bel[u])d=r-1-h[u];else d=r-h[u]; */
/* if(bel[u]){ */
/* to[u]=0; */
/* boun[u]=d; */
/* }else{ */
/* to[u]=0; */
/* boun[u]=d+1; */
/* if(d==0){ */
/* to[u]=1; */
/* boun[u]=0; */
/* } */
/* } */
/* } */
/* upmo(ans,-dp()); */
/* rep(u,1,n+1){ */
/* int d; */
/* if(!bel[u])d=r-1-h[u];else d=r-h[u]; */
/* if(bel[u]){ */
/* to[u]=0; */
/* boun[u]=d; */
/* }else{ */
/* to[u]=0; */
/* boun[u]=d+1; */
/* if(d==0){ */
/* to[u]=-1; */
/* boun[u]=0; */
/* } */
/* } */
/* } */
/* upmo(ans,-dp()); */
rep(u,1,n+1){
int d;
if(!bel[u])d=r-1-h[u];else d=r-h[u];
if(bel[u]){
to[u]=0;
boun[u]=d+1;
}else{
to[u]=0;
boun[u]=d;
}
}
upmo(ans,dp());
rep(u,1,n+1){
int d;
if(!bel[u])d=r-1-h[u];else d=r-h[u];
if(!bel[u]){
to[u]=0;
boun[u]=d;
}else{
to[u]=0;
boun[u]=d+1;
if(d==0){
to[u]=1;
boun[u]=0;
}
}
}
//upmo(ans,-dp());
rep(u,1,n+1){
int d;
if(!bel[u])d=r-1-h[u];else d=r-h[u];
if(!bel[u]){
to[u]=0;
boun[u]=d;
}else{
to[u]=0;
boun[u]=d+1;
if(d==0){
to[u]=-1;
boun[u]=0;
}
}
}
//upmo(ans,-dp());
/* rep(u,1,n+1){ */
/* int d; */
/* if(!bel[u])d=r-1-h[u];else d=r-h[u]; */
/* if(!bel[u]){ */
/* to[u]=1; */
/* boun[u]=d+1; */
/* if(d==0){ */
/* to[u]=0; */
/* boun[u]=0; */
/* } */
/* }else{ */
/* to[u]=0; */
/* boun[u]=d+1; */
/* if(d==0){ */
/* to[u]=1; */
/* boun[u]=0; */
/* } */
/* } */
/* } */
/* upmo(ans,dp()); */
/* rep(u,1,n+1){ */
/* int d; */
/* if(!bel[u])d=r-1-h[u];else d=r-h[u]; */
/* if(bel[u]){ */
/* to[u]=1; */
/* boun[u]=d+1; */
/* if(d==0){ */
/* to[u]=0; */
/* boun[u]=0; */
/* } */
/* }else{ */
/* to[u]=0; */
/* boun[u]=d+1; */
/* if(d==0){ */
/* to[u]=1; */
/* boun[u]=0; */
/* } */
/* } */
/* } */
/* upmo(ans,dp()); */
rep(u,1,n+1){
int d;
if(!bel[u])d=r-1-h[u];else d=r-h[u];
if(!bel[u]){
to[u]=0;
boun[u]=d;
}else{
to[u]=1;
boun[u]=d;
}
}
upmo(ans,-dp());
rep(u,1,n+1){
int d;
if(!bel[u])d=r-1-h[u];else d=r-h[u];
if(bel[u]){
to[u]=0;
boun[u]=d;
}else{
to[u]=1;
boun[u]=d;
}
}
upmo(ans,-dp());
}
printf("%d\n",ans);
#ifdef JCVB
debug("time: %d\n",int(clock()-_time_jc));
#endif
return 0;
}
Submission Info
Submission Time |
|
Task |
D - Oriented Tree |
User |
jcvb |
Language |
C++14 (GCC 5.4.1) |
Score |
1800 |
Code Size |
7724 Byte |
Status |
AC |
Exec Time |
16 ms |
Memory |
9984 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 |
4 ms |
9984 KB |
0_01.txt |
AC |
6 ms |
9984 KB |
0_02.txt |
AC |
7 ms |
9984 KB |
0_03.txt |
AC |
4 ms |
9984 KB |
1_00.txt |
AC |
6 ms |
9984 KB |
1_01.txt |
AC |
7 ms |
9984 KB |
1_02.txt |
AC |
16 ms |
9984 KB |
1_03.txt |
AC |
5 ms |
9984 KB |
1_04.txt |
AC |
6 ms |
9984 KB |
1_05.txt |
AC |
5 ms |
9984 KB |
1_06.txt |
AC |
7 ms |
9984 KB |
1_07.txt |
AC |
5 ms |
9984 KB |
1_08.txt |
AC |
7 ms |
9984 KB |
1_09.txt |
AC |
7 ms |
9984 KB |
1_10.txt |
AC |
5 ms |
9984 KB |
1_11.txt |
AC |
8 ms |
9984 KB |
1_12.txt |
AC |
5 ms |
9984 KB |
1_13.txt |
AC |
11 ms |
9984 KB |
1_14.txt |
AC |
6 ms |
9984 KB |
1_15.txt |
AC |
7 ms |
9984 KB |
1_16.txt |
AC |
7 ms |
9984 KB |
1_17.txt |
AC |
5 ms |
9984 KB |
1_18.txt |
AC |
5 ms |
9984 KB |
1_19.txt |
AC |
5 ms |
9984 KB |
1_20.txt |
AC |
6 ms |
9984 KB |
1_21.txt |
AC |
5 ms |
9984 KB |
1_22.txt |
AC |
5 ms |
9984 KB |
1_23.txt |
AC |
5 ms |
9984 KB |
1_24.txt |
AC |
5 ms |
9984 KB |
1_25.txt |
AC |
5 ms |
9984 KB |
1_26.txt |
AC |
5 ms |
9984 KB |
1_27.txt |
AC |
5 ms |
9984 KB |
1_28.txt |
AC |
5 ms |
9984 KB |