设sum是所有灯泡的亮度之和

有两种情况:

一种是存在结点U和V,U是V的祖先,并且U的子树权值和为sum/3*2,且U不是根,且V的子树权值和为sum/3。

另一种是存在结点U和V,他们之间没有祖先关系,两者的子树权值和都是sum/3。(已经出栈的结点和当前访问的结点之间,必然没有祖先关系)

两次dfs解决。

#include<cstdio>
#include<algorithm>
#include<cstdlib>
using namespace std;
int n,a[1000010],fa[1000010],b[1000010];
int v[1000010],next[1000010],first[1000010],e,root,sum;
void AddEdge(int U,int V)
{
v[++e]=V;
next[e]=first[U];
first[U]=e;
}
void dfs(int U)
{
b[U]=a[U];
for(int i=first[U];i;i=next[i])
{
dfs(v[i]);
b[U]+=b[v[i]];
}
}
void df2(int U,int ance)
{
if(ance && b[U]==sum/3)
{
printf("%d %d\n",min(ance,U),max(ance,U));
exit(0);
}
for(int i=first[U];i;i=next[i])
if(ance)
df2(v[i],ance);
else if(U!=root && b[U]==sum/3*2)
df2(v[i],U);
else
df2(v[i],0);
}
int other;
void df3(int U)
{
if(b[U]==sum/3 && other)
{
printf("%d %d\n",min(other,U),max(other,U));
exit(0);
}
for(int i=first[U];i;i=next[i])
df3(v[i]);
if(U!=root && b[U]==sum/3)
other=U;
}
int main()
{
// freopen("c.in","r",stdin);
int x;
scanf("%d",&n);
for(int i=1;i<=n;++i)
{
scanf("%d%d",&x,&a[i]);
sum+=a[i];
if(x)
{
fa[i]=x;
AddEdge(x,i);
}
else
root=i;
}
if(sum%3!=0)
{
puts("-1");
return 0;
}
dfs(root);
df2(root,0);
df3(root);
puts("-1");
return 0;
}

【DFS】Codeforces Round #398 (Div. 2) C. Garland的更多相关文章

  1. 【DFS】Codeforces Round #402 (Div. 2) B. Weird Rounding

    暴搜 #include<cstdio> #include<algorithm> using namespace std; int n,K,Div=1,a[21],m,ans=1 ...

  2. 【推导】【DFS】Codeforces Round #429 (Div. 1) B. Leha and another game about graph

    题意:给你一张图,给你每个点的权值,要么是-1,要么是1,要么是0.如果是-1就不用管,否则就要删除图中的某些边,使得该点的度数 mod 2等于该点的权值.让你输出一个留边的方案. 首先如果图内有-1 ...

  3. 【贪心】【DFS】Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) C. Andryusha and Colored Balloons

    从任意点出发,贪心染色即可. #include<cstdio> #include<algorithm> using namespace std; int v[200010< ...

  4. 【枚举】【贪心】 Codeforces Round #398 (Div. 2) B. The Queue

    卡题意……妈的智障 一个人的服务时间完整包含在整个工作时间以内. 显然,如果有空档的时间,并且能再下班之前完结,那么直接输出即可,显然取最左侧的空档最优. 如果没有的话,就要考虑“挤掉”某个人,就是在 ...

  5. 【暴力】Codeforces Round #398 (Div. 2) A. Snacktower

    题意不复述. 用个bool数组记录一下,如果某一天,当前剩下的最大的出现了的话,就输出一段. #include<cstdio> using namespace std; int n; bo ...

  6. 【题解】Codeforces Round #798 (Div. 2)

    本篇为 Codeforces Round #798 (Div. 2) 也就是 CF1689 的题解,因本人水平比较菜,所以只有前四题 A.Lex String 题目描述 原题面 给定两个字符串 \(a ...

  7. Codeforces 716A Crazy Computer 【模拟】 (Codeforces Round #372 (Div. 2))

    A. Crazy Computer time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  8. Codeforces 716B Complete the Word【模拟】 (Codeforces Round #372 (Div. 2))

    B. Complete the Word time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  9. 【CF1256】Codeforces Round #598 (Div. 3) 【思维+贪心+DP】

    https://codeforces.com/contest/1256 A:Payment Without Change[思维] 题意:给你a个价值n的物品和b个价值1的物品,问是否存在取物方案使得价 ...

随机推荐

  1. codeforces902B. Coloring a Tree

    B. Coloring a Tree 题目链接: https://codeforces.com/contest/902/problem/B 题意:给你一颗树,原先是没有颜色的,需要你给树填色成指定的样 ...

  2. 自己实现的JDBC工具类

    最近做了个后台应用程序,刚开始用Spring+iBatis来做的,后来因为种种原因,不让用Spring.iBatis以及一些开源的工具包.   于是用JDBC重写了原来的Service实现,项目做完了 ...

  3. AngularJs学习——实现数据绑定的三种方式

    三种方式: 方式一:<h5>{{msg}}</h5>  此方式在页面刷新的时候会闪现{{}} 方式二:<h5 ng-bind="msg">< ...

  4. 图片上传是否为空,以及类型的js验证

    function check2() { var file = document.getElementsByName("file").value; if(file=="&q ...

  5. Windows下使用批处理实现启动关闭mysql_DOS/BAT

    cls @echo off :设置窗口字体颜色 color 0a :设置窗口标题 TITLE MySQL管理程序 by ThinkVenus call :checkAdmin goto menu :菜 ...

  6. bzoj3918 [Baltic2014]Postman

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3918 [题解] 每日至少更一题啊qwq凑任务(迷 明显猜个结论:随便搜环就行了 然后搜环姿势 ...

  7. [POJ1845&POJ1061]扩展欧几里得应用两例

    扩展欧几里得是用于求解不定方程.线性同余方程和乘法逆元的常用算法. 下面是代码: function Euclid(a,b:int64;var x,y:int64):int64; var t:int64 ...

  8. NYOJ 284 坦克大战 (广搜)

    题目链接 描述 Many of us had played the game "Battle city" in our childhood, and some people (li ...

  9. bzoj 1251 裸splay

    裸的splay,只需要注意 max[-1]:=-maxlongint 就行了,否则在update的时候子节点的 max值会在max[-1]里选 /*************************** ...

  10. JavaScript设置获取和设置属性的方法

    这篇文章主要介绍了JavaScript设置获取和设置属性的方法,学会使用getAttribute.setAttribute的用法,需要的朋友可以参考下   getAttribute 该方法用来获取元素 ...