#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. 【转】C#获取当前路径7种方法

    webformvar s = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; //C盘 IIS路径 var s1 ...

  2. bzoj 1878

    莫队乱搞的第一题,(感觉这个算法初学的时候就能想到啊) 总之就是离线,然后扫一遍然后回答,用数组记录状态 但还是有一个地方不太明白 为什么要除siz?这样为什么会优化复杂度呢?? bool cmp(c ...

  3. CentOS 6通过yum升级Git

    By francis_hao    Mar 9,2017   在一个新机器上推送代码到github上时出现了下面的问题 error: The requested URL returned error: ...

  4. POJ2699:The Maximum Number of Strong Kings(枚举+贪心+最大流)

    The Maximum Number of Strong Kings Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2488 ...

  5. Vue2.0关于生命周期和钩子函数

    Vue生命周期简介:   Vue1.0+和Vue2.0在生命周期钩子上的区别还是很大的,如下:   代码验证: <!DOCTYPE html> <html> <head& ...

  6. HDU 5881--Tea 思维规律

    感谢http://blog.csdn.net/black_miracle/article/details/52567718 题意:有一壶水, 体积在 L和 R之间, 有两个杯子, 你要把水倒到两个杯子 ...

  7. php多虚拟主机配置

    一.配置httpd.conf# Virtual hosts#Include conf/extra/httpd-vhosts.conf       //取消这一行的# 二.配置httpd-vhosts. ...

  8. 403 Forbidden 错误

    Forbidden You don't have permission to access /a.php on this server. apache昨天调试 httpd.conf 文件:<Di ...

  9. 「6月雅礼集训 2017 Day2」C

    [题目大意] 有一棵n个点的完全二叉树,边权均为1,每个点有小鸟容量c[i] 依次来了m只小鸟,第i只小鸟初始位置在pos[i]上,问来了x只小鸟的时候,怎样安排小鸟的路线可以使得小鸟移动的边权和最小 ...

  10. linux基础——文件的压缩解压缩以及vim编辑

       一.将用户信息数据库文件和组信息数据库文件纵向合并为一个文件/1.txt(覆盖) cat  /etc/{passwd,group} > /1.txt  查看:cat /1.txt   二. ...