【codeforces 767C】Garland
【题目链接】:http://codeforces.com/contest/767/problem/C
【题意】
一棵树;
树上的每个节点都有一个权值;
让你把一棵树切掉两条边;
然后把这棵树分成了3个部分;
要求这3个部分,每个部分的权值和相同;
即sum1=sum2=sum3
【题解】
树形DP;
一开始累加所有节点的权值和sum;
如果不是3的倍数则直接输出无解;
用cnt[x]记录x节点下方的子树和(权值和);
假设dfs到了第x个节点
考虑两种情况;
①
有两个节点y,z;
且sum/3==cnt[y]==cnt[z]
且y和z分别在x的两个儿子节点所在的子树中;
输出y和z就好
②
2/3*sum==cnt[x];
然后x的子树里面有另外一个节点y
满足1/3*sum==cnt[y];
输出x和z就好
这里注意x不能为根节点!
定义个数组往上传这个子树里面有没有1/3sum和2/3sum就好;
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 1e6+1000;
int n,sum=0,goal;
int fa,w[N],root,cnt[N],haved1[N],haved2[N];
vector <int> G[N];
void dfs(int x)
{
cnt[x] = w[x];
int num = 0,a[5];
for (int y : G[x])
{
dfs(y);
if (haved1[y])
{
haved1[x] = haved1[y];
a[++num] = haved1[y];
if (num > 1)
{
printf("%d %d\n", a[1], a[2]);
exit(0);
}
}
if (haved2[y]) haved2[x] = haved2[y];
cnt[x] += cnt[y];
}
if (cnt[x] == goal) haved1[x] = x;
if (cnt[x] == 2 * goal) haved2[x] = x;
if (x!=root && 2*goal==cnt[x])
{
for (int y : G[x])
{
if (haved1[y])
{
printf("%d %d\n", haved1[y], x);
exit(0);
}
}
}
}
int main()
{
//freopen("F:\\rush.txt", "r", stdin);
rei(n);
rep1(i, 1, n)
{
rei(fa), rei(w[i]);
if (fa == 0)
root = i;
G[fa].ps(i);
sum += w[i];
}
if (sum % 3) return puts("-1"), 0;
goal = sum / 3;
dfs(root);
puts("-1");
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
【codeforces 767C】Garland的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 758B】Blown Garland
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
- 【Codeforces 670C】 Cinema
[题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...
随机推荐
- XAML实例教程系列 - 对象和属性(二)
XAML实例教程系列 - 对象和属性 2012-05-22 14:18 by jv9, 1778 阅读, 6 评论, 收藏, 编辑 在前一篇已经介绍XAML概念:“XAML语言是Extensible ...
- go语言笔记——指针,和C用法以及本质一样,但不支持指针的+-运算!
4.4.2 值类型和引用类型 所有像 int.float.bool 和 string 这些基本类型都属于值类型,使用这些类型的变量直接指向存在内存中的值. Go 语言的取地址符是 &,放到一个 ...
- Vue.prototype的用法
基础事例: 在vue项目main.js文件中: Vue.prototype.$appName = 'My App' 这样你可以通过在原型上定义它们使其在每个 Vue 的实例中可用. new Vue({ ...
- Ballot evaluation
http://acm.hdu.edu.cn/showproblem.php?pid=2986 题意很简单,主要是要处理精度,最手残的是把单词拼写错了... #include <stdio.h&g ...
- 9.15NOIP模拟题
GRYZ 模拟考试套题 9.15 gryz信息组专场 题目名称 最初的最初 太 妃 糖 可执行文件名 eat hwc dance sugar 输入文件 eat.in hwc.in dance.in s ...
- mysql索引的操作
一.创建和查看普通索引 这是最基本的索引类型,而且它没有唯一性之类的限制 1.创建表时创建普通索引 CREATE TABLE table_name( 属性名 数据类型, ... 属性名 数据类型, I ...
- 从源码看ASP.NET框架(一)-打造页面控件树
测试实例如下: 前台代码MyFirstWeb.aspx(没有服务器控件,即没有runat) CodeBehind="MyFirstWeb.aspx.cs":表示代码后置类文件 In ...
- Android中Adapter和Bridge模式理解和应用
一 Adapter模式 意图: 将一个类的接口转换成客户希望的另外一个接口. Adapter模式使得原本由于接口不兼容而不能在一起工作的那些类可以在一起工作. 适用性: 使用一个已存在的类,而它的接口 ...
- UIWebview 截获html并修改内容。
需求:混合应用UIWebView打开html后,UIWebView有左右滚动条,要去掉左右滚动效果: 方法:通过js截获UIWebView中的html,然后修改html标签内容: 实例代码: 服务 ...
- id拼接保存到单个字段后作为表连接的查询条件
SELECT q.id, concat(q. NAME) qname, d.id did, d. NAME FROM question_po q LEFT JOIN data_configuratio ...