花了6个小时,终于成功ac。。。。。。

两边dfs,第一遍求子树和,第二遍判断有没有2*t【s】/3和t【s】/3,因为要求的节点可能是在同一条线上,同时要有2*t【s】/3和t【s】/3的情况,且2*t【s】/3距根节点更近

还要注意的是如果同一条线上有两个t【s】/3的点是行不通的,必须要在两个不同分支上

结果跑了1996ms,险过=-=

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cassert>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define C 0.5772156649
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f; ll t[N];
int s;
vector<int>v[N];
set<int>ans;
map<int,int>ma;
void dfs(int u,int f)
{
for(int i=;i<v[u].size();i++)
if(v[u][i]!=f)
{
dfs(v[u][i],u);
t[u]+=t[v[u][i]];
}
}
void dfs1(int u,int f,int two,int one)
{
// cout<<u<<" "<<f<<" "<<two<<" "<<one<<endl;
for(int i=;i<v[u].size();i++)
if(v[u][i]!=f)
{
if(t[v[u][i]]==*t[s]/)dfs1(v[u][i],u,v[u][i],one);
else if(t[v[u][i]]==t[s]/)
{
ans.insert(v[u][i]);
ma[one]=;
if(two!=)
{
cout<<two<<" "<<v[u][i]<<endl;
exit();
}
else dfs1(v[u][i],u,two,v[u][i]);
}
else dfs1(v[u][i],u,two,one);
}
// if(v[u].size()==1&&one!=0)ans.insert(one);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
int n;
cin>>n;
for(int i=;i<=n;i++)
{
int a;
cin>>a>>t[i];
if(a==)
{
s=i;
continue;
}
v[a].push_back(i);
v[i].push_back(a);
}
dfs(s,-);
// cout<<t[3]<<endl;
if(t[s]%!=)
{
cout<<-<<endl;
return ;
}
dfs1(s,-,,);
// for(auto u:ans)cout<<u<<endl;
v[].clear();
for(auto u:ans)
if(!ma[u])
v[].push_back(u);
if(v[].size()>=)cout<<v[][]<<" "<<v[][]<<endl;
else cout<<-<<endl;
return ;
}
/********************
5
0 2
1 2
2 -1
3 -1
4 1
********************/

CodeForces - 767C的更多相关文章

  1. codeforces 767C - Garland

    题目链接:http://codeforces.com/contest/767/problem/C 问能否将一棵带点权的书分成点权$3$块,求任意方案. 其实考虑一棵以$x$为根的子树权值为${\fra ...

  2. 【codeforces 767C】Garland

    [题目链接]:http://codeforces.com/contest/767/problem/C [题意] 一棵树; 树上的每个节点都有一个权值; 让你把一棵树切掉两条边; 然后把这棵树分成了3个 ...

  3. CodeForces - 767C Garland 树的遍历

    C. Garland time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  4. Codeforces#398 &767C. Garland 树形求子节点的和

    传送门 题意:在一个树上,问能否切两刀,使得三块的节点值的和相同. 思路: 由于这个总的节点和是不变的,每块的节点值和sum固定,dfs搜索,和等于sum/3,切.若不能分成三块(不能被3整除,-1) ...

  5. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  6. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  7. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  8. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  9. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

随机推荐

  1. 【总结】两种 NIO 实现:Selector 与 Epoll

    时间2012-11-17 08:38:42 开源中国新闻原文  http://my.oschina.net/ielts0909/blog/89849 我想用这个话题小结下最近这一阶段的各种测试和开发. ...

  2. Squid 缓存代理服务器的完整配置

    Squid 缓存代理服务器 Squid 的作用 1.通过缓存的方式为用户提供web访问加速 2.对用户的web访问进行过滤控制 缓存代理服务器又分为普通代理服务器,透明代理服务器,和反向代理服务器. ...

  3. Json反序列化Map的key不能是Object

    使用json作为数据传输格式,碰到一个问题.我希望传输的是一个Map<Target, TargetInfo>其中Target是一个对象,作为map的一个key public class T ...

  4. Redis数据存储优化机制(转)

    原文:Redis学习笔记4--Redis数据存储优化机制 1.zipmap优化hash: 前面谈到将一个对象存储在hash类型中会占用更少的内存,并且可以更方便的存取整个对象.省内存的原因是新建一个h ...

  5. 0503-Hystrix保护应用-feign的hystrix支持

    一.概述 1.1.基础[示例一] 如果Hystrix在类路径上并且feign.hystrix.enabled = true,Feign将用断路器包装所有方法.还可以返回com.netflix.hyst ...

  6. JS操作时间 - UNIX时间戳简单介绍

    准备知识 GMT(Greenwich Mean Time) - 格林尼治时间.UTC(Universal Time Code) - 国际协调时间.PST(Pacific Standard Time,太 ...

  7. HTTP协议简要介绍

    1. 网络基础 TCP/IP 通常使用的网络是在TCP/IP协议簇基础上运作的. HTTP属于它内部的一个子集. TCP/IP分为4个层次, 应用层, 传输层, 网络层, 链路层. (Applicat ...

  8. redmine安装及SVN(https)配置

    一键安装redmine https://blog.csdn.net/qq_26898315/article/details/50233483 配置SVN(引用: https://blog.csdn.n ...

  9. Java消息队列ActiveMQ (一)--JMS基本概念

    摘要:The Java Message Service (JMS) API is a messaging standard that allows application components bas ...

  10. JSON 弹窗

    JSON和AJAX <script type="text/javascript"> $(document).ready(function(e) { var a = { ...