CodeForces - 767C Garland 树的遍历
2 seconds
256 megabytes
standard input
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.
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.
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.
6
2 4
0 5
4 2
2 1
1 1
4 2
1 4
6
2 4
0 6
4 2
2 1
1 1
4 2
-1
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 树的遍历的更多相关文章
- codeforces 767C - Garland
题目链接:http://codeforces.com/contest/767/problem/C 问能否将一棵带点权的书分成点权$3$块,求任意方案. 其实考虑一棵以$x$为根的子树权值为${\fra ...
- 数据结构--树(遍历,红黑,B树)
平时接触树还比较少,写一篇博文来积累一下树的相关知识. 很早之前在数据结构里面学的树的遍历. 前序遍历:根节点->左子树->右子树 中序遍历:左子树->根节点->右子树 后序遍 ...
- YTU 3023: 树的遍历
原文链接:https://www.dreamwings.cn/ytu3023/2617.html 3023: 树的遍历 时间限制: 1 Sec 内存限制: 128 MB 提交: 3 解决: 2 题 ...
- 团体程序设计天梯赛-练习集L2-006. 树的遍历
L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...
- leetcode404-----简单的树的遍历
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...
- pat L2-006. 树的遍历
L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...
- L2-006. 树的遍历
题目链接:L2-006. 树的遍历 今天一神给我手敲二叉树模板,瞬间就领悟了,感觉自己萌萌哒! 看上去很直观! #include <iostream> #include <cstdi ...
- js实现对树深度优先遍历与广度优先遍历
深度优先与广度优先的定义 首先我们先要知道什么是深度优先什么是广度优先. 深度优先遍历是指从某个顶点出发,首先访问这个顶点,然后找出刚访问这个结点的第一个未被访问的邻结点,然后再以此邻结点为顶点,继续 ...
- L2-006 树的遍历 (25 分) (根据后序遍历与中序遍历建二叉树)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 L2-006 树的遍历 (25 分 ...
随机推荐
- CCBPM中的消息机制,CCIM服务端安装说明
1.改动LeeIMService.exe.config服务端配置文件: 2.client的配置就比較简单了,仅仅须要在"SercerIP"节点上写上server的IP: 3.然后要 ...
- 【BZOJ4800】[Ceoi2015]Ice Hockey World Championship Meet in the Middle
[BZOJ4800][Ceoi2015]Ice Hockey World Championship Description 有n个物品,m块钱,给定每个物品的价格,求买物品的方案数. Input 第一 ...
- LeetCode算法题目解答汇总(转自四火的唠叨)
LeetCode算法题目解答汇总 本文转自<四火的唠叨> 只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少.我的初衷就是练习, ...
- Transforming Auto-encoders
http://www.cs.toronto.edu/~hinton/absps/transauto6.pdf The artificial neural networks that are used ...
- A桶中有多少水?
如果你能算出桶中有多少水,我便许你下山去玩.有一天,老和尚让小和尚将A桶的水挑到B桶去,可是小和尚却想下山玩,不愿意挑水,老和尚便说:”如果你能够根据我的提示算出A桶中有多少升水,我便许你下山去玩.” ...
- 6.JS输出
JavaScript 通常用于操作 HTML 元素. ① document.getElementById(id),可以访问某个 HTML 元素 请使用 "id" 属性来标识 HTM ...
- spring IOC(转)
原文 http://stamen.iteye.com/blog/1489223 引述:IoC(控制反转:Inverse of Control)是Spring容器的内核,AOP.声明式事务等功能在此基础 ...
- Label标签 自动触发onclick,点击内部的Input
最近项目遇到了一个bug,点击外层元素会直接触发元素内部的input框.(外层元素用的是label包裹的).找了很久才发现是label标签造成的. label定义和用法: label 标签为 inpu ...
- ICE学习笔记 -- RFC 5245
RFC 5245 ICE 1, offer/answer model 2, ICE Step: 1) 产生候选地址(1.公网 2.NAT反射 3.Relay转发地址) Generate ca ...
- [转]FPGA入门——basys2开发板的伪随机gold码的生成
本文原创,转载请注明出处:http://www.cnblogs.com/risten/p/4166169.html 1.系统原理 通过频率控制字选择相位步进,产生访问ROM的地址,进而控制DAC的输出 ...