博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【HDOJ】1520 Anniversary party
阅读量:7191 次
发布时间:2019-06-29

本文共 1463 字,大约阅读时间需要 4 分钟。

第二道树形DP,先是MLE。后来仅需改小邻接矩阵的第二个维度到30就过了。

1 #include 
2 #include
3 #include
4 5 #define MAXN 6005 6 7 int rat[MAXN]; 8 int dp[MAXN][2]; 9 int adj[MAXN][30];10 bool visit[MAXN];11 int n;12 13 int max(int a, int b) {14 return a>b ? a:b;15 }16 17 void dfs(int r) {18 int i, v;19 20 visit[r] = true;21 dp[r][1] = rat[r];22 for (i=1; i<=adj[r][0]; ++i) {23 v = adj[r][i];24 if (visit[v])25 continue;26 dfs(v);27 // r node not attend, v get the maximum28 dp[r][0] += max(dp[v][0], dp[v][1]);29 // r node attend, v not attend.30 dp[r][1] += dp[v][0];31 }32 }33 34 int main() {35 int i, j, k;36 int a, b;37 38 #ifndef ONLINE_JUDGE39 freopen("data.in", "r", stdin);40 #endif41 42 while (scanf("%d", &n) != EOF) {43 for (i=1; i<=n; ++i)44 scanf("%d", &rat[i]);45 46 memset(adj, 0, sizeof(adj));47 memset(visit, false, sizeof(visit));48 memset(dp, 0, sizeof(dp));49 while (scanf("%d %d", &a, &b)!=EOF && (a||b)) {50 ++adj[a][0];51 adj[a][adj[a][0]] = b;52 ++adj[b][0];53 adj[b][adj[b][0]] = a;54 }55 56 dfs(1);57 printf("%d\n", max(dp[1][0], dp[1][1]));58 }59 60 return 0;61 }

 

转载于:https://www.cnblogs.com/bombe1013/p/4169298.html

你可能感兴趣的文章
apache配置文件参数优化
查看>>
Manifest.xml中不要出现重复的uses-permission声明
查看>>
UFS文件系统简明学习笔记
查看>>
详解Redis 的持久化机制--RDB和AOF
查看>>
就算神游 之四:富士山和富士游乐园 9
查看>>
linux 学习 12 服务管理
查看>>
maven全局配置文件settings.xml详解
查看>>
模型图纸数据库设计
查看>>
Two classes have the same XML type name 排错【转】
查看>>
linux笔记:linux常用命令-关机重启命令
查看>>
想要提高网页转换率?试试这16个UI秘诀
查看>>
转)VCSA 6.5重启无法访问,报错“503 Service Unavailable”的解决方法
查看>>
Configuring and troubleshooting a Schema Provider
查看>>
Windows环境安装MySQL数据库
查看>>
javascript函数以及作用域简介
查看>>
Windows Phone 编程中页面间传值方法 - [WP开发]
查看>>
apollo实现c#与android消息推送(四)
查看>>
Spring 上下文操作工具类 ContextUtils
查看>>
程序员的智囊库系列之3--分布式文件系统(Distributed file systems)
查看>>
工具推荐|程序员必须知道的11款新型编程工具
查看>>