想到枚举m个点,然后求最小生成树,ratio即为最小生成树的边权/总的点权.但是怎么枚举这m个点,实在不会.网上查了一下大牛们的解法,用dfs枚举,没想到dfs还有这么个作用. 参考链接:http://blog.csdn.net/xingyeyongheng/article/details/9373271 #include <stdio.h> #include <string.h> #include <set> #include <vector> #incl…
Minimal Ratio Tree Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 12   Accepted Submission(s) : 7 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description For a tree, which n…
思路: 1. 如果根节点是0,那么可以通过一次dfs计算出所有节点的最大值. 2. 如果根节点不是0,那么其余各点的最大值一定是根节点的一个因子.首先计算出根节点的所有因子.在dfs到一个深度为d的节点v时,遍历所有因子:对于因子x,p表示从根节点到达v的路径中能够被x整除的节点个数.如果p >= d - 1,则x就是当前节点的一个候选最大值(d-1是因为可以把路径中其中一个数替换为0). 于是通过两次dfs即可得出答案.复杂度O(n * sqrt(n)).在dfs的过程中,要注意维护全局变量.…
#include <stdio.h> #include <set> #include <string.h> #include <algorithm> using namespace std; ; ; double minans; int vis[maxn];//记录选中的点 int mp[maxn][maxn], ans[maxn][maxn]; int min_road[maxn]; int Prim(int n) { ; int dis[maxn]; i…
C. Ilya And The Tree 题意 给一棵树求每个点到根的路上允许修改一个为0,gcd的最大值. 题解 g是从根到当前点允许修改的最大gcd,gs为不修改的最大gcd.枚举当前点的因子,更新路径上每个因子出现次数,回溯时减去.并用这个因子更新答案.另外当前点修改为0时,还要用父节点的gs更新答案.复杂度\(O(n\sqrt n)\) 代码 const int N=201000; int n; int a[N]; int u,v; VI e[N]; int g[N],gs[N]; in…
C. Ilya And The Tree 写法还是比较容易想到,但是这么暴力的写法不是那么的敢写. 就直接枚举了每一个点上面的点的所有的情况,对于这个点不放进去特判一下,然后排序去重提高效率. 注意dp[v]一开始存的是从根节点到这个节点都选的情况,这样才好往后转移. #include <iostream> #include <cstdio> #include <algorithm> #include <vector> #include <cstrin…
题意:给出a和b的gcd和lcm,让你求a和b.按升序输出a和b.若有多组满足条件的a和b,那么输出a+b最小的.思路:lcm=a*b/gcd   lcm/gcd=a/gcd*b/gcd 可知a/gcd与b/gcd互质,由此我们可以先用Pollard_rho法对lcm/gcd进行整数分解, 然后对其因子进行深搜找出符合条件的两个互质的因数,然后再都乘以gcd即为输出答案. #include <iostream> #include <stdio.h> #include <alg…
地址:http://codeforces.com/contest/842/problem/C 题目: C. Ilya And The Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Ilya is very fond of graphs, especially trees. During his last trip…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2489 Problem Description For a tree, which nodes and edges are all weighted, the ratio of it is calculated according to the following equation. Given a complete graph of n nodes with all nodes and edges…
原题链接:http://codeforces.com/contest/842/problem/C 题意:一个以1为根节点的树,每个节点有一个值ai,定义美丽度:从根节点到这个节点的路径上所有ai的gcd,即gcd(a1,a2,a5...ai),对每个节点的美丽度,我们可以使根到这个节点的路径上一个点的ai值变为0.求所有点的最大美丽度. 思路:先求出没有节点变为0的情况g[i],在此基础上,如果使x节点变为0,那么它的美丽度为父节点的g[i]:假设让其他在路径上的点变为0,可以知道x节点的最大美…