[美团 CodeM 初赛 Round A]最长树链
题目大意:
给你一棵带点权的树,找出一个最长的树链满足链上点权的最大公因数不为1。
思路:
暴力DP。
对于每个点,记录一下以这个点为一个端点的所有链的最大公因数及长度。
然后暴力转移一下,时间复杂度O(n^2logn),不过中间有一些情况是可以舍掉的,做不满。
本来是想试试这样暴力能拿多少分的,没想到直接A掉了。
#include<cstdio>
#include<cctype>
#include<vector>
#include<hash_map>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int N=;
int w[N],ans;
std::vector<int> e[N];
inline void add_edge(const int &u,const int &v) {
e[u].push_back(v);
e[v].push_back(u);
}
int gcd(const int &a,const int &b) {
return b?gcd(b,a%b):a;
}
__gnu_cxx::hash_map<int,int> dfs(const int &x,const int &par) {
__gnu_cxx::hash_map<int,int> map,tmp;
map[w[x]]=;
for(unsigned i=;i<e[x].size();i++) {
const int &y=e[x][i];
if(y==par) continue;
tmp=dfs(y,x);
for(register __gnu_cxx::hash_map<int,int>::iterator i=map.begin();i!=map.end();i++) {
for(register __gnu_cxx::hash_map<int,int>::iterator j=tmp.begin();j!=tmp.end();j++) {
if(gcd(i->first,j->first)!=) {
ans=std::max(ans,i->second+j->second);
}
}
}
for(register __gnu_cxx::hash_map<int,int>::iterator i=tmp.begin();i!=tmp.end();i++) {
const int tmp=gcd(w[x],i->first);
if(tmp!=) {
map[tmp]=std::max(map[tmp],i->second+);
}
}
}
return map;
}
int main() {
const int n=getint();
for(register int i=;i<n;i++) {
add_edge(getint(),getint());
}
for(register int i=;i<=n;i++) {
w[i]=getint();
}
dfs(,);
printf("%d\n",ans);
return ;
}
[美团 CodeM 初赛 Round A]最长树链的更多相关文章
- CodeM资格赛 Round A 最长树链
按照题解的做法,对于每一个质约数分别进行讨论最长链就行 对于每一个数的质约数可是比logn还要小的 比赛的时候没人写,我也没看 = =,可惜了,不过我当时对于复杂度的把握也不大啊 #include & ...
- #6164. 「美团 CodeM 初赛 Round A」数列互质-莫队
#6164. 「美团 CodeM 初赛 Round A」数列互质 思路 : 对这个题来言,莫队可以 n*根号n 离线处理出各个数出现个的次数 ,同时可以得到每个次数出现的次数 , 但是还要处理有多少 ...
- 「美团 CodeM 初赛 Round A」最长树链
题目描述 Mr. Walker 最近在研究树,尤其是最长树链问题.现在树中的每个点都有一个值,他想在树中找出最长的链,使得这条链上对应点的值的最大公约数不等于1.请求出这条最长的树链的长度. 输入格式 ...
- 「美团 CodeM 初赛 Round A」试题泛做
最长树链 树形DP.我们发现gcd是多少其实并不重要,只要不是1就好了,此外只要有一个公共的质数就好了.计f[i][j]表示i子树内含有j因子的最长链是多少.因为一个数的不同的质因子个数是log级别的 ...
- 【填坑】loj6159. 「美团 CodeM 初赛 Round A」最长树链
水一水 枚举各个质数,把是这个数倍数的点留下,跑直径,没了 #include <bits/stdc++.h> using namespace std; int h,t,n,p,q,M,N; ...
- loj #6177. 「美团 CodeM 初赛 Round B」送外卖2 状压dp floyd
LINK:#6177.美团 送外卖2 一道比较传统的状压dp题目. 完成任务 需要知道自己在哪 已经完成的任务集合 自己已经接到的任务集合. 考虑这个dp记录什么 由于存在时间的限制 考虑记录最短时间 ...
- 【loj6177】「美团 CodeM 初赛 Round B」送外卖2 Floyd+状压dp
题目描述 一张$n$个点$m$条边的有向图,通过每条边需要消耗时间,初始为$0$时刻,可以在某个点停留.有$q$个任务,每个任务要求在$l_i$或以后时刻到$s_i$接受任务,并在$r_i$或以前时刻 ...
- [美团 CodeM 初赛 Round A]数列互质
题目大意: 给出一个长度为n的数列a1,a2,a3,...,an,以及m组询问(li,ri,ki),求区间[li,ri]中有多少数在该区间中的出现次数与ki互质. 思路: 莫队. f[i]记录数字i出 ...
- Loj #6164. 「美团 CodeM 初赛 Round A」数列互质
link : https://loj.ac/problem/6164 莫队傻题,直接容斥做. #include<bits/stdc++.h> #define maxn 100005 #de ...
随机推荐
- mysql————表类型(存储引擎)的选择
表类型(存储引擎)的选择 7.1 mysql存储引擎概述 插件式存储引擎是mysql数据库最重要的特性之一,用户可以根据应用的需要选择ruhr存储和索引数据,是否使用事务等. InnoDB和BDB提供 ...
- Python之日志操作(logging)
import logging 1.自定义日志级别,日志格式,输出位置 logging.basicConfig( level=logging.DEBUG, format='%(asctime)s | ...
- AWS CLI command example
1.list ec2 instance-id, instance status, type, ip address, name aws ec2 describe-instances --query ' ...
- CSS属性中cursor:hand
在 IE 下设置鼠标为手型的方法: cursor: hand,但是在 FIREFOX 中是无效的,解决方法是在FIREFOX中设置: cursor: pointer. 而这个pointer 值在IE和 ...
- vue双向绑定原理
要了解vue的双向绑定原理,首先得了解Object.defineProperty()方法,因为访问器属性是对象中的一种特殊属性,它不能直接在对象中设置,而必须通过 Object.definePrope ...
- org.apache.http.conn.HttpHostConnectException: Connection to xxx refused.
if you are using emulator to run your app for local server. mention the local ip as 10.0.2.2 and hav ...
- POJ1061-青蛙的约会---扩展欧几里德算法求最小整数解
扩展欧几里得算法模板 #include <cstdio> #include <cstring> #define ll long long using namespace std ...
- 【bzoj3510】首都 LCT维护子树信息(+启发式合并)
题目描述 在X星球上有N个国家,每个国家占据着X星球的一座城市.由于国家之间是敌对关系,所以不同国家的两个城市是不会有公路相连的. X星球上战乱频发,如果A国打败了B国,那么B国将永远从这个星球消失, ...
- 我在开发中所遇到的iOS7新特性以及iOS7与iOS6的适配问题总结
⓵UIImageView 1. // iOS7添加的对图像颜色处理的功能,过滤颜色的功能 2. _imageView.tintColor = [UIColor blueColor]; 3. //重 ...
- Codeforces Round #475 Div. 2 A B C D
A - Splits 题意 将一个正整数拆分成若干个正整数的和,从大到小排下来,与第一个数字相同的数字的个数为这个拆分的权重. 问\(n\)的所有拆分的不同权重可能个数. 思路 全拆成1,然后每次将2 ...