CF842C Ilya And The Tree
思路:
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的过程中,要注意维护全局变量。dfs到下一个分支时,要把上一个分支对全局变量造成的影响消除。
实现:
#include <bits/stdc++.h>
using namespace std;
int a[], val[];
vector<int> tree[];
int vis[];
int divisors[]; void dfs(int cur, int div)
{
vis[cur] = ;
for (int i = ; i < tree[cur].size(); i++)
{
int tmp = tree[cur][i];
if (vis[tmp]) continue;
val[tmp] = __gcd(div, a[tmp]);
dfs(tmp, val[tmp]);
}
} void dfs2(int cur, int d, vector<int>& v)
{
vis[cur] = ;
for (int i = ; i < tree[cur].size(); i++)
{
int tmp = tree[cur][i];
if (vis[tmp]) continue;
for (int j = ; j < v.size(); j++)
{
if (a[tmp] % v[j] == ) divisors[j]++;
if (divisors[j] >= d) val[tmp] = max(val[tmp], v[j]);
}
dfs2(tmp, d + , v);
for (int j = ; j < v.size(); j++)
if (a[tmp] % v[j] == ) divisors[j]--;
}
} int main()
{
int n, x, y;
while (cin >> n)
{
for (int i = ; i <= n; i++)
tree[i].clear();
for (int i = ; i <= n; i++)
cin >> a[i];
for (int i = ; i < n - ; i++)
{
cin >> x >> y;
tree[x].push_back(y);
tree[y].push_back(x);
}
int tmp = a[];
a[] = ;
memset(vis, , sizeof vis);
dfs(, );
a[] = val[] = tmp;
vector<int> v;
for (int i = ; i * i <= a[]; i++)
{
if (a[] % i == )
{
v.push_back(i);
if (a[] / i != i) v.push_back(a[] / i);
}
}
sort(v.begin(), v.end());
for (int i = ; i < v.size(); i++) divisors[i] = ;
memset(vis, , sizeof vis);
dfs2(, , v);
for (int i = ; i <= n; i++)
cout << val[i] << " ";
cout << endl;
}
return ;
}
CF842C Ilya And The Tree的更多相关文章
- 【cf842C】 Ilya And The Tree(dfs、枚举因子)
C. Ilya And The Tree 题意 给一棵树求每个点到根的路上允许修改一个为0,gcd的最大值. 题解 g是从根到当前点允许修改的最大gcd,gs为不修改的最大gcd.枚举当前点的因子,更 ...
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree
地址:http://codeforces.com/contest/842/problem/C 题目: C. Ilya And The Tree time limit per test 2 second ...
- C. Ilya And The Tree 树形dp 暴力
C. Ilya And The Tree 写法还是比较容易想到,但是这么暴力的写法不是那么的敢写. 就直接枚举了每一个点上面的点的所有的情况,对于这个点不放进去特判一下,然后排序去重提高效率. 注意d ...
- codeforces 842C Ilya And The Tree
Ilya is very fond of graphs, especially trees. During his last trip to the forest Ilya found a very ...
- Codeforces Round #430 C. Ilya And The Tree
Ilya is very fond of graphs, especially trees. During his last trip to the forest Ilya found a very ...
- C - Ilya And The Tree Codeforces Round #430 (Div. 2)
http://codeforces.com/contest/842/problem/C 树 dp 一个数的质因数有限,用set存储,去重 #include <cstdio> #includ ...
- Codeforces 842C Ilya And The Tree 树上gcd
题目链接 题意 给定一棵根为\(1\)的树.定义每个点的美丽值为根节点到它的路径上所有点的\(gcd\)值.但是对于每个点,在计算它的美丽值时,可以将这条路径上某个点的值变为\(0\)来最大化它的美丽 ...
- Ilya And The Tree CodeForces - 842C
((半个)智商题,主要难度在于实现) 题意:有一棵n个结点组成的树,其根是编号为1的结点.对于每一个结点,生成从根结点走到这个结点的路径(包括自身),选择路径上的一个点或者不选择任何点,使得其它点的最 ...
- codeforces 842C Ilya And The Tree (01背包+dfs)
(点击此处查看原题) 题目分析 题意:在一个树中,有n个结点,记为 1~n ,其中根结点编号为1,每个结点都有一个值val[i],问从根结点到各个结点的路径中所有结点的值的gcd(最大公约数)最大是多 ...
随机推荐
- 经常使用的android设计模式
一般来说,经常使用的android设计模式有下面8种:单例.工厂.观察者.代理.命令.适配器.合成.訪问者. 单例模式:目的是为了让系统中仅仅有一个调用对象,缺点是单例使其它程序过分依赖它,并且不 ...
- WebSphere报错指南
看了下面的文章,泥坑会叫我标题党,没错我就是啊. 1.was日志路径 ${WebSphere根路径}/AppServer/profiles/AppSrv01/logs/,比如说我的路径就是/opt/I ...
- 内容可编辑且随内容自增长的div
<!DOCTYPE HTML> <html> <head> <title></title> <meta http-equiv=&quo ...
- TMS320F28335项目开发记录6_28335之cmd文件具体解释
1.CMD文件的作用 CMD文件的作用就像仓库的货物摆放记录一样,为程序代码和数据分配指定的空间. 2.C语言生成的段 C语言生成的段大致分为两大类:初始化和未初始化,已初始化的段含有真正的指令和数据 ...
- please get a license from www.texturepacker.com
我们在使用texturepacker创建资源后,在使用资源时出现下述问题:please get a license from www.texturepacker.com 如图: 这个是由于我们的版本号 ...
- 【bzoj4604】The kth maximum number
暴力 #include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> ...
- 加密散列算法——SHA-1
与MD5算法类似,SHA-1算法也是在接收到消息后产生消息摘要,能够用来验证数据的完整性. 不同的是SHA1会产生一个160位的消息摘要(MD5产生的是128位). 其它的性质之类的不多说(请參考前面 ...
- su 认证失败
jiqing@ThinkPad:~$ su 密码: su:认证失败 jiqing@ThinkPad:~$ sudo passwd root [sudo] password for jiqing: 输入 ...
- [Codeforces 623A] Graph and String
[题目链接] http://codeforces.com/contest/623/problem/A [算法] 首先 , 所有与其他节点都有连边的节点需标号为'b' 然后 , 我们任选一个节点 , 将 ...
- POJ1808 平方(二次)同余方程
POJ1808 给定一个方程 x*x==a(mod p) 其中p为质数 判断是否有解 程序中 MOD_sqr()返回整数解 无解返回-1 数学证明略 #include<iostream> ...