算法训练 结点选择
2017-02-17 16:38
211 查看
有一棵 n 个节点的树,树上每个节点都有一个正整数权值。如果一个点被选择了,那么在树上和它相邻的点都不能被选择。求选出的点的权值和最大是多少?
输入格式
第一行包含一个整数 n 。
接下来的一行包含 n 个正整数,第 i 个正整数代表点 i 的权值。
接下来一共 n-1 行,每行描述树上的一条边。
输出格式
输出一个整数,代表选出的点的权值和的最大值。
样例输入
5
1 2 3 4 5
1 2
1 3
2 4
2 5
样例输出
12
样例说明
选择3、4、5号点,权值和为 3+4+5 = 12 。
数据规模与约定
对于20%的数据, n <= 20。
对于50%的数据, n <= 1000。
对于100%的数据, n <= 100000。
权值均为不超过1000的正整数
第一次运行超时
import java.util.Scanner;
import java.util.Vector;
public class Main {
static class Node{
int val;
boolean visit = false;
int dp[] = new int[2];
Vector<Integer> vector = new Vector<Integer>();
}
static Node node[];
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
int N = input.nextInt();
node = new Node[N+1];
for (int i = 1; i < node.length; i++) {
int value = input.nextInt();
node[i] = new Node();
node[i].val = value;
node[i].dp[1] = value;
node[i].dp[0] = 0;
}
for (int i = 2; i < node.length; i++) {
int m = input.nextInt();
int n = input.nextInt();
node[m].vector.add(n);
node
.vector.add(m);
}
dfs(1);
int ans = Math.max(node[1].dp[0], node[1].dp[1]);
System.out.println(ans);
}
private static void dfs(int num) {
node[num].visit = true;
int sum = node[num].vector.size();
for (int i = 0; i < sum; i++) {
int son = node[num].vector.get(i);
if (node[son].visit==false) {
dfs(son);
node[num].dp[0]+=Math.max(node[son].dp[1], node[son].dp[0]);
if (node[son].dp[0]>0) {
node[num].dp[1] += node[son].dp[0];
}
}
}
}
}
输入格式
第一行包含一个整数 n 。
接下来的一行包含 n 个正整数,第 i 个正整数代表点 i 的权值。
接下来一共 n-1 行,每行描述树上的一条边。
输出格式
输出一个整数,代表选出的点的权值和的最大值。
样例输入
5
1 2 3 4 5
1 2
1 3
2 4
2 5
样例输出
12
样例说明
选择3、4、5号点,权值和为 3+4+5 = 12 。
数据规模与约定
对于20%的数据, n <= 20。
对于50%的数据, n <= 1000。
对于100%的数据, n <= 100000。
权值均为不超过1000的正整数
第一次运行超时
import java.util.Scanner;
import java.util.Vector;
public class Main {
static class Node{
int val;
boolean visit = false;
int dp[] = new int[2];
Vector<Integer> vector = new Vector<Integer>();
}
static Node node[];
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
int N = input.nextInt();
node = new Node[N+1];
for (int i = 1; i < node.length; i++) {
int value = input.nextInt();
node[i] = new Node();
node[i].val = value;
node[i].dp[1] = value;
node[i].dp[0] = 0;
}
for (int i = 2; i < node.length; i++) {
int m = input.nextInt();
int n = input.nextInt();
node[m].vector.add(n);
node
.vector.add(m);
}
dfs(1);
int ans = Math.max(node[1].dp[0], node[1].dp[1]);
System.out.println(ans);
}
private static void dfs(int num) {
node[num].visit = true;
int sum = node[num].vector.size();
for (int i = 0; i < sum; i++) {
int son = node[num].vector.get(i);
if (node[son].visit==false) {
dfs(son);
node[num].dp[0]+=Math.max(node[son].dp[1], node[son].dp[0]);
if (node[son].dp[0]>0) {
node[num].dp[1] += node[son].dp[0];
}
}
}
}
}
相关文章推荐
- 蓝桥杯 算法训练 结点选择 (树形动态规划)
- 算法训练:结点选择
- 蓝桥杯,算法训练 结点选择(简箪的树形DP)
- 算法训练 结点选择 【树形dp】
- 算法训练 结点选择
- 蓝桥杯 算法训练 结点选择 树形动态规划
- 算法训练 结点选择 (树形dp)(点权)
- 蓝桥杯 算法训练-结点选择
- 算法训练 结点选择
- 蓝桥杯 算法训练 结点选择 DP(SDNU 1482.结点选择)
- 算法训练 结点选择
- 算法训练 结点选择
- 蓝桥杯 算法训练 结点选择
- 蓝桥杯 算法训练 结点选择 (树形动态规划)
- 算法训练 结点选择【树形动态规划】
- 算法训练 结点选择 (树形DP)
- 算法训练 结点选择
- 算法训练 结点选择 树dp
- 蓝桥杯 ALGO-4 算法训练 结点选择
- 算法训练 结点选择 蓝桥杯