【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 ...
随机推荐
- hdu 4123(树形dp+倍增)
Bob’s Race Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- PKUACM2018 A Wife——DP
题目:http://poj.openjudge.cn/practice/C18A/ 据说正解是差分约束,转化的过程还要用到标准型.对偶型什么的知识,暂时还不太懂... 但也有贪心DP做法,有个结论:一 ...
- IDEA Spark Streaming 操作(文件源)
import org.apache.spark.SparkConf import org.apache.spark.streaming.{Seconds, StreamingContext} obje ...
- A. Train and Peter
A. Train and Peter time limit per test 1 second memory limit per test 64 megabytes input standard in ...
- 如何使js函数异步执行
CallbacksCallbacks使用场景在哪里?在很多时候需要控制一系列的函数顺序执行.那么一般就需要一个队列函数来处理这个问题: function Aaron(List, callback) { ...
- PropertyInfo 类
[AttributeUsage(AttributeTargets.Property)] //Models 特性 public class CanWriteAttribute : Attr ...
- Bitmap与String之间的转换
/** * 将bitmap转换成base64字符串 * * @param bitmap * @return base64 字符串 */ public String bitmaptoString(Bit ...
- jsp文件就是Servlet,可以在tomcat里进行查看
D:\apache-tomcat-8.0.21\work\Catalina\localhost\springmvc2\org\apache\jsp\index_jsp.class
- 百度之星2017初赛B1006 小小粉丝度度熊
思路: 考虑到补签卡一定是连续放置才更优,所以直接根据起始位置枚举.预先处理区间之间的gap的前缀和,在枚举过程中二分即可.复杂度O(nlog(n)). 实现: #include <iostre ...
- mysql之数据去重并记录总数
引用: http://blog.sina.com.cn/s/blog_6c9d65a10101bkgk.htmlhttp://www.jb51.net/article/39302.htm 1.使用di ...