题目链接: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. spark机器学习笔记01

     1)外部数据源 val distFile1 = sc.textFile("data.txt") //本地当前目录下文件 val distFile2 =sc.textFile(& ...

  2. NodeJS遍历文件生产文件列表

    本文实例讲述了NodeJS遍历文件生产文件列表功能.分享给大家供大家参考,具体如下: 功能需求:在工作中我们可能经常需要知道项目中静态文件列表发布,一个一个去检索写,那就太苦逼了. 要想知道里面的文件 ...

  3. Redis Windows 安装

    摘自:https://www.cnblogs.com/M-LittleBird/p/5902850.html 一.下载windows版本的Redis 去官网找了很久,发现原来在官网上可以下载的wind ...

  4. GoldenGate 12.3 MA架构介绍系列(2) - 数据同步测试

    安装配置可参考上一篇:http://www.cnblogs.com/margiex/p/8071957.html 安装完成之后,会自动启动ServiceManager服务,此时,可以通过浏览器访问. ...

  5. 看起来像是break的考量

    代码运用了if+break: package day20181008; import java.util.Scanner; public class ZuoYe01 { public static v ...

  6. 12: xlrd 处理Excel文件

    1.1 xlrd处理.xlsx 文件 1.xlrd常用方法 #!/usr/bin/python # coding:utf-8 # 用xlrd读取Excel文件基本用法 import sys impor ...

  7. left join加上where条件的困惑

    eft join的困惑:一旦加上where条件,则显示的结果等于inner join将where 换成 and 用where 是先连接然后再筛选   用and 是先筛选再连接 数据库在通过连接两张或多 ...

  8. 20145320《网络对抗》逆向及Bof基础实践

    1 逆向及Bof基础实践说明 1.1 实践目标 本次实践的对象是一个名为pwn20145320的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的 ...

  9. VSCode 预览 .md 文件

    VSCode安装md插件 选择Extensions, 输入Markdown Theme Kit,下面会出现相应插件,点击install(截图中因为我已经安装,所以是设置) 或者安装插件Markdown ...

  10. noip 2013 提高组 day1

    1.转圈游戏: 解析部分略,快速幂就可以过 Code: #include<iostream> #include<fstream> using namespace std; if ...