牛客小白月賽3 G 旅遊(樹形dp)

2022-05-08 13:42:06 字數 1387 閱讀 5409

分析:1、對於點cur,dp[cur][0]表示在該點住宿;dp[cur][1]表示其某個子結點住宿,自己被訪問到;dp[cur][2]表示其父結點住宿,自己被訪問到;

2、如果dp[cur][0],則cur的子結點全是2狀態

3、如果dp[cur][2],則cur的子結點要麼為0狀態,要麼為1狀態,取這兩個狀態的最大值累加。

4、如果dp[cur][1],則cur的子結點至少有乙個為0狀態,其餘的為1狀態,同樣是取兩個狀態的最大值累加;

不過,若每個子結點都是取1狀態時為最優,則必須在他們當中找出乙個損失最小的0狀態,所以記錄每個結點1狀態與0狀態差值的最小值,然後將變為0狀態後損失最小的結點變為0狀態即可。

#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#define lowbit(x) (x & (-x))

const double eps = 1e-8;

inline int dcmp(double a, double b)

typedef long long ll;

typedef unsigned long long ull;

const int int_inf = 0x3f3f3f3f;

const int int_m_inf = 0x7f7f7f7f;

const ll ll_inf = 0x3f3f3f3f3f3f3f3f;

const ll ll_m_inf = 0x7f7f7f7f7f7f7f7f;

const int dr = ;

const int dc = ;

const double pi = acos(-1.0);

const int maxn = 500000 + 10;

const int maxt = 10000 + 10;

using namespace std;

int dp[maxn][3];

vectorv[maxn];

int dfs(int cur, int id, int fa)

}else if(id == 1 && v[cur].size() > 0)

else

}if(!ok)

}else if(id == 2)

}return dp[cur][id];

}int main()

int x, y;

for(int i = 0; i < n - 1; ++i)

printf("%d\n", dfs(s, 0, -1));

return 0;

}

牛客小白月賽3

a 我們規定母音字母有a e i o u,並且規定半母音字母y也是母音字母。cwbc在學習英語,xhrlyb為了讓cwbc的記憶更加深刻,於是她讓cwbc把每個字串的所有字母都變成乙個恰好不大於它本身的小寫母音字母。可是cwbc比較貪玩,並且他想讓你幫他完成這個任務。聰明的你在仔細閱讀題目後,一定可...

牛客小白月賽 3

按照題目模擬就行了,注意題目母音加了乙個y就行 include include using namespace std const int maxn 2e5 5 char yuan 6 intmain cout 字首和的使用 c,cw,cwb,cwbc都是根據上乙個狀態來進行的所以,每乙個狀態就是上...

A 音標 牛客小白月賽3

題目描述 我們規定母音字母有a e i o u,並且規定半母音字母y也是母音字母。cwbc在學習英語,xhrlyb為了讓cwbc的記憶更加深刻,於是她讓cwbc把每個字串的所有字母都變成乙個恰好不大於它本身的小寫母音字母。可是cwbc比較貪玩,並且他想讓你幫他完成這個任務。聰明的你在仔細閱讀題目後,...