Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7574 | Accepted: 4340 |
Description
There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical structure of employees. It means that the supervisor relation forms a tree rooted at the rector V. E. Tretyakov. In order to make the party funny for every one, the rector does not want both an employee and his or her immediate supervisor to be PResent. The personnel office has evaluated conviviality of each employee, so everyone has some number (rating) attached to him or her. Your task is to make a list of guests with the maximal possible sum of guests' conviviality ratings.Input
Employees are numbered from 1 to N. A first line of input contains a number N. 1 <= N <= 6 000. Each of the subsequent N lines contains the conviviality rating of the corresponding employee. Conviviality rating is an integer number in a range from -128 to 127. After that go N – 1 lines that describe a supervisor relation tree. Each line of the tree specification has the form: L K It means that the K-th employee is an immediate supervisor of the L-th employee. Input is ended with the line 0 0Output
Output should contain the maximal sum of guests' ratings.Sample Input
711111111 32 36 47 44 53 50 0Sample Output
5題意:
某公司要舉辦一次晚會,但是為了使得晚會的氣氛更加活躍,每個參加晚會的人都不希望在晚會中見到他的直接上司,現在已知每個人的活躍指數和上司關系(當然不可能存在環),求邀請哪些人(多少人)來能使得晚會的總活躍指數最大。
樹形DP:dp[i]// 以i號人為根的關系樹,dp [i][1]表示當前樹 i 號人出席的價值和最大值,表示當前樹 i 號人不出席的價值和最大值。
方程:dp[i][0] +=Σ max(dp[son][0],dp[son][1]);
dp[i][1] +=Σ dp[son][0];
dp[son] 在dp[i]之前算,所以DFS。
沒什么好說的。。還是很水的,通過這個了解下樹形DP的概念吧,樹形DP通常有“樹”的關系,一般通過葉子節點向根節點傳遞信息,所以一般dfs在轉移方程之前。。還有就是怎么找根節點,要明白如果是一顆樹的話,只有根節點沒有父節點,其余的都有一個,所以找到那個沒有父節點的節點就是根節點,另外明白樹的構造,每兩個點之間都由一條邊,兩個子樹之間沒有邊直接把他們連接。
vector存圖
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#define me(a) memset(a, 0, sizeof(a))using namespace std;const int maxn = 6e3+5;//vector <int> v[maxn];vector< vector<int > > v(maxn);int dp[maxn][2], book[maxn], n;void dfs(int x){ for(int i = 0; i < v[x].size(); i++) { int to = v[x][i]; dfs(to); dp[x][1] += dp[to][0]; dp[x][0] += max(dp[to][1], dp[to][0]); }}int main(){ scanf("%d", &n); for(int i = 1; i <= n; i++) scanf("%d", &dp[i][1]); int x, y; for(int i = 1; i < n; i++) { scanf("%d%d", &x, &y); v[y].push_back(x); book[x] = 1; //這里是找根節點 } scanf("%d%d", &x, &y); int rt; for(int i = 1; i <= n; i++) { if(!book[i]) { rt = i; break; } } dfs(rt); printf("%d/n", max(dp[rt][0],dp[rt][1])); return 0;}網上前向星存圖代碼:#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>using namespace std;long long dp[6666][2];int cnt;int head[6666];int v[6666];int in[6666];struct edge{ int to,next;}E[6666];void addedge(int from , int to){ E[cnt].to = to; E[cnt].next = head[from]; head[from] = cnt++;}void dfs(int now){ for (int i = head[now] ; i != -1 ; i = E[i].next ) { int to = E[i].to; dfs(to); dp[now][0] += max(dp[to][1],dp[to][0]); dp[now][1] += dp[to][0]; } return;}int main(){ int N; //freopen("in.txt","r",stdin); ios::sync_with_stdio(false); cin >> N; memset(dp,0,sizeof(dp)); for (int i = 1; i <= N ; i++) { cin >> dp[i][1]; } int a,b; cnt = 0; memset(head,-1,sizeof head); long long start = N*(N+1)/2; while (cin >> a >> b &&a!=0&&b!=0) { addedge(b,a); start -= (long long)a; } dfs((int)start); cout << max(dp[start][1],dp[start][0]) << endl;}
新聞熱點
疑難解答