国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

POJ 2342 Anniversary party (最基礎樹形DP入門)

2019-11-10 19:07:47
字體:
來源:轉載
供稿:網友

Anniversary party
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 0 

Output

Output should contain the maximal sum of guests' ratings.

Sample Input

711111111 32 36 47 44 53 50 0

Sample 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;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新巴尔虎左旗| 淮滨县| 田东县| 桑植县| 彭州市| 深圳市| 沭阳县| 西乡县| 东乌| 本溪市| 宁化县| 兰溪市| 兴国县| 濉溪县| 大厂| 滁州市| 霸州市| 台江县| 广灵县| 安多县| 阿拉善右旗| 上高县| 秦安县| 衡东县| 仪陇县| 大姚县| 延安市| 罗平县| 遂溪县| 东台市| 铜山县| 安平县| 越西县| 彭州市| 鹤岗市| 石渠县| 沧州市| 莆田市| 安乡县| 黑河市| 葫芦岛市|