题意:给出一棵树,求不能一笔划的点集{A,B,C}的数量。
简单树形DP吧,DP[ I ][ J ],以I点为根的子树中,不经过根的两点点集数量跟单点数量,每个子树相乘就行。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <sstream>
#include <iostream>
#include <algorithm>
#include<cstdlib>
#include<queue>
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
#define N 100005
#define L(x) x<<1
#define R(x) x<<1|1
#define M(x,y) (x + y)>>1
#define MOD 20090308
#define MODD 1000000006
#define inf 0x7fffffff
#define llinf 0x7fffffffffffffff
#define LL __int64
struct edge
{
LL v,next;
}s[N*2];
struct st
{
LL num;
LL c;
}a[N];
LL cnt,visit[N];
LL mark[N];
LL dp[N][2]; //0单,1双
LL ans,n;
void addedge(LL a,LL b)
{
s[cnt].v = b;
s[cnt].next = visit[a];
visit[a] = cnt++;
s[cnt].v = a;
s[cnt].next = visit[b];
visit[b] = cnt++;
}
void dfs(LL x)
{
mark[x] = 1;
for(LL i = visit[x];i != -1;i = s[i].next)
{
if(mark[s[i].v] == 1)
continue;
dfs(s[i].v);
ans += dp[x][0]*dp[s[i].v][1] + dp[x][1]*dp[s[i].v][0] + dp[s[i].v][1];
dp[x][1] += dp[s[i].v][0]*dp[x][0] + dp[s[i].v][1];
dp[x][0] += dp[s[i].v][0];
// cout<<x<<' '<<dp[x][0]<<' '<<dp[x][1]<<endl;
}
dp[x][0]++;
}
int main()
{
LL i,j,k;
LL t;
while(scanf("%I64d",&n) != EOF)
{
cnt = 0;
memset(visit,-1,sizeof(visit));
memset(mark,0,sizeof(mark));
memset(dp,0,sizeof(dp));
for(i = 1;i < n;i++)
{
scanf("%I64d%I64d",&j,&k);
addedge(j,k);
}
ans = 0;
dfs(1);
printf("%I64d\n",ans);
}
return 0;
}