#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的更多相关文章

  1. Codeforces #430 Div2 D

    #430 Div2 D 题意 给出一些数,每次操作先将所有数异或一个值,再求这些数中没有出现过的最小的非负整数. 分析 对于更新操作,对于 \(x\) 所有为 \(1\) 的位给相应层添加一个标记,当 ...

  2. Codeforces #180 div2 C Parity Game

    // Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...

  3. Codeforces #541 (Div2) - E. String Multiplication(动态规划)

    Problem   Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...

  4. Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)

    Problem   Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...

  5. Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)

    Problem   Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...

  6. Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)

    Problem   Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...

  7. 【Codeforces #312 div2 A】Lala Land and Apple Trees

    # [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...

  8. Codeforces #263 div2 解题报告

    比赛链接:http://codeforces.com/contest/462 这次比赛的时候,刚刚注冊的时候非常想好好的做一下,可是网上喝了个小酒之后.也就迷迷糊糊地看了题目,做了几题.一觉醒来发现r ...

  9. codeforces #round363 div2.C-Vacations (DP)

    题目链接:http://codeforces.com/contest/699/problem/C dp[i][j]表示第i天做事情j所得到最小的假期,j=0,1,2. #include<bits ...

随机推荐

  1. Java Integer比较

    今天看微信做了一个选择题,对Integer比较结果有点意外,题目如下: public static void main(String[] args) { Integer a = 1; Integer ...

  2. win8.1 host被删,host无法修改,host无法复制进去解决方案

    1.C:\Windows\System32\drivers\etc\hosts  复制到桌面 2.删除C:\Windows\System32\drivers\etc\hosts 3.右键编辑文本--& ...

  3. BZOJ3242 [Noi2013]快餐店 【环套树 + 单调队列dp】

    题目链接 BZOJ3242 题解 题意很清楚,找一点使得最远点最近 如果是一棵树,就是直径中点 现在套上了一个环,我们把环单独拿出来 先求出环上每个点外向树直径更新答案,并同时求出环上每个点外向的最远 ...

  4. 【bzoj3224】Tyvj 1728 普通平衡树 01Trie姿势+平衡树的四种姿势 :splay,旋转Treap,非旋转Treap,替罪羊树

    直接上代码 正所谓 人傻自带大常数 平衡树的几种姿势:  AVL Red&Black_Tree 码量爆炸,不常用:SBT 出于各种原因,不常用. 常用: Treap 旋转 基于旋转操作和随机数 ...

  5. POJ3159:Candies(差分约束)

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 39666   Accepted: 11168 题目链接:h ...

  6. scrapy新版本特性

    1:在spider中返回一个自定义的字典,老版本中需要先定义一个Item,填充后再返回一个对象 新版本中可以直接返回一个字典 2:Per-spider settings  为每个spider进行单独设 ...

  7. Astah画时序图

    Astah画时序图,666 1.生命线 时序图中表示为从对象图标向下延伸的一条虚线,表示对象存在的时间, 一般用来描述 系统 :如 客户端,代理层,缓存层,服务器层1.....服务器层N,数据库等. ...

  8. org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.oskyhang.gbd.service.UserService] found for dependency: expected at least 1 bean which qualifies as aut

    spring中一个符号的错误居然让我浪费了四五个小时才找出来,不得不给自己了两个耳光.. 由于新建项目与原来项目的目录结构有所不同,copy过来的配置文件,有些地方修改的不彻底,导致spring扫描注 ...

  9. Create a conditional DNS forwarder on our domain.com to Amazon default DNS provider

    Backgroup: I have an AWS Managed Active Directory(domain.com). I created a DHCP options set  to my d ...

  10. 转:Nginx国人开发缩略图模块(ngx_image_thumb)

    ngx_image_thumb是nginx中用来生成缩略图的模块,生存缩略图的方法很多,之前也写过一篇 <nginx生成缩略图配置>,在github上发现国人开发的一款模块,作者的文档写的 ...