There is a tree of n vertices. For each vertex a list of all its successors is known (not only direct ones). It is required to restore the tree or to say there is no such tree. Input The first line contains a single integer n (1 ≤ n ≤ 1000) — the num…
Problem Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties. There is exactly one node, called the…
题目描述 给定一棵N个点的树,求树上一条链使得链的长度乘链上所有点中的最小权值所得的积最大. 其中链长度定义为链上点的个数. 输入 第一行N 第二行N个数分别表示1~N的点权v[i] 接下来N-1行每行两个数x.y,表示一条连接x和y的边 输出 一个数,表示最大的痛苦程度. 样例输入 3 5 3 5 1 2 1 3 样例输出 10 题解 树的直径+并查集 首先肯定是把权值从大到小排序,按照顺序加点,维护每个连通块的最长链乘以当前点权值作为贡献. 那么如何在加上一条边,连接两棵树后快速得出新的直径…
点此看题面 大致题意: 给你一棵树,每条边为黑色或红色, 求有多少个三元组\((x,y,z)\),使得路径\((x,y),(x,z),(y,z)\)上都存在至少一条红色边. 容斥 我们可以借助容斥思想,用总方案数减去不合法方案数,就可以得到合法方案数. 一个不合法方案,就要使得路径\((x,y),(x,z),(y,z)\)中,至少存在一条路径是全黑的. 如果我们删去树上的红色边,只留下黑色的边.则可以发现,一个不合法方案,满足至少存在两个点在同一个连通块内. 计算答案 考虑用并查集,统计每一个连…
挺好的一道题. 把所有点都离线下来,一个个往里加入就行了. #include <cstdio> #include <algorithm> #define N 100003 #define ll long long #define setIO(s) freopen(s".in","r",stdin) using namespace std; int n,val[N]; namespace tree { int edges; int hd[N],…
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2654 [题目大意] 给你一个无向带权连通图,每条边是黑色或白色. 让你求一棵最小权的恰好有need条白色边的生成树.题目保证有解. [题解] 我们发现对于选中的边白色是从小到大的,黑色也是从小到大的, 因此我们对所有的白色边加一个权值,那么排序后做mst选取的白色边数量增减性单调, 对于增加的权值进行二分,验证能否满足要求即可. [代码] #include <cstdio> #in…
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16702    Accepted Submission(s): 3761 Problem Description A tree is a well-known data structure that is either empty (null, void, n…
Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26002   Accepted: 8879 Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edge…
Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. One day Petya encountered a tre…
Spies Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Description In the aftermath of Canada’s annexation of Pittsburgh tensions have been pretty high between Canada and the US. You have personally been hired…