花了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. Vue(6)- Vue-router进阶、单页面应用(SPA)带来的问题

    一.Vue-router进阶 回顾学过的vue-router,并参考官方文档学习嵌套路由等路由相关知识. 二.单页面应用(SPA)带来的问题 1.虽然单页面应用有优点,但是,如果后端不做服务器渲染(h ...

  2. springboot集成shiro和开涛的动态url问题

    我出现的问题就是一旦/**=authc不管放到前面还是后面都会把所有的资源全部拦截,css和js都访问不到,只需要把开涛的动态url代码改一下就行了(如上图)

  3. Python高级教程-高阶函数

    Higher-order function(高阶函数) 变量可以指向函数 以Python内置的求绝对值的函数abs()为例,调用该函数用以下代码: >>> abs(-10) 10 但 ...

  4. Nordic Blue Tooth

    一 . nordic BLE4.0 1.开发nordic的应用需要安装支持keil的pack库和插件 2.nordic的SDK很完整,实例涵盖了几乎所有的应用 https://www.nordicse ...

  5. LeetCode:下一个更大元素I【31】

    LeetCode:下一个更大元素I[31] 题目描述 给定两个没有重复元素的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集.找到 nums1 中每个元素在 nums2 中的 ...

  6. mysql行列互相转换

    列转行: mysql> select * from test; +------+----------+-------+ | id | subject | score | +------+---- ...

  7. ubuntu16.04 虚拟机 安装win7/win10

    http://www.xitongcheng.com/jiaocheng/xtazjc_article_26588.html https://blog.csdn.net/sunyao_123/arti ...

  8. 20145217《网络对抗》web基础

    20145217<网络对抗>web基础 一.问题 1.什么是表单? 表单:可以收集用户的信息和反馈意见,是网站管理者与浏览者之间沟通的桥梁. 表单包括两个部分:一部分是HTML源代码用于描 ...

  9. 2062326 齐力锋 实验四《Java面向对象程序设计Android开发》实验报告

    北京电子科技学院(BESTI) 实 验 报 告 课程: 程序设计与数据结构  班级: 1623  姓名: 齐力锋 学号: 20162326 成绩: 指导教师: 娄嘉鹏/王志强 实验日期: 2017年5 ...

  10. React Native混合开发中必须要学会点FlexBox布局

    在前面的案例中,界面的搭建都是采用CSS的布局,基于盒子模型,依赖 display属性 , position属性, float属性.但对于那些特殊布局非常不方便,比如,垂直居中. 一种全新的针对web ...