花了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. 【我的Android进阶之旅】解决Android Studio启动时报错:Java 1.8 or later is required.

    错误描述 在公司电脑上运行Android Studio 2.2已经有一段时间了,但是自己的笔记本上还是用的Android Studio 1.5,今天晚上下了一个Android Studio 2.2压缩 ...

  2. Delphi 正则表达式语法(7): 匹配转义字符

    Delphi 正则表达式语法(7): 匹配转义字符 // ? 号的意义是匹配 0-1 次, 如果需要匹配 ? 怎么办 var   reg: TPerlRegEx; begin   reg := TPe ...

  3. PL/SQL编程—包

    1.PLSQL 中的包就相当于java中的package,主要好处有(1)防止命名污染,(2)功能统一,(3)允许重载,(4)可以隐藏核心代码,(5)最重要的就是断开依赖链. 2.对于一个程序需要大量 ...

  4. PHP 权限管理

    login页面 <form action="loginchuli.php" method="post"> <div>用户名:<in ...

  5. Kattis - honey【DP】

    Kattis - honey[DP] 题意 有一只蜜蜂,在它的蜂房当中,蜂房是正六边形的,然后它要出去,但是它只能走N步,第N步的时候要回到起点,给出N, 求方案总数 思路 用DP 因为N == 14 ...

  6. SVN如何切换用户对代码进行操作

    在使用svn更新或提交数据时需要输入用户名和密码,在输入框中可以选择是否记录,以便下次操作无需再次输入用户名和密码: 要切换其他用户名时,需要删除已记录用户的数据,在电脑桌面上右击,依次点击菜单项To ...

  7. poj3125

    /*水题,模拟排队*/#include<stdio.h>#include<string.h>#include<algorithm>using namespace s ...

  8. Zabbix JVM 安装

    Zabbix 服务端安装插件 系统:centos 7.4 x64 环境:zabbix 3.0.16 yum源:rpm -ivh http://repo.zabbix.com/zabbix/3.0/rh ...

  9. jQuery上下切换带缩略图的焦点图

    在线演示 本地下载

  10. 黑苹果Yosemite 10.10.1 修改wowpc.iso文件免选择直接启动Mac系统

    安装教程见: http://www.cnblogs.com/zouzf/p/4356641.html 网上很多教程都是OK的,但每个人的具体情况不同就可能有一些细节问题搞死你1.本文所指的 wowpc ...