题目大意:
  给你一棵带点权的树,找出一个最长的树链满足链上点权的最大公因数不为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]最长树链的更多相关文章

  1. CodeM资格赛 Round A 最长树链

    按照题解的做法,对于每一个质约数分别进行讨论最长链就行 对于每一个数的质约数可是比logn还要小的 比赛的时候没人写,我也没看 = =,可惜了,不过我当时对于复杂度的把握也不大啊 #include & ...

  2. #6164. 「美团 CodeM 初赛 Round A」数列互质-莫队

    #6164. 「美团 CodeM 初赛 Round A」数列互质 思路 : 对这个题来言,莫队可以 n*根号n 离线处理出各个数出现个的次数 ,同时可以得到每个次数出现的次数 , 但是还要处理有多少 ...

  3. 「美团 CodeM 初赛 Round A」最长树链

    题目描述 Mr. Walker 最近在研究树,尤其是最长树链问题.现在树中的每个点都有一个值,他想在树中找出最长的链,使得这条链上对应点的值的最大公约数不等于1.请求出这条最长的树链的长度. 输入格式 ...

  4. 「美团 CodeM 初赛 Round A」试题泛做

    最长树链 树形DP.我们发现gcd是多少其实并不重要,只要不是1就好了,此外只要有一个公共的质数就好了.计f[i][j]表示i子树内含有j因子的最长链是多少.因为一个数的不同的质因子个数是log级别的 ...

  5. 【填坑】loj6159. 「美团 CodeM 初赛 Round A」最长树链

    水一水 枚举各个质数,把是这个数倍数的点留下,跑直径,没了 #include <bits/stdc++.h> using namespace std; int h,t,n,p,q,M,N; ...

  6. loj #6177. 「美团 CodeM 初赛 Round B」送外卖2 状压dp floyd

    LINK:#6177.美团 送外卖2 一道比较传统的状压dp题目. 完成任务 需要知道自己在哪 已经完成的任务集合 自己已经接到的任务集合. 考虑这个dp记录什么 由于存在时间的限制 考虑记录最短时间 ...

  7. 【loj6177】「美团 CodeM 初赛 Round B」送外卖2 Floyd+状压dp

    题目描述 一张$n$个点$m$条边的有向图,通过每条边需要消耗时间,初始为$0$时刻,可以在某个点停留.有$q$个任务,每个任务要求在$l_i$或以后时刻到$s_i$接受任务,并在$r_i$或以前时刻 ...

  8. [美团 CodeM 初赛 Round A]数列互质

    题目大意: 给出一个长度为n的数列a1,a2,a3,...,an,以及m组询问(li,ri,ki),求区间[li,ri]中有多少数在该区间中的出现次数与ki互质. 思路: 莫队. f[i]记录数字i出 ...

  9. Loj #6164. 「美团 CodeM 初赛 Round A」数列互质

    link : https://loj.ac/problem/6164 莫队傻题,直接容斥做. #include<bits/stdc++.h> #define maxn 100005 #de ...

随机推荐

  1. codeforces803D. Magazine Ad

    D. Magazine Adtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutput ...

  2. Java之戳中痛点 - (5)switch语句break不能忘以及default不同位置的用法

    先看一段代码: public class Test{ public static void main(String[] args){ System.)); } } public static Stri ...

  3. 使用vue做移动app时,调用摄像头扫描二维码

    现在前端技术发展飞快,前端都能做app了,那么项目中,也会遇到调用安卓手机基层的一些功能,比如调用摄像头,完成扫描二维码功能 下面我就为大家讲解一下,我在项目中调用这功能的过程. 首先我们需要一个中间 ...

  4. canvas知识01

    本文转自:http://www.cnblogs.com/jsdarkhorse/archive/2012/06/29/2568451.html 更多参考:http://www.cnblogs.com/ ...

  5. centos yum 安装 mysql

      centos7下使用yum安装mysql 时间:2015-03-07 21:26:20      阅读:87445      评论:0      收藏:1      [点我收藏+] 标签: Cen ...

  6. MDIO/MDC(SMI)接口-leonwang202

    ChinaUnix博客 http://blog.chinaunix.net/uid-24148050-id-132863.html

  7. bzoj 5010: [Fjoi2017]矩阵填数

    Description 给定一个 h*w 的矩阵,矩阵的行编号从上到下依次为 1..h,列编号从左到右依次1..w.在这个矩阵中你需要在每 个格子中填入 1..m 中的某个数.给这个矩阵填数的时候有一 ...

  8. Codeforces Round #391 A B C D E

    A. Gotta Catch Em' All! 题意 从给定的字符串中选取字符,问可构成多少个\(Bulbasaur\) // 想到柯南里一些从报纸上剪汉字拼成的恐吓信_(:з」∠)_ Code #i ...

  9. java中 快捷键输入System.out.println();

    syso 然后:alt+ /(就是问号键)

  10. JSOI2016酱油记

    高一,第一次参加NOIP. 后悔初中没有报过名...唉,后悔也来不及了. 不知道自己一个暑假干了什么...算法没学多少,脑子倒是越来越不好使了. 过了初赛,数周后一脸茫(meng)然(bi)地去考场. ...