樹狀DP入門例題

2021-09-26 09:21:10 字數 4524 閱讀 1524

p1352 沒有上司的舞會

/*

沒有上司的舞會

p1352

題意:有一場舞會,每乙個人都有乙個快樂值,但不能和上司同時出現在舞會中,問這個舞會的快樂值最大為多少

解法:樹狀dp

*/#include

using namespace std;

#define maxn 100000

struct nodeedge[maxn]

;int head[maxn]

,cnt;

int dp[maxn][2

];int a[maxn]

;void

init()

void

add(

int u,

int v)

void

dfs(

int u,

int fa)}}

intmain()

dfs(1,

-1);

cout<<

max(dp[1]

[0],dp[1]

[1])

}

the more, the better

/*

the more, the better

hdu - 1561

題意:每乙個城市都有一定的寶物,允許攻克m個城堡,有些城堡不能直接攻克,要攻克它先必須攻克指定的城堡,問最多可以得到多少寶物

解法:樹狀dp+01揹包

*/#include

#include

#include

using namespace std;

#define maxn 1000

int dp[maxn]

[maxn]

;int f[maxn]

[maxn]

;int as[maxn]

;struct nodeedge[maxn]

;int head[maxn]

;int cnt;

int n,m,a,b;

void

init()

void

add(

int u,

int v)

void

dfs(

int u)}}

for(

int i=

1;i<=m+

1;i++

) dp[u]

[i]=f[u]

[i-1

]+as[u];}

intmain()

dfs(0)

;printf

("%d\n"

,dp[0]

[m+1])

;}return0;

}

解法一:

/*

ural - 1018

題面:有一棵二叉蘋果樹,每個節點都有蘋果,現在讓你保留下來m條枝頭,問最多可以剩餘多少個蘋果

解法:樹狀dp + 01揹包

*/#include

#include

#include

using namespace std;

#define maxn 1000

int f[maxn]

[maxn]

;int as[maxn]

;struct nodeedge[maxn]

;int head[maxn]

;int cnt;

int n,m;

void

init()

intmax

(int a,

int b)

void

add(

int u,

int v,

int w)

void

dfs(

int u,

int fa)}}

}}intmain()

dfs(1,

1);printf

("%d\n"

,f[1

][m]);

}return0;

}

解法二:

/*

ural - 1018

題面:有一棵二叉蘋果樹,每個節點都有蘋果,現在讓你保留下來m條枝頭,問最多可以剩餘多少個蘋果

解法:dfs + 記憶化 + 列舉  

*/#include

#include

#include

using namespace std;

#define maxn 1000

int dp[maxn]

[maxn]

;int f[maxn]

[maxn]

;int as[maxn]

;struct nodeedge[maxn]

;int head[maxn]

;int cnt;

int n,m;

void

init()

intmax

(int a,

int b)

void

add(

int u,

int v,

int w)

void

dfs(

int u,

int fa)}}

}for

(int i=

1;i<=m+

1;i++

) dp[u]

[i]=f[u]

[i-1

]+as[u];}

intmain()

dfs(1,

1);printf

("%d\n"

,dp[1]

[m+1])

;}return0;

}

strategic game

/*

strategic game

poj - 1463

題面:最小點覆蓋

解法: 樹狀dp

*/#include

#include

#include

#include

using namespace std;

#define maxn 100000

struct nodeedge[maxn]

;int head[maxn]

;int dp[maxn][2

];int cnt;

void

init()

void

add(

int u,

int v)

void

dfs(

int u,

int fa)}}

intmain()

}dfs(0

,0);

cout<<

min(dp[0]

[0],dp[0]

[1])

<}return0;

}

cell phone network

/*

cell phone network

poj - 3659

題面:最小支配集

解法: 樹狀dp

*/#include

#include

#include

#include

using namespace std;

#define maxn 500000

#define inf 0x3f3f3f3f

struct nodeedge[maxn]

;int head[maxn]

;int dp[maxn][3

];int cnt;

void

init()

void

add(

int u,

int v)

void

dfs(

int u,

int fa)

else

if(dp[v][1

]!=inf&&dp[u][2

]!=inf) dp[u][2

]+=dp[v][1

];else dp[u][2

]=inf;

dp[u][1

]+=min

(dp[v][0

],dp[v][1

]);}

}if(inc==inf&&

!judge) dp[u][1

]=inf;

else

}int

main()

dfs(1,

1); cout<<

min(dp[1]

[0],

min(dp[1]

[1],dp[1]

[2]+

1))<}return0;

}

樹狀dp入門

description 實驗室被zser大魔王統治了,這天zser想選出一部分人看看他們對zser的恐懼值,不過實驗室中除了某雲飛之外每個人都有他最討厭的人,就像樹形結構一樣。zser選出的人中不能存在乙個人討厭乙個人。問zser選出的人可以帶來的最大恐懼值為多少。input 第1行1個正整數 n,...

POJ 2342 (樹狀DP)入門

入門比較好的部落格 include cstdio include iostream include cstring include algorithm using namespace std const int max 6005 樹形dp 入門 題意 公司舉辦 晚會,為了提高活躍度 每個人都有自己的...

樹狀陣列部分例題

poj 1037 給出n個星星的座標,如果乙個星星的左下方 包含正左和正下 有k顆星星,就說這顆星星是k級的,統計每個等級有多少個點。這題可用樹狀陣列,對於每個星星按y座標從小到大排序,相同y座標按x座標從小到大排序 題目中資料已經有序 輸入順序已排好序,那麼只要依次統計星星i之前x座標小於等於i....