C. Garland
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Once at New Year Dima had a dream in which he was presented a fairy garland. A garland is a set of lamps, some pairs of which are connected by wires. Dima remembered that each two lamps in the garland were connected directly or indirectly via some wires. Furthermore, the number of wires was exactly one less than the number of lamps.

There was something unusual about the garland. Each lamp had its own brightness which depended on the temperature of the lamp. Temperatures could be positive, negative or zero. Dima has two friends, so he decided to share the garland with them. He wants to cut two different wires so that the garland breaks up into three parts. Each part of the garland should shine equally, i. e. the sums of lamps' temperatures should be equal in each of the parts. Of course, each of the parts should be non-empty, i. e. each part should contain at least one lamp.

Help Dima to find a suitable way to cut the garland, or determine that this is impossible.

While examining the garland, Dima lifted it up holding by one of the lamps. Thus, each of the lamps, except the one he is holding by, is now hanging on some wire. So, you should print two lamp ids as the answer which denote that Dima should cut the wires these lamps are hanging on. Of course, the lamp Dima is holding the garland by can't be included in the answer.

Input

The first line contains single integer n (3 ≤ n ≤ 106) — the number of lamps in the garland.

Then n lines follow. The i-th of them contain the information about the i-th lamp: the number lamp ai, it is hanging on (and 0, if is there is no such lamp), and its temperature ti ( - 100 ≤ ti ≤ 100). The lamps are numbered from 1 to n.

Output

If there is no solution, print -1.

Otherwise print two integers — the indexes of the lamps which mean Dima should cut the wires they are hanging on. If there are multiple answers, print any of them.

Examples
input
6
2 4
0 5
4 2
2 1
1 1
4 2
output
1 4
input
6
2 4
0 6
4 2
2 1
1 1
4 2
output
-1
Note

The garland and cuts scheme for the first example:

树的后序遍历,根节点的值为子树权值和。当和为sum/3时,节点值赋0,返给父节点(相当于剪掉)。当找到两个非根结点的sum/3权值和时,即为答案。注意必须是最先找到的前两个,虽然判断了不是根节点,但是会WA在第九点...

#include<stdio.h>
#include<vector>
using namespace std; int b[],w[],s[];
vector<int> v[];
int root,co,c1,c2; int dfs(int f)
{
int i;
for(i=;i<v[f].size();i++){
if(b[v[f][i]]==){
b[v[f][i]]=;
s[f]+=dfs(v[f][i]);
}
}
s[f]+=w[f];
if(s[f]==co/&&f!=root){
if(c1==) c1=f;
else if(c2==) c2=f; //不加c2==0WA。。
return ;
}
return s[f];
} int main()
{
int n,x,y,i;
scanf("%d",&n);
co=;
for(i=;i<=n;i++){
scanf("%d%d",&x,&y);
if(x==) root=i;
else{
v[i].push_back(x);
v[x].push_back(i);
}
w[i]=y;
co+=y;
}
if(co%) printf("-1\n");
else{
c1=;c2=;
b[root]=;
dfs(root);
if(c1!=&&c2!=&&c1!=c2) printf("%d %d\n",c1,c2);
else printf("-1\n");
}
return ;
}

CodeForces - 767C Garland 树的遍历的更多相关文章

  1. codeforces 767C - Garland

    题目链接:http://codeforces.com/contest/767/problem/C 问能否将一棵带点权的书分成点权$3$块,求任意方案. 其实考虑一棵以$x$为根的子树权值为${\fra ...

  2. 数据结构--树(遍历,红黑,B树)

    平时接触树还比较少,写一篇博文来积累一下树的相关知识. 很早之前在数据结构里面学的树的遍历. 前序遍历:根节点->左子树->右子树 中序遍历:左子树->根节点->右子树 后序遍 ...

  3. YTU 3023: 树的遍历

    原文链接:https://www.dreamwings.cn/ytu3023/2617.html 3023: 树的遍历 时间限制: 1 Sec  内存限制: 128 MB 提交: 3  解决: 2 题 ...

  4. 团体程序设计天梯赛-练习集L2-006. 树的遍历

    L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...

  5. leetcode404-----简单的树的遍历

    Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...

  6. pat L2-006. 树的遍历

    L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...

  7. L2-006. 树的遍历

    题目链接:L2-006. 树的遍历 今天一神给我手敲二叉树模板,瞬间就领悟了,感觉自己萌萌哒! 看上去很直观! #include <iostream> #include <cstdi ...

  8. js实现对树深度优先遍历与广度优先遍历

    深度优先与广度优先的定义 首先我们先要知道什么是深度优先什么是广度优先. 深度优先遍历是指从某个顶点出发,首先访问这个顶点,然后找出刚访问这个结点的第一个未被访问的邻结点,然后再以此邻结点为顶点,继续 ...

  9. L2-006 树的遍历 (25 分) (根据后序遍历与中序遍历建二叉树)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 L2-006 树的遍历 (25 分 ...

随机推荐

  1. git学习(4)---工作流

    一.目的 前三章介绍了git工具本身的操作,主要包含本地仓库操作和远程库操作两部分内容.接下来,我们将介绍怎样使用git进行项目开发,也叫做git工作流. git工作流分为三种模式:共享远程库模式.独 ...

  2. ipython notebook 如何打开.ipynb文件?

    标签: pythontensorflow 2017-03-29 14:17 235人阅读 评论(0) 收藏 举报  分类: TensorFlow(13)  转自:https://www.zhihu.c ...

  3. Android Material Design 中文版

    http://www.google.com/design/spec/animation/authentic-motion.html http://www.oschina.net/question/14 ...

  4. SDWebImage学习

    SDWebImage学习 SDWebImage版本是:'4.2.2' SDWebImage是iOS开发中常用的图片加载的库,能下载并缓存图片.这次就着重介绍SDWebImage的特色功能:下载与缓存. ...

  5. The Apache Thrift API client/server architecture

    http://thrift.apache.org/ The Apache Thrift software framework, for scalable cross-language services ...

  6. JAVA工厂方法模式(Factory Method)

    1.普通工厂模式 普通工厂模式:就是建立一个工厂类,对实现了同一接口的一些类进行实例的创建. 1-1.建立Sender接口 public interface Sender { public void ...

  7. HTML初级教程

    1:标题h1~h6HTML标签有专门的标签处理你页面上的标题,它们是h1,h2,h3,h4,h5和h6,象封建社会一样,h1就是万能的君主而h6就是最底层的百姓. 注意,h1标签在一个页面只能使用一次 ...

  8. Linux_基于Docker快速搭建个人博客网站

    时间:2017年04月28日星期五 说明:基于docker技术,使用jpress开源框架搭建个人博客网站.特别感谢jpress开源项目.系统版本:CentOS 7.2-64bit. 步骤一:准备Doc ...

  9. ACM应该学什么(知乎学长)

    网络上流传的答案有很多,估计提问者也曾经去网上搜过.所以根据自己微薄的经验提点看法. 我ACM初期是训练编码能力,以水题为主(就是没有任何算法,自己靠动脑筋能够实现的),这种题目特点是麻烦,但是不难, ...

  10. hdu-5673 Robot(默次金数)

    题目链接: Robot Time Limit: 12000/6000 MS (Java/Others)  Memory Limit: 65536/65536 K (Java/Others) 问题描述 ...