题目链接:http://codeforces.com/contest/767/problem/C


问能否将一棵带点权的书分成点权$3$块,求任意方案。

其实考虑一棵以$x$为根的子树权值为${\frac{1}{3}\sum val[i]}$之后它就一定要作为单独的一块了,那么DFS一遍即可。


 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<cstring>
using namespace std;
#define maxn 1000010
#define llg long long
#define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
llg n,m,ans[maxn],tot,val[maxn],size[maxn],sum,dad[maxn],root;
vector<llg>a[maxn]; void dfs(llg x,llg fa)
{
if (tot==) return ;
llg w=a[x].size(),v;
size[x]=val[x];
for (llg i=;i<w;i++)
{
if (tot==) return ;
v=a[x][i];
if (v==fa) continue;
if (tot==) return ;
dfs(v,x);
if (tot==) return ;
size[x]+=size[v];
}
if (size[x]==sum) {ans[++tot]=x; size[x]=;}
if (tot==) return ;
} void init()
{
cin>>n;
for (llg i=;i<=n;i++)
{
scanf("%I64d%I64d",&dad[i],&val[i]);
if (dad[i]==) root=i;
a[dad[i]].push_back(i);
sum+=val[i];
}
} int main()
{
// yyj("C");
init();
if (sum%!=) {cout<<-; return ;}
sum/=;
dfs(root,);
sort(ans+,ans+tot+);
for (llg i=;i<=n;i++)
if (ans[i]==root)
{
cout<<-;
return ;
}
if (tot>=) {for (llg i=;i<=tot;i++) cout<<ans[i]<<" "; }else cout<<-;
return ;
}

codeforces 767C - Garland的更多相关文章

  1. CodeForces - 767C Garland 树的遍历

    C. Garland time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  2. 【codeforces 767C】Garland

    [题目链接]:http://codeforces.com/contest/767/problem/C [题意] 一棵树; 树上的每个节点都有一个权值; 让你把一棵树切掉两条边; 然后把这棵树分成了3个 ...

  3. Codeforces#398 &767C. Garland 树形求子节点的和

    传送门 题意:在一个树上,问能否切两刀,使得三块的节点值的和相同. 思路: 由于这个总的节点和是不变的,每块的节点值和sum固定,dfs搜索,和等于sum/3,切.若不能分成三块(不能被3整除,-1) ...

  4. CodeForces - 767C

    花了6个小时,终于成功ac...... 两边dfs,第一遍求子树和,第二遍判断有没有2*t[s]/3和t[s]/3,因为要求的节点可能是在同一条线上,同时要有2*t[s]/3和t[s]/3的情况,且2 ...

  5. Codeforces 1108D - Diverse Garland - [简单DP]

    题目链接:http://codeforces.com/problemset/problem/1108/D time limit per test 1 secondmemory limit per te ...

  6. 【codeforces 758B】Blown Garland

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. C. Nice Garland Codeforces Round #535 (Div. 3) 思维题

    C. Nice Garland time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  8. D. Diverse Garland Codeforces Round #535 (Div. 3) 暴力枚举+贪心

    D. Diverse Garland time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  9. Diverse Garland CodeForces - 1108D (贪心+暴力枚举)

    You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ...

随机推荐

  1. 算法提高 P0101

    一个水分子的质量是3.0*10-23克,一夸脱水的质量是950克.写一个程序输入水的夸脱数n(0 <= n <= 1e10),然后输出水分子的总数.输入 109.43输出 3.465283 ...

  2. GMIS 2017 大会陈雨强演讲:机器学习模型,宽与深的大战

    https://blog.csdn.net/starzhou/article/details/72819374 2017-05-27 19:15:36     GMIS 2017    10 0 5 ...

  3. PID控制器开发笔记(转)

    源: PID控制器开发笔记

  4. php 腾讯云 对象存储V5版本 获取返回的上传文件的链接方法

    腾讯云 对象存储V5版本 文档地址:https://github.com/tencentyun/cos-php-sdk-v5 调用简单文件上传方法: 返回数据如下 Array ( [data:prot ...

  5. 错误 1 error LNK2019: 无法解析的外部符号 __imp__pthread_create,该符号在函数 _main 中被引用 解决方法

    晚上花几分钟在windows下测了下pthread的用法,出现错误 1 error LNK2019: 无法解析的外部符号 __imp__pthread_create,该符号在函数 _main 中被引用 ...

  6. Angular 快速入门

    Angular 快速入门 AngularJS 官方网址 Angular:https://www.angular.cn/ Angular官网:https://angularjs.org/ Angular ...

  7. [c/c++] programming之路(10)、格式符后续

    一.格式符 1. f格式符 #include<stdio.h> #include<stdlib.h> void main(){ printf("%f",10 ...

  8. hdu 1151 Air Raid - 二分匹配

    Consider a town where all the streets are one-way and each street leads from one intersection to ano ...

  9. Install Virtualbox on ubuntu

    1.Use the command: sudo apt-get install virtualbox

  10. uniGUI试用笔记(十)

    今天用LoadRunner对uniGUI的Standalone模式的程序进行了一次压力测试,程序采用三层模式,将应用服务器与Web服务器分离,由于条件限制,数据库.应用服务和Web服务都部署在同一条云 ...