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,问你什么时候来 排队等待的时间最少 ...
随机推荐
- Oracle PL/SQL块 多表查询(emp员工表、dept部门表、salgrade工资等级表)
范例: 查询每个员工的编号,姓名,职位,工资,工资等级,部门名称 ●确定要使用的数据表 |- emp表:员工的编号.姓名.职位.工资 |- salgrade表:工资等级 |- dept表:部门名称 ● ...
- MQTT协议介绍
http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.pdf MQTT 文档 http://mqtt.org/new/wp-c ...
- fastjson的序列化属性
在将使用JSON.toJSONString(result, SerializerFeature.PrettyFormat)将JSONObject转化为字符串时,可以指定一些序列化属性,设置转化后的字符 ...
- 支持WEB、Android、IOS的地图解决方案
转自原文 支持WEB.Android.IOS的地图解决方案 工具链 GIS工具集 OpenGeo Suite 包含PostGIS, GeoServer, GeoWebCache, OpenLayers ...
- Oracle SOA Suit Medicator and OSB
Medicator和OSB (Oracle Service Bus)存在的目的,从架构的设计模式上看,和解耦多态等理念非常的相似. 通过Proxy代理的方式,把真正某个Service的实现进行隐藏,让 ...
- JavaScript校验输入的字符串是否包含特殊字符
校验在文本框输入的字符串中是否包含特殊字符串,js代码如下 function strInclude(substring){ if(substring){ var reg = new RegExp(&q ...
- 一个Netfilter nf_conntrack流表查找的优化-为conntrack添加一个per cpu cache
独悲须要忍受.快乐须要分享对Linux协议栈多次perf的结果,我无法忍受conntrack的性能,然而它的功能是如此强大,以至于我无法对其割舍,我想自己实现一个高速流表.可是我不得不抛弃依赖于con ...
- C#中如何让ListView控件点击选中整行
将Listview控件的FullRowSelect属性置为True,当然Listview的View属性应该是Details. 2017年6月25日17:15:55
- C#向Sql数据库插入空值的控制
string name = textBox1.Text; int age = Convert.ToInt32(textBox2.Text.Trim()); ? null : (int?)Convert ...
- UVA 610 - Street Directions(割边)
UVA 610 - Street Directions option=com_onlinejudge&Itemid=8&page=show_problem&category=5 ...