花了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. tomcat 介绍

    Tomcat简介 Tomcat是Apache软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache.Sun和其他一些公司及个人共同开发 ...

  2. 部署 jdk

    首先安装jdk jdk提供java环境变量 jvm虚拟机 为什么同一份java程序可以在不同系统上跑? 就是因为jdk jvm虚拟机使java支持 跨平台服务器部署 首先jvm 去读取java代码   ...

  3. 假设做一个精美的Login界面(攻克了一EditText自带clear的功能,相似iphone的UITextField)

    先上图:     XML为: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ...

  4. Ubuntu Linux下通过代理(proxy)使用git上github.com

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/loveaborn/article/details/24575659 github.com.作为程序猿 ...

  5. ViewPager学习及使用(一)

    一:基础篇 1.ViewPager的简介和作用ViewPager是android扩展包v4包中的类,这个类可以让用户左右切换当前的view1)ViewPager类直接继承了ViewGroup类,所有它 ...

  6. getElementsByClassName - 兼容详细介绍

    概述 JavaScript中getElementsByClassName()方法IE8及以下不支持.本文通过使用正则表达式实 现1个兼容方案. 本文内容分为3个部分. 浏览器原生getElements ...

  7. 吴超老师课程--Flume的安装和介绍

    常用的分布式日志收集系统

  8. python16_day02【列表、字典】

    1.列表 names = ['Alex',"Tenglan",'Eric'] >>> names[0] 'Alex' >>> names[2] ...

  9. go——变量

    在数学概念中,变量(variable)表示没有固定值且可以改变的数.但从计算机系统实现角度来看,变量是一段或多段用来存储数据的内存.作为静态类型语言,Go语言总是有固定的数据类型,类型决定了变量内存的 ...

  10. Delphi 正则表达式之TPerlRegEx 类的属性与方法(5): Compile、Study

    Delphi 正则表达式之TPerlRegEx 类的属性与方法(5): Compile.Study // Compile.Study var   reg: TPerlRegEx; begin   re ...