Codeforces Round #398 (Div. 2) C. Garland —— DFS
题目链接:http://codeforces.com/contest/767/problem/C
题解:类似于提着一串葡萄,用剪刀剪两条藤,葡萄分成了三串。问怎样剪才能使三串葡萄的质量相等。
首先要做的就是统计葡萄的总质量tot。之后就是找到两子串质量为(tot/3)的葡萄(如果除不尽,则必定找不到),那么剩下的就是dfs搜索了。
我一开始的做法是先建一棵记录子树质量和的树,然后再从上往下dfs,如果找到了,就把它剪掉。后来发现被剪掉的那一串可能就含有两串质量为(tot/3)的葡萄(这里质量 可为负数), 所以这种方法行不通。
那么正确的做法是先深入到最底层(叶子结点),然后再从下往上搜索,这个过程与建树的过程是一致的。所以可以将两个过程合并在一起。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<string>
#include<set>
#define LL long long
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
#define INF 0x7fffffff
#define LNF ((1LL<<62)-1)
#define maxn 200010 using namespace std; int n, tot = ,cnt = , sum[], vis[];
vector<int>child[];
int ans[]; int build(int rt)
{
vis[rt] = ;
int m = child[rt].size();
for(int i = ; i<m; i++)
{
if(!vis[child[rt][i]])
sum[rt] += build(child[rt][i]); if(cnt==)
return ;
} if(sum[rt]==tot)
{
ans[cnt++] = rt;
return ;
}
else return sum[rt];
} int main()
{
int rt,v;
scanf("%d",&n);
for(int i = ; i<=n; i++)
{
scanf("%d%d",&v,&sum[i]); if(v)
child[i].push_back(v);
child[v].push_back(i); tot += sum[i];
if(!v) rt = i, vis[rt] = ;
} if(tot%)
{
puts("-1");
return ;
} tot /= ;
int m = child[rt].size();
for(int i = ; i<m; i++)
{
build(child[rt][i]);
if(cnt==) break;
} if(cnt==)
printf("%d %d\n",ans[],ans[]);
else
puts("-1"); }
Codeforces Round #398 (Div. 2) C. Garland —— DFS的更多相关文章
- 【DFS】Codeforces Round #398 (Div. 2) C. Garland
设sum是所有灯泡的亮度之和 有两种情况: 一种是存在结点U和V,U是V的祖先,并且U的子树权值和为sum/3*2,且U不是根,且V的子树权值和为sum/3. 另一种是存在结点U和V,他们之间没有祖先 ...
- Codeforces Round #398 (Div. 2)
Codeforces Round #398 (Div. 2) A.Snacktower 模拟 我和官方题解的命名神相似...$has$ #include <iostream> #inclu ...
- Codeforces Round #398 (Div. 2) A B C D 模拟 细节 dfs 贪心
A. Snacktower time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Codeforces Round #398 (div.2)简要题解
这场cf时间特别好,周六下午,于是就打了打(谁叫我永远1800上不去div1) 比以前div2的题目更均衡了,没有太简单和太难的...好像B题难度高了很多,然后卡了很多人. 然后我最后做了四题,E题感 ...
- Codeforces Round #222 (Div. 1) A. Maze dfs
A. Maze 题目连接: http://codeforces.com/contest/377/problem/A Description Pavel loves grid mazes. A grid ...
- Codeforces Round #398 (Div. 2) A,B,C,D
A. Snacktower time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Codeforces Round #245 (Div. 2) C. Xor-tree DFS
C. Xor-tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/430/problem/C ...
- Codeforces Round #398 (Div. 2) B,C
B. The Queue time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- Codeforces Round #398 (Div. 2) BCD
B:The Queue 题目大意:你要去办签证,那里上班时间是[s,t), 你知道那一天有n个人会来办签证,他们分别是在时间点ai来的.每个人办业务要花相同的时间x,问你什么时候来 排队等待的时间最少 ...
随机推荐
- MX
A mail exchanger record (MX record) is a type of resource record in the Domain Name System that spec ...
- awk在企业中最常用的语句
awk最常用以及面试基本都会被问到的实例: A.统计日志中每个URL被访问的次数 cat access.log http://www.etiantian.org/1.html http://post. ...
- Jython中文乱码问题
最近,在项目中需要用到Java代用Python的代码,并且需要传参数,因此选用了Jython包,但是,如果在调用python脚本时,出现了中文乱码的现象.代码如下: PythonInterpreter ...
- tomcat访问(access)日志配置、记录Post请求参数
tomcat访问(access)日志配置.记录Post请求参数 一.配置与说明 tomcat访问日志格式配置,在config/server.xml里Host标签下加上 <Valve classN ...
- 快速比较两个uiimage是否相等防止使用原始dsdata造成界面卡顿问题
UIImage *imageLater = image1; UIImage *imagePre = image2; if (imageLater == imagePre){....}
- git 怎样删除远程仓库的最近一次错误提交?
假设你有3个commit如下: commit 3 commit 2 commit 1 其中最后一次提交commit 3是错误的,那么可以执行: git reset --hard HEAD~1 你会发现 ...
- DVBS/S2在数字电视系统中的应用 三 (LNB介绍)
DVBS/S2在数字电视系统中的应用 三 (LNB介绍) 老谢在前面两篇文章中(例如以下).都有提到LNB这一概念. DVBS/S2在数字电视系统中的应用 一 (DVBS/S2接收系统Block Di ...
- Unity开发规范(个人习惯,仅供參考)
近期整理了一下unity里的文件夹使用和脚本上的一些规范,这个看个人习惯,仅供參考 1.unity中的Project文件夹 总体文件夹大致例如以下: 按资源种类分目录. ...
- php性能监控扩展xhprof
XHProf是facebook开源出来的一个php轻量级的性能分析工具,跟Xdebug类似,但性能开销更低,还可以用在生产环境中,也可以由程序开 关来控制是否进行profile.总体来说是个不错的工具 ...
- 上传图片/文件到server
package yao.camera.util; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; impo ...