【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 ...
随机推荐
- ubuntu下使用crontab定时器
crontab 定时工具(周期性执行的任务列表称为Cron Table),其中每一个任务,被称为Cron Job. 可以,每分钟执行,每小时执行,每天执行,每周执行,每月执行. 检查服务 1.查看任务 ...
- 91. Ext中获取combobox中的valueField和displayField的值
转自:https://blog.csdn.net/jcy472578/article/details/42113119Ext.getCmp("schemaVersion").val ...
- 05、ListActivity的使用
第一个好处:处理共同的操作,避免代码重复 假设我要写第二个界面我也是需要使用到mapView,那你都要去查找一个mapView.都要获取一个Map的一个地图. 第二个好处:代码规范(方便阅读,真实开发 ...
- Going Home(MCMF)
http://poj.org/problem?id=2195 题意:在一个n*m的图中,'m'代表人,'H'代表房子,人每移动一次的费用为1,求所有人移动到房子里的最小花费. 思路:最小费用最大流问题 ...
- RHEL6.5安装成功ORACLE11GR2之后,编写PROC程序出错解决方法
1. proc: error while loading shared libraries: libclntsh.so.11.1: cannot open shared object file: N ...
- 图解TCP/IP笔记(3)——IP协议
目录 IP协议 IP寻址 IP地址组成 IP地址分类 广播地址 子网掩码 全局地址和私有地址 IP协议 跨越不同数据链路,实现两端节点之间的数据包传输 数据链路:只负责某一个区间之间的通信传输 IP协 ...
- Android基础TOP2_1:输出系统时间
Activity: <TextView android:id="@+id/tv" android:layout_width="wrap_content" ...
- dnn 添加图片
public string fileUpload() { if (fuPhoto.PostedFile != null && fuPhoto.P ...
- CSS动画:旋转卡片效果
<!DOCTYPE html> <html> <head> <title>demo</title> </head> <bo ...
- 用python写一个百度翻译
运行环境: python 3.6.0 今天处于练习的目的,就用 python 写了一个百度翻译,是如何做到的呢,其实呢就是拿到接口,通过这个接口去访问,不过中间确实是出现了点问题,不过都解决掉了 先晾 ...