zoukankan      html  css  js  c++  java
  • [不知道出自什么OJ]最大利润

    题目描述

    政府邀请了你在火车站开饭店,但不允许同时在两个相连接的火车站开。任意两个火车站有且只有一条路径,每个火车站最多有50个和它相连接的火车站。
    告诉你每个火车站的利润,问你可以获得的最大利润为多少。

    输入格式

    第一行输入整数N(N≤100000),表示有N个火车站,分别用1,2,... ,N来编号。
    接下来N行,每行一个整数(不超过10000)表示每个站点的利润。
    接下来N-1行描述火车站网络,每行两个整数,表示相连接的两个站点。

    输出格式

    输出一个整数表示可以获得的最大利润。

    题目分析:

    树形(dp)入门题 为了讲课码的

    (dp[i][0/1])表示第(i)个节点选/不选的最优解
    方程显然,懒了,见代码

    代码:

    #include<bits/stdc++.h>
    #define N (300000 + 10)
    using namespace std;
    inline void read(int &cnt) {
    	cnt = 0;
    	int f = 1; char c = getchar();
    	while (!isdigit(c)) {if (c == '-') f = -f; c = getchar();}
    	while (isdigit(c)) {cnt = (cnt << 3) + (cnt << 1) + (c ^ 48); c = getchar();}
    	cnt *= f;
    }
    int n, first[N], to[N], nxt[N], tot, a, b;
    int dp[N][2];
    void add(int x, int y) {nxt[++tot] = first[x], first[x] = tot, to[tot] = y;}
    
    void dfs_(int p, int fa) {
    	for (register int i = first[p]; i; i = nxt[i]) {
    		int v = to[i];
    		if (v == fa) continue;
    		dfs_(v, p);
    		dp[p][0] += max(dp[v][0], dp[v][1]);
    		dp[p][1] += dp[v][0];
    	}
    	
    }
    
    int main() {
    	read(n);
    	for (register int i = 1; i <= n; ++i) read(dp[i][1]);
    	for (register int i = 1; i < n; ++i) read(a), read(b), add(a, b), add(b, a);
    	dfs_(1, 0);
    	printf("%d", max(dp[1][0], dp[1][1]));
    	return 0;
    }
    
  • 相关阅读:
    在安卓上用Termux安装sqlmap
    地址转换函数
    字节操作函数
    主机字节序和网络字节序
    20191231 Spring官方文档(Core 1.13-1.14)
    20191230 Spring官方文档(Core 1.12)
    20191230 Tomcat权威指南-读书摘要系列【归档】
    20191230 On Java8【归档】
    20191227 Java8 日期时间API
    20191226 Spring官方文档(Core 1.11)
  • 原文地址:https://www.cnblogs.com/kma093/p/11621025.html
Copyright © 2011-2022 走看看