【DFS】Codeforces Round #398 (Div. 2) C. Garland
设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的更多相关文章
- 【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 ...
- 【推导】【DFS】Codeforces Round #429 (Div. 1) B. Leha and another game about graph
题意:给你一张图,给你每个点的权值,要么是-1,要么是1,要么是0.如果是-1就不用管,否则就要删除图中的某些边,使得该点的度数 mod 2等于该点的权值.让你输出一个留边的方案. 首先如果图内有-1 ...
- 【贪心】【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< ...
- 【枚举】【贪心】 Codeforces Round #398 (Div. 2) B. The Queue
卡题意……妈的智障 一个人的服务时间完整包含在整个工作时间以内. 显然,如果有空档的时间,并且能再下班之前完结,那么直接输出即可,显然取最左侧的空档最优. 如果没有的话,就要考虑“挤掉”某个人,就是在 ...
- 【暴力】Codeforces Round #398 (Div. 2) A. Snacktower
题意不复述. 用个bool数组记录一下,如果某一天,当前剩下的最大的出现了的话,就输出一段. #include<cstdio> using namespace std; int n; bo ...
- 【题解】Codeforces Round #798 (Div. 2)
本篇为 Codeforces Round #798 (Div. 2) 也就是 CF1689 的题解,因本人水平比较菜,所以只有前四题 A.Lex String 题目描述 原题面 给定两个字符串 \(a ...
- 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 ...
- 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 ...
- 【CF1256】Codeforces Round #598 (Div. 3) 【思维+贪心+DP】
https://codeforces.com/contest/1256 A:Payment Without Change[思维] 题意:给你a个价值n的物品和b个价值1的物品,问是否存在取物方案使得价 ...
随机推荐
- 如何设置项目encoding为utf-8
1.鼠标右键点击项目,选择[properties] 2.选择[Resource],在Text file encoding里面选择UTF-8,点击[ok] 大功告成! 木头大哥所发的文章均基于自身实践, ...
- MySQL 数据库性能优化之缓存参数优化
在平时被问及最多的问题就是关于 MySQL 数据库性能优化方面的问题,所以最近打算写一个MySQL数据库性能优化方面的系列文章,希望对初中级 MySQL DBA 以及其他对 MySQL 性能优化感兴趣 ...
- ionic3自定义图标
http://blog.csdn.net/qq993284758/article/details/78107412
- Django随笔 01
Django 视图 不处理用户输入,而仅仅决定要展现哪些数据给用户: Django 模板 仅仅决定如何展现Django视图指定的数据. dd http://blog.csdn.net/pipisorr ...
- ubunut14.04 mentohust配置
1.设置网卡eth0的IP地址和子网掩码 sudo ifconfig eth0 10.162.32.94 netmask 255.0.0.0 将IP地址改为:10.162.32.94,子网掩码改为 ...
- kafka.common.ConsumerRebalanceFailedException异常解决
kafka.common.ConsumerRebalanceFailedException: group_dd-1446432618163-2746a209 can't rebalance after ...
- CentOS 安装 debuginfo-install
安装debuginfo相关的包步骤如下: 1. 修改文件/etc/yum.repos.d/CentOS-Debuginfo.repo中的enabled参数,将其值修改为1 2. 使用命令: yum i ...
- 使用命令wsimport生成WebService客户端
使用命令wsimport生成WebService客户端 wsimpost命令有几个重要的参数: -keep:是否生成java源文件 -d:指定输出目录 -s:指定源代码输出目录 -p ...
- Linux Top 命令参数解析
转载自:http://www.jb51.net/LINUXjishu/34604.html TOP是一个动态显示过程,即可以通过用户按键来不断刷新当前状态.如果在前台执行该命令,它将独占前台,直到用户 ...
- Maven环境搭建、调试、打包
1.配置Maven环境 将下载文件解压,然后设置maven环境 新建环境变量M2_HOME 变量名:M2_HOME 变量值:F:\maven\apache-maven-3.0.3 追加path环境变量 ...