CodeM资格赛 Round A 最长树链
按照题解的做法,对于每一个质约数分别进行讨论最长链就行
对于每一个数的质约数可是比logn还要小的
比赛的时候没人写,我也没看 = =,可惜了,不过我当时对于复杂度的把握也不大啊
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <queue>
#include <cmath>
using namespace std;
typedef long long ll;
#define MP(x, y) make_pair(x, y)
#define lson l,m, rt<<1
#define rson m+1, r, rt<<1|1
const int N = 1e5+5;
const int INF = 0x3f3f3f3f;
struct Node{
int to, nx;
}E[N*2];
int head[N], tot;
void add(int fr, int to) {
E[tot].to = to; E[tot].nx = head[fr]; head[fr] = tot ++;
}
int vis[N]; int nw; int divisor;
int val[N];
int Prime[N];
int Isprime[N]; int cnt;
int dis[N];
int ans;
map<int, vector<int> > mp;
map<int, vector<int> > ::iterator it;
void dfs(int x) {
dis[x] = 1;
int maxx = 0, Maxx = 0;
for(int i = head[x]; ~i; i = E[i].nx) {
int y = E[i].to;
if(vis[y] == nw || val[y] % divisor) continue;
vis[y] = nw;
dfs(y);
if(dis[y] > Maxx) maxx = Maxx, Maxx = dis[y];
else if(dis[y] > maxx) maxx = dis[y];
}
ans = max(maxx + Maxx + 1, ans);
dis[x] = Maxx + 1;
}
void solve(int x) {
divisor = x;
// printf("%d: ",x); for(int i = 0; i < mp[x].size(); ++i) printf("%d ", mp[x][i]); printf("\n");
++nw;
for(int i = 0; i < mp[x].size(); ++i) {
int y = mp[x][i];
if(vis[y] != nw) {
vis[y] = nw;
dfs(y);
// printf("hh\n");
}
}
}
int main() {
int n;
cnt = 0;
for(int i = 2; i < N; ++i) {
if(!Prime[i]) {
Isprime[++cnt] = i;
for(int j = 2*i; j < N; j += i) {
Prime[j] ++;
}
}
}
// for(int i = 1; i <= 10; ++i) printf("%d ", Isprime[i]); printf("\n");
while(~scanf("%d", &n)) {
mp.clear();
nw = 0; memset(vis, 0, sizeof(vis));
memset(head, -1, sizeof(head)); tot = 0;
for(int i = 1; i < n; ++i) {
int a, b; scanf("%d %d", &a, &b);
add(a, b); add(b, a);
}
for(int i = 1; i <= n; ++i) {
scanf("%d", &val[i]);
int tt = val[i];
// printf("%d ", val[i]);
for(int j = 1; 1ll*Isprime[j] * Isprime[j] <= tt; ++j) {
if(tt % Isprime[j] == 0) {
mp[Isprime[j]].push_back(i);
while(tt % Isprime[j] == 0) {
tt /= Isprime[j];
}
}
}
if(tt != 1) {
mp[tt].push_back(i);
}
}
// printf("hh\n");
ans = -1;
for(it = mp.begin(); it != mp.end(); ++it) {
solve(it->first);
}
printf("%d\n", ans);
}
return 0;
}
CodeM资格赛 Round A 最长树链的更多相关文章
- [美团 CodeM 初赛 Round A]最长树链
题目大意: 给你一棵带点权的树,找出一个最长的树链满足链上点权的最大公因数不为1. 思路: 暴力DP. 对于每个点,记录一下以这个点为一个端点的所有链的最大公因数及长度. 然后暴力转移一下,时间复杂度 ...
- 「美团 CodeM 初赛 Round A」最长树链
题目描述 Mr. Walker 最近在研究树,尤其是最长树链问题.现在树中的每个点都有一个值,他想在树中找出最长的链,使得这条链上对应点的值的最大公约数不等于1.请求出这条最长的树链的长度. 输入格式 ...
- newcoder-最长树链-树/gcd
https://ac.nowcoder.com/acm/problem/13233 链接:https://ac.nowcoder.com/acm/problem/13233来源:牛客网 题目描述 树链 ...
- 【填坑】loj6159. 「美团 CodeM 初赛 Round A」最长树链
水一水 枚举各个质数,把是这个数倍数的点留下,跑直径,没了 #include <bits/stdc++.h> using namespace std; int h,t,n,p,q,M,N; ...
- [LOJ 6159] 最长树链
看到要求gcd不为1所以肯定在这条答案链上都是一个质数的倍数,所以就会产生一个很暴力的想法 没错,正解就是这样的暴力 只让走是i(素数)倍数的点,作最长链 最长链可以树形dp或两遍bfs,一遍找端点, ...
- BZOJ 3531: [Sdoi2014]旅行 [树链剖分]
3531: [Sdoi2014]旅行 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 1685 Solved: 751[Submit][Status] ...
- 「美团 CodeM 初赛 Round A」试题泛做
最长树链 树形DP.我们发现gcd是多少其实并不重要,只要不是1就好了,此外只要有一个公共的质数就好了.计f[i][j]表示i子树内含有j因子的最长链是多少.因为一个数的不同的质因子个数是log级别的 ...
- 从lca到树链剖分 bestcoder round#45 1003
bestcoder round#45 1003 题,给定两个点,要我们求这两个点的树上路径所经过的点的权值是否出现过奇数次.如果是一般人,那么就是用lca求树上路径,然后判断是否出现过奇数次(用异或) ...
- 树链剖分-点的分治(点数为k且距离最长的点对)
hdu4871 Shortest-path tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 130712/130712 K ( ...
随机推荐
- Spring mybatis源码学习指引目录
前言: 分析了很多方面的mybatis的源码以及与spring结合的源码,但是难免出现错综的现象,为了使源码陶冶更为有序化.清晰化,特作此随笔归纳下分析过的内容.博主也为mybatis官方提供过pul ...
- Effective Java 第三版——32.合理地结合泛型和可变参数
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
- 一个Windows下线程池的实现(C++)
前言 本文配套代码:https://github.com/TTGuoying/ThreadPool 先看看几个概念: 线程:进程中负责执行的执行单元.一个进程中至少有一个线程. 多线程:一个进程中有多 ...
- request、response的setCharacterEncoding与response的setContentType
一.request中的setCharacterEncoding方法:作用是用指定的编码集去覆盖request对象中的默认的"ISO-8859-1"编码集,如"UTF-8& ...
- 实时滚动图表绘制方法: LightningChart教程 + 源码下载
LightningChart图形控件彻底发挥了GPU加速和性能优化的最大效应,能够实时呈现超过10亿数据点的庞大数据,为大家提供先进与快速的图表库.这里的实时图实现的比较简单,大家先试一下这个效果,熟 ...
- Linux 快速执行历史命令,用 !编号
例如: history | grep mysql 使用 !914 可以快速执行上述命令:
- 940C Phone Numbers
传送门 题目大意 给你两个数字n和k,给你一个字符串s,n是s的长度,求字母集合是s的字母集合子集的字典序大于s的长度为k的字典序最小的字符串t 分析 将字符转化为数字,然后分两种情况处理: 1.n& ...
- angular2^ typescript 将 文件和Json数据 合并发送到服务器(2.服务端)
nodejs 中使用框架 express web框架 multer 文件接受 直接贴代码了,我就不解释了 "use strict"; exports.__esModule = tr ...
- springmvc log4j 配置
web.xml 增加 <context-param> <param-name>log4jConfigLocation</param-name> <param- ...
- 读书简记-java与模式