CF EDU 1101D GCD Counting 树形DP + 质因子分解
题意
有一颗树,每个节点有一个值,问树上最长链的长度,要求链上的每个节点的GCD值大于1。
思路
由于每个数的质因子很少,题目的数据200000<2*3*5*7*11*13*17=510510。所以每个节点的质因子个数不多。那么树形DP的时候直接枚举每种因子即可。
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000") //c++
// #pragma GCC diagnostic error "-std=c++11"
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c);
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //18
// const int mod = 998244353;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
}
/*-----------------------showtime----------------------*/ const int maxn = 2e5+;
int a[maxn]; vector<int>mp[maxn];
int vis[maxn];
int dp[maxn];
int ans = ;
vector<int>p[maxn],t[maxn]; void dfs(int u, int fa){
for(int i=; i<mp[u].size(); i++){
int v = mp[u][i];
if(v == fa)continue;
dfs(v,u); for(int j=; j<p[u].size(); j++){
for(int k=; k<p[v].size(); k++){
if(p[u][j] == p[v][k]){
ans = max(ans, t[u][j] + t[v][k]);
t[u][j] = max(t[u][j], t[v][k] + );
}
}
}
}
if(a[u] > ) ans = max(ans, );
} int main(){
int n; scanf("%d", &n);
int flag = ;
for(int i=; i<=n; i++) {
scanf("%d", &a[i]);
int x = a[i];
for(ll j=; j*j <=x; j++){
if(x%j == ){
p[i].pb(j);
t[i].pb();
while(x%j==) x/=j;
}
}
if(x > ){
p[i].pb(x);
t[i].pb();
}
if(a[i] > ) flag = ;
} if(flag) {
puts("");
return ;
}
for(int i=; i<n; i++){
int u,v;
scanf("%d%d", &u, &v);
mp[u].pb(v);
mp[v].pb(u);
} dfs(,-);
printf("%d\n", ans);
return ;
}
CF EDU 1101D GCD Counting 树形DP + 质因子分解的更多相关文章
- CF 337D Book of Evil 树形DP 好题
Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area contains n se ...
- CF 461B Appleman and Tree 树形DP
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other ...
- CF 161D Distance in Tree 树形DP
一棵树,边长都是1,问这棵树有多少点对的距离刚好为k 令tree(i)表示以i为根的子树 dp[i][j][1]:在tree(i)中,经过节点i,长度为j,其中一个端点为i的路径的个数dp[i][j] ...
- CF 219D 树形DP
CF 219D [题目链接]CF 219D [题目类型]树形DP &题意: 给一个n节点的有向无环图,要找一个这样的点:该点到其它n-1要逆转的道路最少,(边<u,v>,如果v要到 ...
- cf842C 树形dp+gcd函数
树形dp用一下就好了 /* dp[i]表示不删节点的gcd值 每个结点开个vector用来存储删一个点之后的最大值 然后排序 去重 */ #include<bits/stdc++.h> # ...
- CF 486D vailid set 树形DP
As you know, an undirected connected graph with n nodes and n - 1 edges is called a tree. You are gi ...
- CF 219D Choosing Capital for Treeland 树形DP 好题
一个国家,有n座城市,编号为1~n,有n-1条有向边 如果不考虑边的有向性,这n个城市刚好构成一棵树 现在国王要在这n个城市中选择一个作为首都 要求:从首都可以到达这个国家的任何一个城市(边是有向的) ...
- CF 463A && 463B 贪心 && 463C 霍夫曼树 && 463D 树形dp && 463E 线段树
http://codeforces.com/contest/462 A:Appleman and Easy Task 要求是否全部的字符都挨着偶数个'o' #include <cstdio> ...
- CF F - Tree with Maximum Cost (树形DP)给出你一颗带点权的树,dist(i, j)的值为节点i到j的距离乘上节点j的权值,让你任意找一个节点v,使得dist(v, i) (1 < i < n)的和最大。输出最大的值。
题目意思: 给出你一颗带点权的树,dist(i, j)的值为节点i到j的距离乘上节点j的权值,让你任意找一个节点v,使得dist(v, i) (1 < i < n)的和最大.输出最大的值. ...
随机推荐
- 一个项目中:只能存在一个 WebMvcConfigurationSupport (静态文件失效之坑)
一个项目中:只能存在一个 WebMvcConfigurationSupport 在一个项目中WebMvcConfigurationSupport只能存在一个,多个的时候,只有一个会生效. 静态文件访问 ...
- 并发栅栏CyclicBarrier---简单问2
并发栅栏CyclicBarrier---简单问 背景:前几天在网上看到关于Java并发包java.concurrent中一个连环炮的面试题,整理下以备不时之需. CyclicBarrier简介: 栅栏 ...
- 隐马尔科夫模型HMM介绍
马尔科夫链是描述状态转换的随机过程,该过程具备“无记忆”的性质:即当前时刻$t$的状态$s_t$的概率分布只由前一时刻$t-1$的状态$s_{t-1}$决定,与时间序列中$t-1$时刻之前的状态无关. ...
- jenkins未授权访问漏洞
jenkins未授权访问漏洞 一.漏洞描述 未授权访问管理控制台,可以通过脚本命令行执行系统命令.通过该漏洞,可以后台管理服务,通过脚本命令行功能执行系统命令,如反弹shell,wget写webshe ...
- 『开发技术』Windows极简安装使用face_recognition
face_recognition是一个强大.简单.易上手的人脸识别开源项目,并且配备了完整的开发文档和应用案例,特别是兼容树莓派系统.此项目是世界上最简洁的人脸识别库,你可以使用Python和命令行工 ...
- [Abp vNext 源码分析] - 7. 权限与验证
一.简要说明 在上篇文章里面,我们在 ApplicationService 当中看到了权限检测代码,通过注入 IAuthorizationService 就可以实现权限检测.不过跳转到源码才发现,这个 ...
- Unity基础之:UnityAPI的学习
版权声明: 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客&qu ...
- 利用MAVEN打包可运行jar包,包括依赖的第三方包
转载自:http://bglmmz.iteye.com/blog/2058914 背景: 另一篇文章说了如何利用IDEA来打包,现在来说说如何利用MAVEN打包 目标:应用本身打成一个jar包,依赖的 ...
- Docker最简单入门之(一)——介绍和配置Docker
0. 前言 最近学完了Dokcer,特别记录一下,算是对自己学习成果的一个总结.以便自己能够更好的理解Docker.粗略估计了一下,我大概会分成4个部分,只记录一下常用的操作,至于一些比较难的操作或者 ...
- 如何用Hexo+Github创建自己的技术博客
注册一个github GitHub官网.按照一般的网站注册登录执行就好了,不详细说. 安装git 安装很简单,一直下一步 git安装教程 很多教程里都说要配置环境变量,我本人安装过5次左右的git,一 ...