HDU 4705

HDU 4705

题意:给出一棵树,求不能一笔划的点集{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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值