【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的物品,问是否存在取物方案使得价 ...
随机推荐
- 深入理解Java虚拟机—内存管理机制
前面说过了类的加载机制,里面讲到了类的初始化中时用到了一部分内存管理的知识,这里让我们来看下Java虚拟机是如何管理内存的. 先让我们来看张图 有些文章中对线程隔离区还称之为线程独占区,其实是一个意思 ...
- nodejs 喜欢报cannot find module .....的简单解决方案
在安装nodejs后使用命令npm install <package_name>一直喜欢报cannot find module........ 因为我之前在我的电脑上安装过nodejs,当 ...
- 使用fuser查询文件、目录、socket端口的占用进程
fuser可用于查询文件.目录.socket端口和文件系统的使用进程 1.查询文件和目录使用者 fuser最基本的用法是查询某个文件或目录被哪个进程使用: # fuser -v ./ ...
- 设置eclipse控制台上的信息输入到某个文件
转摘自:http://binary.duapp.com/2013/09/1511.html Run->Run Configurations->Common->File
- 微信小程序基础知识
一.基本目录结构 app.js 定义app入口 app.json 定义页面配置 index.js 页面中的事件和监听 index.wxml 定义布局文件 1.app.json配置基本信息 { “pag ...
- 图论---图的m-点着色判定问题(回溯法--迭代式)
转自 图的m着色问题 图的m-着色判定问题——给定无向连通图G和m种不同的颜色.用这些颜色为图G的各顶点着色,每个顶点着一种颜色,是否有一种着色法使G中任意相邻的2个顶点着不同颜色? 图的m-着色优化 ...
- SpringMvc基础知识(二) springmvc和mybatis整合
1 springmvc和mybatis整合 1.1 需求 使用springmvc和mybatis完成商品列表查询. 1.2 整合思路 springmvc+mybaits的系统架构: 第一步:整合dao ...
- arm SecurCore 处理器【转】
转自:http://www.arm.com/zh/products/processors/securcore/index.php SecurCore 处理器 (View Larger SecurCor ...
- JQ子页面对父页面的元素进行操作
需要加上parent.document,才能找到父页面的元素 如: $("#tabs", parent.document).click();
- 【bzoj1086】王室联邦
我以为树分块什么的必有高论,结果居然是个暴力…… 方法也很简单,我看下每个节点dfs的时候是否已经大于k个,大于的话我就新开一块. 注意dfs的时候当前节点不能放进子树的块中. #include< ...