Codeforces #430 Div2 C
#430 Div2 C
题意
给出一棵带点权的树,每一个节点的答案为从当前节点到根节点路径上所有节点权值的最大公因子(在求最大共因子的时候可以选择把这条路径上的任意一点的权值置为0)。对于每一个节点单独考虑,输出最大的答案。
分析
本以为是一道树形DP,写完就 WA 了。
补题的时候呢时间复杂度很迷,写完就 T 了。
一直想着这道题可以抢救一下,还好没看题解,其实只要想想 gcd 的下降速度是很快的,然后就可以乱搞了。
在搜索的时候记录下前面是否把某个权值当做0来算了,分情况讨论下即可。
code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 100;
struct Edge {
int to, nxt;
}e[MAXN << 1];
int head[MAXN], cnt = 0;
void add(int u, int v) {
e[cnt].to = v;
e[cnt].nxt = head[u];
head[u] = cnt++;
}
int a[MAXN], dp[MAXN];
int gcd(int a, int b) {
return !b ? a : gcd(b, a % b);
}
void dfs(int fa, int u, int fac, int is) {
if(fac == 1) return;
dp[u] = max(dp[u], fac);
for(int i = head[u]; ~i; i = e[i].nxt) {
int v = e[i].to;
if(v != fa) {
int gcd_ = gcd(fac, a[v]);
if(is && gcd_ > 1) {
dfs(u, v, gcd_, 1);
} else if(!is) {
if(gcd_ < fac) {
if(gcd_ > 1) dfs(u, v, gcd_, 0); // 看似很暴力,但是注意到 gcd 的下降速度是很快的,且保证了 gcd_ < fac 。
dfs(u, v, fac, 1);
}
else dfs(u, v, fac, 0);
}
}
}
}
//适用于正整数
template <class T>
inline void scan_d(T &ret) {
char c; ret=0;
while((c=getchar())<'0'||c>'9');
while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar();
}
int main() {
memset(head, -1, sizeof head);
int n;
scan_d(n);
for(int i = 1; i <= n; i++) {
scan_d(a[i]);
dp[i] = 1;
}
for(int i = 1; i < n; i++) {
int u, v;
scan_d(u); scan_d(v);
add(u, v);
add(v, u);
}
dfs(0, 1, 0, 1);
dfs(0, 1, a[1], 0);
for(int i = 1; i <= n; i++) {
printf("%d%c", dp[i], " \n"[i == n]);
}
return 0;
}
Codeforces #430 Div2 C的更多相关文章
- Codeforces #430 Div2 D
#430 Div2 D 题意 给出一些数,每次操作先将所有数异或一个值,再求这些数中没有出现过的最小的非负整数. 分析 对于更新操作,对于 \(x\) 所有为 \(1\) 的位给相应层添加一个标记,当 ...
- Codeforces #180 div2 C Parity Game
// Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...
- Codeforces #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...
- Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)
Problem Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...
- Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
- Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)
Problem Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...
- 【Codeforces #312 div2 A】Lala Land and Apple Trees
# [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...
- Codeforces #263 div2 解题报告
比赛链接:http://codeforces.com/contest/462 这次比赛的时候,刚刚注冊的时候非常想好好的做一下,可是网上喝了个小酒之后.也就迷迷糊糊地看了题目,做了几题.一觉醒来发现r ...
- codeforces #round363 div2.C-Vacations (DP)
题目链接:http://codeforces.com/contest/699/problem/C dp[i][j]表示第i天做事情j所得到最小的假期,j=0,1,2. #include<bits ...
随机推荐
- [洛谷P1951]收费站_NOI导刊2009提高(2)
题目大意:有一张$n$个点$m$条边的图,每个点有一个权值$w_i$,有边权,询问从$S$到$T$的路径中,边权和小于$s$,且$\max\limits_{路径经过k}\{w_i\}$最小,输出这个最 ...
- Tumblr:150亿月浏览量背后的架构挑战
Tumblr:150亿月浏览量背后的架构挑战 2013/04/08 · IT技术, 开发 · 9.9K 阅读 · HBase, Tumblr, 架构 英文原文:High Scalability,编译: ...
- WebKit阅读起步
转摘自:http://my.oschina.net/myemptybottle/blog/42683 部分转摘,全文请查看原文! 我第一次看到WebKit代码中did,will前缀有点困惑,看多了才熟 ...
- [06] 盒模型 + auto 居中 + 垂直合并
1.盒模型 盒子模型有两种,分别是 ie 盒子模型和标准 w3c 盒子模型. 标准(W3C)模型中:CSS中的宽(width) = 内容 (content)的宽 CSS中的宽(width) = 内容( ...
- 给 ecplise 配置struts2配置环境
下面介绍在Eclipse中配置Struts2的过程: 一.下载Struts2,因为Struts2是开源的,百度或者google一下就可以找到下载地址.我下载的是struts-2.3.1.2版本 的,解 ...
- LABVIEW伺服电机测试平台
遇见的关键问题总结: 怎么发脉冲:(1)保持电平一段时间进行翻转(2)仿真脉冲 怎样测试脉冲数:通过检测当前时刻和前一时刻的电平是否相同(通过反馈或者移位寄存器实现)来检测脉冲跳变 通过编码器测量速度 ...
- 【Foreign】Research Rover [DP]
Research Rover Time Limit: 25 Sec Memory Limit: 256 MB Description Input Output 仅一行一个整数表示答案. Sample ...
- bzoj3223 文艺平衡树 codevs3303 翻转区间
splay模版题吧 只有区间翻转 至于为什么要把须翻转区间旋到根 因为查找一个区间可以先找出他左端点左边第一个点和右端点x右边第一个点y 然后将x旋到根节点 y旋到x的右儿子 这样x的右边的点就是所有 ...
- Nginx的主要配置参数说明
#定义Nginx运行的用户和用户组user www www; #nginx进程数,建议设置为等于CPU总核心数.worker_processes 8; #全局错误日志定义类型,[ debug | in ...
- Red-Black Tree
A red-black tree is a Binary Search Tree that satisfy the red-black tree properties: 1. Every node i ...