题目链接: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. B/S开发介绍

    b/s 的优势: 1.开发成本低 2.管理维护简单 3.产品升级便利 4.对用户的培训费用低 5.用户使用方便,出现故障的概率小 b/s 的不足: 1.安全性不足 2.客户端不能随心变化,受浏览器限制

  2. Python-OpenCV基本操作cv2

    1.图片加载.显示和保存 import cv2 # 生成图片 img = cv2.imread("1.jpg") # 生成灰色图片 imgGrey = cv2.imread(&qu ...

  3. Linux进程内存分析和内存泄漏定位

    在Linux产品开发过程中,通常需要注意系统内存使用量,和评估单一进程的内存使用情况,便于我们选取合适的机器配置,来部署我们的产品. Linux本身提供了一些工具方便我们达成这些需求,查看进程实时资源 ...

  4. 【题解】Luogu P1503 鬼子进村

    平衡树好题 原题传送门 这道题要用Splay,我博客里有对Splay的详细介绍 这道题思维有点难,要把被摧毁的节点插入平衡树,而不是把没有摧毁的节点插入 先把0和n+1插入平衡树,作为边界 操作1:摧 ...

  5. Tree Implementation with Python

    Tree Implementation with Python List of List 代码如下: def binary_tree(val): return [val, [], []] def in ...

  6. 非关系型数据库&&缓存

    面试其他篇 目录: 1.1

  7. 02: flask 使用举例

    1.1 项目说明   https://github.com/rickyyangrui/Flask_web_demo1 1.项目文件结构 2.项目主文件 cssmin==0.2.0 Flask==0.1 ...

  8. 01:adminLTE2基本使用

    1.1 adminLTE介绍 1.adminLTE 介绍 1.基于Bootstrap3高度可定制的响应式管理模板,免去前端架构师大量的js和css的编写 2.adminLTE除了可以使用bootstr ...

  9. linux /Android 平台下使用 i2c-tools

    下载源码将 i2c-tools 代码下载到 Android 源码的 external 目录下 在 i2c-tools 目录下新建 Android.mk 文件,内容如下: # external/i2c- ...

  10. Windows 登录用户的类型

    转载:http://blog.csdn.net/chenlycly/article/details/45419259 1.登录的是超级管理员Administrator 默认情况下,超级管理员Admin ...