【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 ...
随机推荐
- CF949 C Data Center Maintenance——边双连通分量
题目:http://codeforces.com/contest/949/problem/C 把一个点指向修改它会影响到的点就可以做了: 有取模,所以多出一些要注意的地方,首先是可能出现环,所以需要 ...
- Makefile 实际用例分析(三) ------- 是用GUN automake 处理自己的工程
前面两篇已经说过了自己怎么去为一个工程写makefile: 第一篇 第二篇 现在这一篇说的是怎么使用GNU的工具去写一个符合开源标准的Makefile呢! 首先我觉你应该参考: Automake Au ...
- Java根据年度将数据分组
现在有这么一组数据 code name year 45615654 x1 ...
- 自己的myeclipse添加javaee7步骤
1 new一个javaee名字,然后add jars 就可以
- 324 Wiggle Sort II 摆动排序 II
给定一个无序的数组nums,将它重新排列成nums[0] < nums[1] > nums[2] < nums[3]...的顺序.例子:(1) 给定nums = [1, 5, 1, ...
- [ HAOI 2010 ] 最长公共子序列
\(\\\) \(Description\) 求两个长度\(\le5000\)的大写字母串的\(LCS\)长度及个数,定义两\(LCS\)中某一字符在两序列出现位置有一处不同就视为不同. \(\\\) ...
- SQL基本操作——case end
case end进行多条件的判断 --查看Person表 select * from Person --对math字段进行条件判断 select name,数学成绩= case then '优' th ...
- spring 415
不支持的媒体类型 spring mvc 使用@requestBody注解json请求时,jQuery有限制,否则会出现 415 错误 1.使用ajax $.ajax({ ...
- JavaScript学习书签
JavaScript常用正则表达式 闭包 JavaScipt DOM 变量提升
- 浏览器的 local storage
浏览器 local storage 本地存储 session storage 会话存储 cookies 本地存储 1. local stora ...