CodeForces - 767C
花了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的更多相关文章
- codeforces 767C - Garland
题目链接:http://codeforces.com/contest/767/problem/C 问能否将一棵带点权的书分成点权$3$块,求任意方案. 其实考虑一棵以$x$为根的子树权值为${\fra ...
- 【codeforces 767C】Garland
[题目链接]:http://codeforces.com/contest/767/problem/C [题意] 一棵树; 树上的每个节点都有一个权值; 让你把一棵树切掉两条边; 然后把这棵树分成了3个 ...
- CodeForces - 767C Garland 树的遍历
C. Garland time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- Codeforces#398 &767C. Garland 树形求子节点的和
传送门 题意:在一个树上,问能否切两刀,使得三块的节点值的和相同. 思路: 由于这个总的节点和是不变的,每块的节点值和sum固定,dfs搜索,和等于sum/3,切.若不能分成三块(不能被3整除,-1) ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
随机推荐
- mysql数据库表插入单条数据/批量插入数据
1.创建表格 reate table trade( id int(4) not null primary key auto_increment, product varchar(30) null, p ...
- rails 单数 复数 大写 小写转换 下划线 驼峰命名
downcase 变小写 pluralize 复数 singularize 单数 camelcase 驼峰 underscore : “MyScore”.undersocre ==> my_s ...
- PL/SQL编程—包
1.PLSQL 中的包就相当于java中的package,主要好处有(1)防止命名污染,(2)功能统一,(3)允许重载,(4)可以隐藏核心代码,(5)最重要的就是断开依赖链. 2.对于一个程序需要大量 ...
- Java消息队列ActiveMQ (一)--JMS基本概念
摘要:The Java Message Service (JMS) API is a messaging standard that allows application components bas ...
- Java程序设计专题
- ThinkPHP框架基础知识三
一.JS文件与Css文件存放位置 其实JS与Css文件放在任意位置都可以找到,只要路径正确就行. 在TP框架中我们访问的所有文件都要走入口文件index.php,相当于访问的是index.php页面. ...
- HDFS 详解
HDFS 概述 基于2.7.3 HDFS 优点: 1.高容错性 数据自动保存多个副本,默认是三个副本 副本丢失后,会自动恢复 2.适合批处理 移动计算而非移动数据,批处理的时候,数据量很大,移动数据是 ...
- javascript 中的比较大小,兼 typeof()用法
javascript中的排序: 1.不同类型 比 类型 (字符串 > 数字) 2.同类型:(字符串 比 按字母顺序 )(数字 比 大小) 测试: <!DOCTYPE html> ...
- hadoop26----netty,多个handler
k客户端: package cn.itcast_03_netty.sendorder.client; import io.netty.bootstrap.Bootstrap; import io.ne ...
- Django CRM 数据库增删改查
原文链接 http://www.cnblogs.com/yangmv/p/5327477.html