1098A - Sum in the tree(贪心、树)

本文介绍了一种使用树形动态规划方法解决特定数值恢复问题的算法。问题背景为在一棵根节点编号从1到n的树中,每个节点上初始写有一个非负整数。目标是从根节点到每个节点的路径上的数值之和中恢复这些数值,特别是当部分节点的数值被意外删除后。通过DFS遍历和优化策略,文章详细解释了如何找到一种恢复方案,使得所有节点数值之和最小。

A. Sum in the tree
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Mitya has a rooted tree with n
vertices indexed from 1 to n, where the root has index 1. Each vertex v initially had an integer number av≥0 written on it. For every vertex v Mitya has computed sv: the sum of all values written on the vertices on the path from vertex v to the root, as well as hv — the depth of vertex v, which denotes the number of vertices on the path from vertex v to the root. Clearly, s1=a1 and h1=1

.

Then Mitya erased all numbers av
, and by accident he also erased all values sv for vertices with even depth (vertices with even hv). Your task is to restore the values av for every vertex, or determine that Mitya made a mistake. In case there are multiple ways to restore the values, you’re required to find one which minimizes the total sum of values av

for all vertices in the tree.
Input

The first line contains one integer n
— the number of vertices in the tree (2≤n≤105). The following line contains integers p2, p3, … pn, where pi stands for the parent of vertex with index i in the tree (1≤pi<i). The last line contains integer values s1, s2, …, sn (−1≤sv≤109), where erased values are replaced by −1

.
Output

Output one integer — the minimum total sum of all values av
in the original tree, or −1 if such tree does not exist.

思路:给出了我们Si,要我们求出使所有Vi的和最小的sum,想一想就能明白,让一个Vi尽可能的去给更多的Si做贡献,即可使总的sum最小,故对每个-1的Si dfs求出其孩子中的最小Si(因为要求每个子Si总会大于父Si),最后判断如果有子Si<父Si,则输出-1。

Code:

#include<iostream>
#include<string>
#include<map>
#include<algorithm>
#include<memory.h>
#include<cmath>
#include<queue>
#include<sstream>
#define pii pair<int,int>
#define FAST ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
typedef long long ll;
const int Max = 2e5 + 5;
int lst[Max];

int u[Max], v[Max], w[Max];
int first[Max], nex[Max], f[Max];
int g = 0;
ll s[Max];

void add(int a, int b)
{
	u[++g] = a;
	v[g] = b;
	nex[g] = first[a];
	first[a] = g;
}



ll dfs(int n)
{
	if (s[n]!=-1)
	{
		//v[n] = s[n] - s[f[n]];
		return s[n];
	}
	else
	{
		int k = first[n];ll mi = 1e9+5;
		while (k != -1)
		{
			mi = min(dfs(v[k]), mi);
			k = nex[k];
		}
		s[n] = mi;
		return s[n];
	}
}

int main()
{
	FAST;
	int n;cin >> n;
	memset(first, -1, sizeof(first));
	for (int i = 2;i <= n;i++)
	{
		int t;cin >> t;
		add(t, i);f[i] = t;
	}
	for (int i = 1;i <= n;i++)cin >> s[i];
	ll ans = 0;int flag = 0;
	for (int i = 1;i <= n;i++)
	{
		dfs(i);dfs(f[i]);
		if (dfs(i) == 1e9+5)ans += 0;//后面Si全是-1则令该点权为0
		else
		{
			if (dfs(f[i]) > dfs(i))flag = 1;
			ans += (dfs(i) - dfs(f[i]));
		}
	}
	if (flag)cout << -1 << endl;
	else cout << ans << endl;

}
最小生成问题可以使用贪心算法来解决,其中Prim算法和Kruskal算法是两种常见的贪心算法。 下面以Prim算法为例,给出C语言的实现: ```c #include <stdio.h> #include <stdbool.h> #define INFINITY 1000000 int n; // 顶点数 int graph[100][100]; // 图的邻接矩阵 int lowcost[100]; // 存储当前点到已选点集的最小边权值 bool visited[100]; // 标记点是否已被选中 // Prim算法求最小生成 void Prim(int v0) { int i, j, k; int min, sum = 0; // 初始化 for (i = 0; i < n; i++) { lowcost[i] = graph[v0][i]; visited[i] = false; } visited[v0] = true; // 每次找到一个未被选中的点集中到已选点集最小边 for (i = 1; i < n; i++) { min = INFINITY; k = v0; for (j = 0; j < n; j++) { if (!visited[j] && lowcost[j] < min) { min = lowcost[j]; k = j; } } visited[k] = true; sum += min; for (j = 0; j < n; j++) { if (!visited[j] && graph[k][j] < lowcost[j]) { lowcost[j] = graph[k][j]; } } } printf("The weight of the minimum spanning tree is: %d\n", sum); } int main() { int i, j, v0; // 输入图的顶点数和邻接矩阵 printf("Please input the number of vertices: "); scanf("%d", &n); printf("Please input the adjacency matrix: \n"); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { scanf("%d", &graph[i][j]); } } // 输入起始点 printf("Please input the starting vertex: "); scanf("%d", &v0); // 求最小生成 Prim(v0); return 0; } ``` 以上代码使用了邻接矩阵来表示图,时间复杂度为O(n^2)。如果使用邻接表来表示图,则时间复杂度可以优化到O(nlogn)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_Rikka_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值