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 ...
随机推荐
- P1712 [NOI2016]区间
题目描述 在数轴上有 NN 个闭区间 [l_1,r_1],[l_2,r_2],...,[l_n,r_n][l1,r1],[l2,r2],...,[ln,rn] .现在要从中选出 MM 个区 ...
- CSS3不遥远,几个特性你要知道
CSS是众所周知且应用广泛的网站样式语言,在它的版本三(CSS3)计划中,新增了一些能够节省时间的特性.尽管只有当前最新了浏览器版本才能支持这些 效果,但了解它们还是必须且很有趣味性的.CSS3中的5 ...
- C语言的getopt
By francis_hao Jul 5,2017 getopt:分析命令行选项 概述 #include <unistd.h>int getopt(int argc, char ...
- bulk_insert_buffer_size and InnoDB
Q: I read the following on this page http://dev.mysql.com/doc/mysql/en/server-system-variables.html ...
- [HNOI2003]消防局的设立 (贪心)
[HNOI2003]消防局的设立 题目描述 2020年,人类在火星上建立了一个庞大的基地群,总共有n个基地.起初为了节约材料,人类只修建了n-1条道路来连接这些基地,并且每两个基地都能够通过道路到达, ...
- 教主泡嫦娥(RQNOJ 595)
题目描述 [问题背景] 2012年12月21日下午3点14分35秒,全世界各国的总统以及领导人都已经汇聚在中国的方舟上. 但也有很多百姓平民想搭乘方舟,毕竟他们不想就这么离开世界,所以他们决定要么登上 ...
- #error#错误原因:Cannot find executable for CFBundle 0x8ad60b0 (not loaded)
#error#错误原因:Cannot find executable for CFBundle 0x8ad60b0 </Applications/Xcode.app/Contents/Devel ...
- windows支持applocker的版本
Operating system requirements The following table show the on which operating systems AppLocker fe ...
- kernel_read【转】
转自:http://blog.csdn.net/echoisland/article/details/7101097http://lxr.oss.org.cn/source/fs/exec.c 798 ...
- Linux内核设计与实现读书笔记(8)-内核同步方法【转】
转自:http://blog.chinaunix.net/uid-10469829-id-2953001.html 1.原子操作可以保证指令以原子的方式执行——执行过程不被打断.内核提供了两组原子操作 ...