标签:des style blog http color io os ar java
DP:求出3点构成链的方案数 ,然后总方案数减去它
4 1 2 1 3 1 4
1Hint1. The only set is {2,3,4}. 2. Please use #pragma comment(linker, "/STACK:16777216")
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #pragma comment(linker, "/STACK:16777216") using namespace std; typedef long long int LL; const int maxn=100100; vector<int> g[maxn]; int n; LL ans,doubi[maxn],son[maxn]; void dfs(int u,int fa) { LL ndoubi=0,nson=0; son[u]=1; for(int i=0,sz=g[u].size();i<sz;i++) { int v=g[u][i]; if(v==fa) continue; dfs(v,u); son[u]+=son[v]; doubi[u]+=doubi[v]+son[v]; ans+=son[v]*nson; ans+=son[v]*ndoubi; ans+=doubi[v]*nson; ans+=doubi[v]; nson+=son[v]; ndoubi+=doubi[v]; } } int main() { while(scanf("%d",&n)!=EOF) { ans=0; memset(doubi,0,sizeof(doubi)); memset(son,0,sizeof(son)); for(int i=0;i<=n+10;i++) g[i].clear(); for(int i=0;i<n-1;i++) { int a,b; scanf("%d%d",&a,&b); g[a].push_back(b); g[b].push_back(a); } dfs(1,1); cout<<1LL*n*(n-1)*(n-2)/6-ans<<endl; } return 0; }
标签:des style blog http color io os ar java
原文地址:http://blog.csdn.net/ck_boss/article/details/40435475