HDU P1520 Anniversary party
来源:互联网 发布:华师大网络教育好过吗 编辑:程序博客网 时间:2024/05/16 17:43
HDU P1520 Anniversary party
题目
Problem 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 T 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
题目大意
有
对于这
有多组数据,对于
题解
树形
代码
#include<cstdio>#include<cstring>using namespace std;int n,tot;int lnk[6005],d[6005],f[6005][2],w[6005];struct edge{ int nxt,y;} e[6005];int readln(){ int x=0,f=1; char ch=getchar(); while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();} while ('0'<=ch&&ch<='9') x=x*10+ch-48,ch=getchar(); return x*f;}int add(int x,int y){ tot++;e[tot].nxt=lnk[x];lnk[x]=tot;e[tot].y=y;}int max(int x,int y){return x>y?x:y;}void dfs(int x){ f[x][0]=0; f[x][1]=w[x]; for (int i=lnk[x];i;i=e[i].nxt) { int y=e[i].y; dfs(y); f[x][0]+=max(f[y][0],f[y][1]); f[x][1]+=f[y][0]; }}int main(){ while(~scanf("%d",&n)) { memset(d,0,sizeof(d)); memset(f,0,sizeof(f)); memset(lnk,0,sizeof(lnk)); for (int i=1;i<=n;i++) w[i]=readln(); tot=0; while (true) { int x=readln(),y=readln(); if (!x&&!y) break; add(y,x); d[x]++; } for (int i=1;i<=n;i++) if (!d[i]) { dfs(i); printf("%d\n",max(f[i][0],f[i][1])); break; } } return 0;}
- HDU P1520 Anniversary party
- HDU 1520 Anniversary party
- HDU-1520-Anniversary party
- HDU-1520-Anniversary party
- HDU 1520(Anniversary party)
- HDU 1520 Anniversary party
- hdu 1520 Anniversary party
- Hdu 1520 Anniversary party
- HDU 1520 Anniversary party
- hdu 1520 Anniversary party
- hdu 1520 Anniversary party
- HDU 1520 Anniversary party
- hdu 1520 Anniversary party
- hdu【1520】Anniversary party
- HDU 1520 Anniversary party
- HDU-1520 Anniversary party
- HDU 1520 Anniversary party
- hdu 1520 Anniversary party
- Android 点击 Home 键导致 APP 重启
- 三、tar包安装mysql
- 打印PDF报错:STSong-Light' with 'UniGB-UCS2-H' is not recognized
- 工厂模式
- jQuery选择器
- HDU P1520 Anniversary party
- 共用体2
- JAVA线程池的使用
- Mybatis中使用Handler对字段进行统一处理
- Java绝对/相对路径获取与getResourceAsStream()方法
- java文本I/O(一)
- 基于springCloud的分布式架构体系
- 一个基于COM组件的Matlab与C#混合编程实例
- 【多线程】线程池