题目链接: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. C++11 正则表达式简单运用

    正则表达式(regular expression)是计算机科学中的一个概念,又称规则表达式,通常简写为regex.regexp.RE.regexps.regexes.regexen. 正则表达式是一种 ...

  2. 关于springMVC 传递 对象参数的问题

    1.前端请求必须是 post 2.前端数据data必须做 json字符串处理  JSON.stringify(data) 3. contentType: 'application/json', 4.@ ...

  3. 每日linux命令学习-grep模式检索

    grep模式检索指令包括grep,egrep,和fgrep,.Linux系统使用正则表达式优化文本检索,所以在此,笔者首先学习了一下正则表达式. 1. 正则表达式 正则表达式使用被称为元字符(Meta ...

  4. 抓取awr、语句级awr、ashrpt

    exec dbms_workload_repository.create_snapshot();--调用MMON进程立即收集快照 生成AWR报告@?/rdbms/admin/awrrpt.sql; 9 ...

  5. 实现 AD 采样,使用 LCD1602 显示 AD 数值

    实现 AD 采样,使用 LCD1602 显示 AD 数值 写在前面 单片机内集成的A/D转换,一般都有相应的特殊功能寄存器来设置A/D的使能标志,参考电压,转换频率,通道选择,A/D输入口的属性(模拟 ...

  6. ubuntu----VMware 鼠标自由切换问题及主机虚拟机共享剪切板问题

    VMware 安装了Ubuntu之后,在正常安装了VMware tools后,仍然不能正常的在Ubuntu与物理机之间自由的切换,每次都要按下ctrl+Alt,而且鼠标指针会经常性的离奇的失灵 解决方 ...

  7. 牛客网校招全国统一模拟笔试(二月场)- Java方向

    1.请问下面关于与wait()一起使用的方法notify()的各种陈述,哪个正确? A 多个线程同时等待某个条件,则只有等待时间最长的哪个线程被通知 B 多个线程同时等待某条件,则没有办法预测哪个线程 ...

  8. linux内核中的dquot是什么?

    答:这个与磁盘配额管理(disk quota)有关,内核配置选项为CONFIG_QUOTA,使能此选项意味着可以设置每个用户的硬盘使用限制.

  9. 【Finchley】【新特性】Spring Cloud Finchley 新特性

    Finchley 正式版的发布貌似经历了相当长的时间,这次的重大发布主要带来了以下 4 项重大更新. 重大更新 1.新增 Spring Cloud Gateway 组件 Spring Cloud Ga ...

  10. js选择排序

    选择排序 平均时间复杂度O(n*n) 最好情况O(n*n) 最差情况O(n*n) 空间复杂度O(1) 稳定性:不稳定 function chooseSort (arr) { var temp; var ...