Codeforces 842 C Ilya And The Tree
来源:互联网 发布:电脑磁盘清理软件 编辑:程序博客网 时间:2024/06/06 20:08
题目地址
题意:给出一棵生成树,每个节点都有一个值,现在要求出每个节点的美丽值的最大值,美丽值的定义为从根节点到该节点(包含)路径上所有点的值的gcd,求解每个点时可以把路径上某一个点的值变为0(就相当于删除这个节点的数)。你可以认为每个点美丽值的求解是独立的(每次删除的点都不会影响下一次)。
思路:在这个树上进行dfs,然后我们边记录结果,在dp数组中记录的是到这个点的这条路径中一个节点都不删的美丽值,然而vector中记录的是删除了一个节点的全部情况,第一个就是删除看当前节点(因为他的值是上一个节点的dp数组中的值),然后就是上一个节点的vector中的值与当前节点的值得gcd(因为这个记录的是原来已经删除了一个节点的gcd现在更新一下),最后每个节点取最大值就好了。
#include <iostream>#include <cstring>#include <string>#include <queue>#include <vector>#include <map>#include <set>#include <stack>#include <cmath>#include <cstdio>#include <algorithm>#include <iomanip>#define N 200010#define M 400010#define LL __int64#define inf 0x3f3f3f3f#define lson l,mid,ans<<1#define rson mid+1,r,ans<<1|1#define getMid (l+r)>>1#define movel ans<<1#define mover ans<<1|1using namespace std;const LL mod = 1000000007;const int L = 200;struct node { int to, next;}edge[M];int head[N], cnt, num[N], dp[N], n;//dp存的是没有删除数的结果,v中存储删除数的结果vector<int> v[N];void init() { for (int i = 0; i <= n; i++) { v[i].clear(); } cnt = 0; memset(head, -1, sizeof(head));}void add(int a, int b) { edge[cnt].to = b; edge[cnt].next = head[a]; head[a] = cnt++; edge[cnt].to = a; edge[cnt].next = head[b]; head[b] = cnt++;}int gcd(int a, int b) { return b ? gcd(b, a%b) : a;}void dfs(int u, int pre) { for (int i = head[u]; i != -1; i = edge[i].next) { int to = edge[i].to; if (to == pre) { continue; } dp[to] = gcd(dp[u], num[to]); v[to].push_back(dp[u]); for (int i = 0; i < v[u].size(); i++) { v[to].push_back(gcd(v[u][i], num[to])); } sort(v[to].begin(), v[to].end()); v[to].erase(unique(v[to].begin(), v[to].end()), v[to].end()); dfs(to, u); }}int main() { cin.sync_with_stdio(false); int a, b; while (cin >> n) { init(); for (int i = 1; i <= n; i++) { cin >> num[i]; } for (int i = 1; i < n; i++) { cin >> a >> b; add(a, b); } dp[1] = num[1]; v[1].push_back(0); dfs(1, -1); for (int i = 1; i <= n; i++) { dp[i] = max(dp[i], v[i].back()); } for (int i = 1; i < n; i++) { cout << dp[i] << " "; } cout << dp[n] << endl; } return 0;}
阅读全文
0 0
- Codeforces 842C Ilya And The Tree
- Codeforces 842 C Ilya And The Tree
- Codeforces 842 C. Ilya And The Tree (dfs)
- Codeforces 842C Ilya And The Tree【Dfs】
- Codeforces 842 C. Ilya And The Tree 树上dp
- codeforces 842C. Ilya And The Tree(dfs)
- Codeforces 842 C. Ilya And The Tree (dfs)
- 842C Ilya And The Tree
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree
- Codeforces 430 Div 2 C.Ilya And The tree
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree
- Codeforces 842C Ilya And The Tree(树上DP+因子个数估计)
- Codeforces Round #430 (Div. 2) 842C Ilya And The Tree(暴力)
- Ilya And The Tree
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree(dfs)
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree(dfs+最大公约数+因子+树)
- Codeforces Round #430 (Div. 2)C. Ilya And The Tree dfs D
- Codeforces Round #430(Div.2)Problem C Ilya And The Tree(DFS)
- 夜灵的Html笔记Day05——CSS样式继承、基础选择器
- dns
- Day15-48.Cleanup/finalization and garbage collection
- leetcode 29. Divide Two Integers
- Laravel5.4 下nginx配置
- Codeforces 842 C Ilya And The Tree
- CharSquence 接口的作用,多态以增强String
- Centos6.5 安装(更新)python3.5 和pip3.5,easy_install3.5问题
- ES6编程风格---学习阮一峰ES6入门 笔记
- [Unity3d插件KGFMapSystem]非常不错的小地图的制作
- 编译spark方法,及编译、案例测试问题总结
- Scratch编程初体验-小猫跳舞
- Maven学习资料
- python json.dumps() 与 json.dump()