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

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;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

_Rikka_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值