题意:

给你n个节点,这n个节点构成了一颗以1为树根的树。每一个节点有一个初始值bi,从任意节点 i 的子树中选择任意k个节点,并按他的意愿随机排列这些节点中的数字,从而产生k⋅ai 的成本。对于一个节点i你需要将bi改成ci。

这个bi值和ci值的范围是[0,1]

题解:

对于一个节点,如果它的bi==ci,那么我们就不用管它(因为你改变它的值,那么肯定之后还要花费成本再改回来,增加了成本)。那么我们首先找出来一共有多少个节点bi!=ci ,然后总权值肯定是

num1*a1+num2*a2...+numn*an

numi表示从节点 i 的子树中选择任意numi个节点

那么肯定是尽可能在ai的值越小的子树上尽量增加numi的数量,这样总权值肯定最小

cnt0[i]:以i节点为树根的子树,在bi!=ci,bi等于0的数量

cnt1[i]:以i节点为树根的子树,在bi!=ci,bi等于1的数量

那么我们就从树根开始dfs,并且维护一个ai最小值,在dfs过程中记录一下i这个节点的所有子节点上bi!=ci的数量(如果bi!=ci,还要记录一下i这个点的bi值),如果ai等于我们维护的那个ai最小值,那就对这个节点

的2*min(cnt0[i],cnt1[i])个节点进行排序。然后让cnt1[i]和cnt0[i]都减去min(cnt0[i],cnt1[i])

最后判断一下cnt1[1]和cnt0[1]是否为0,等于0就代表所有节点bi都等于ci,否则输出-1

代码:

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string>
#include<queue>
#include<deque>
#include<string.h>
#include<map>
#include <iostream>
#include <math.h>
#define Mem(a,b) memset(a,b,sizeof(a))
const double II = acos(-1);
const double PP = (II*1.0)/(180.00);
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int maxn=2e5+10;
ll a[maxn],b[maxn],c[maxn],cnt0[maxn],cnt1[maxn],sum;
vector<ll>w[maxn];
void dfs(ll x,ll fa,ll minn)
{
minn=min(minn,a[x]);
for(ll i=0;i<w[x].size();++i)
{
ll now=w[x][i];
if(now!=fa)
{
dfs(now,x,minn);
cnt0[x]+=cnt0[now];
cnt1[x]+=cnt1[now];
}
}
if(b[x]!=c[x])
{
if(b[x])
cnt1[x]++;
else cnt0[x]++;
}
if(minn==a[x])
{
ll ans=min(cnt0[x],cnt1[x]);
ans*=2;
//printf("%d %d****\n",ans,a[x]);
sum=sum+ans*a[x];
ans/=2;
cnt0[x]-=ans;
cnt1[x]-=ans;
}
}
int main()
{
ll n;
sum=0;
scanf("%I64d",&n);
for(ll i=1;i<=n;++i)
{
scanf("%I64d%I64d%I64d",&a[i],&b[i],&c[i]);
}
for(ll i=1;i<n;++i)
{
ll u,v;
scanf("%I64d%I64d",&u,&v);
w[u].push_back(v);
w[v].push_back(u);
}
dfs(1,0,INF);
if(cnt1[1] || cnt0[1])
{
printf("-1\n");
}
else printf("%I64d\n",sum);
}

Codeforces Round #646 (Div. 2) E. Tree Shuffling dfs的更多相关文章

  1. Codeforces Round #646 (Div. 2) E. Tree Shuffling(树上dp)

    题目链接:https://codeforces.com/contest/1363/problem/E 题意 有一棵 $n$ 个结点,根为结点 $1$ 的树,每个结点有一个选取代价 $a_i$,当前 $ ...

  2. Codeforces Round #316 (Div. 2) D. Tree Requests dfs序

    D. Tree Requests time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  3. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  4. Codeforces Round #646 (Div. 2) 题解 (ABCDE)

    目录 A. Odd Selection B. Subsequence Hate C. Game On Leaves D. Guess The Maximums E. Tree Shuffling ht ...

  5. Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树

    题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...

  6. Codeforces Round #540 (Div. 3)--1118F1 - Tree Cutting (Easy Version)

    https://codeforces.com/contest/1118/problem/F1 #include<bits/stdc++.h> using namespace std; in ...

  7. Codeforces Round #353 (Div. 2) D. Tree Construction 模拟

    D. Tree Construction 题目连接: http://www.codeforces.com/contest/675/problem/D Description During the pr ...

  8. Codeforces Round #540 (Div. 3) F1. Tree Cutting (Easy Version) 【DFS】

    任意门:http://codeforces.com/contest/1118/problem/F1 F1. Tree Cutting (Easy Version) time limit per tes ...

  9. Codeforces Round #527 (Div. 3) F. Tree with Maximum Cost 【DFS换根 || 树形dp】

    传送门:http://codeforces.com/contest/1092/problem/F F. Tree with Maximum Cost time limit per test 2 sec ...

随机推荐

  1. 详解 TCP的三次握手四次挥手

    本文转载来自https://blog.csdn.net/qzcsu/article/details/72861891 背景描述 通过上一篇中网络模型中的IP层的介绍,我们知道网络层,可以实现两个主机之 ...

  2. P2979 [USACO10JAN]奶酪塔Cheese Towers(完全背包,递推)

    题目描述 Farmer John wants to save some blocks of his cows' delicious Wisconsin cheese varieties in his ...

  3. 超精讲-逐例分析CS:LAB2-Bomb!(上)

    0. 环境要求 关于环境已经在lab1里配置过了这里要记得安装gdb 安装命令 sudo yum install gdb 实验的下载地址 http://csapp.cs.cmu.edu/3e/labs ...

  4. RecyclerView 源码分析(二) —— 缓存机制

    在前一篇文章 RecyclerView 源码分析(一) -- 绘制流程解析 介绍了 RecyclerView 的绘制流程,RecyclerView 通过将绘制流程从 View 中抽取出来,放到 Lay ...

  5. QT串口助手(三):数据接收

    作者:zzssdd2 E-mail:zzssdd2@foxmail.com 一.前言 开发环境:Qt5.12.10 + MinGW 实现的功能 串口数据的接收 ascii字符形式显示与hex字符形式显 ...

  6. 【Python】中国有哪些同名的省市县?

    这道题适合写个脚本来解. 首先从百度地图API下载一份行政区划数据. 开发资源 | 百度地图API SDK 然后做一个简单的数据统计就可以啦~ 行政区划同一级同名的: import pandas as ...

  7. JavaScript中eval的替代方法

    引自:https://www.cnblogs.com/lxg0/p/7805266.html 通常我们在使用ajax获取到后台返回的json数据时,需要使用 eval 这个方法将json字符串转换成对 ...

  8. mysql忽略表中的某个字段不查询

    业务场景 1.表中字段较多 2.查询不需要表中某个字段的数据 语句如下: SELECT CONCAT(' select ',GROUP_CONCAT(COLUMN_NAME),' from ', TA ...

  9. 唯一ID生成算法剖析

    https://mp.weixin.qq.com/s/E3PGP6FDBFUcghYfpe6vsg 唯一ID生成算法剖析 原创 cloudoxou 腾讯技术工程 2019-10-08    

  10. UML 博客学习 后续继续完善

    http://blog.csdn.net/monkey_d_meng/article/details/6005764  http://blog.csdn.net/badobad/article/det ...