bzoj 3522: [Poi2014]Hotel
呵呵,一开始天真的我以为求个 西格玛 C(??,3)就好了。。
(题解:比枚举2个数的再多一个,,一样搞)
1 #include <bits/stdc++.h> 2 #define LL long long 3 #define lowbit(x) x&(-x) 4 #define inf 1e15 5 using namespace std; 6 inline int ra() 7 { 8 int x=0,f=1; char ch=getchar(); 9 while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();} 10 while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();} 11 return x*f; 12 } 13 struct edge{int next,to;}e[10005]; 14 int tot[5005],mx,n,head[5005],cnt,s1[5005],s2[5005]; 15 LL ans; 16 void insert(int x, int y){e[++cnt].next=head[x]; e[cnt].to=y; head[x]=cnt;} 17 void dfs(int x, int fa, int deep) 18 { 19 tot[deep]++; 20 mx=max(mx,deep); 21 for (int i=head[x];i;i=e[i].next) 22 { 23 if (e[i].to==fa) continue; 24 dfs(e[i].to,x,deep+1); 25 } 26 } 27 int main(int argc, char const *argv[]) 28 { 29 n=ra(); 30 for (int i=1; i<n; i++) 31 { 32 int x=ra(),y=ra(); 33 insert(x,y); insert(y,x); 34 } 35 for (int x=1; x<=n; x++) 36 { 37 memset(s1,0,sizeof(s1)); 38 memset(s2,0,sizeof(s2)); 39 for (int i=head[x];i;i=e[i].next) 40 { 41 dfs(e[i].to,x,1); 42 for (int j=1; j<=mx; j++) 43 { 44 ans+=s2[j]*tot[j]; 45 s2[j]+=s1[j]*tot[j]; 46 s1[j]+=tot[j]; 47 } 48 for (int j=1; j<=mx; j++) tot[j]=0; 49 } 50 } 51 cout<<ans; 52 return 0; 53 }