http://codeforces.com/contest/463/problem/E

给出一个总节点数量为n的树,每个节点有权值,进行q次操作,每次操作有两种选项:

1. 询问节点v到root之间的路径上的各个节点,求满足条件 gcd(val[i], val[v]) > 1 的 距离v最近的节点的下标。

2. 将节点v的值求改为w。

暴力居然过了!

#include <iostream>

#include <cstdio>

#include <cstring>

#include <cmath>

#include <algorithm>

#include <stack>

#include <queue>

#include <string>

#include <vector>

#include <set>

#include <map>

#include <cassert>

using namespace std;

#define RD(x) scanf("%d",&x)

#define RD2(x,y) scanf("%d%d",&x,&y)

#define clr0(x) memset(x,0,sizeof(x))

typedef long long LL;

const int maxn = 400005;

struct edge{

int next,to;

}e[maxn];

int head[maxn],n,q,w[maxn],cntn,f[maxn];

void add(int u,int v)

{

e[cntn] = (edge){head[u],v};

head[u] = cntn++;

e[cntn] = (edge){head[v],u};

head[v] = cntn++;

}

int gcd(int x,int y)

{

return y == 0 ? x:gcd(y,x%y);

}

void dfs(int u,int fa)

{

f[u] = fa;

for(int i = head[u];i != -1;i = e[i].next){

int v = e[i].to;

if(v == fa) continue;

dfs(v,u);

}

}

int find_gcd(int v)

{

int u = f[v];

while(u != -1){

int res = gcd(w[v],w[u]);

if(res > 1){

return u;

}

u = f[u];

}

return -1;

}

int main() {

RD2(n,q);

for(int i = 1;i <= n;++i)

RD(w[i]);

int m = n - 1,u,v,ww;

memset(head,-1,sizeof(head)),clr0(f);

while(m--){

RD2(u,v);

add(u,v);

}

dfs(1,-1);

while(q--){

RD2(u,v);

if(u == 1){

printf("%d\n",find_gcd(v));

}

else{

RD(ww);

w[v] = ww;

}

}

return 0;

}

最新文章

  1. HackerRank-Longest Subarray
  2. 【原创】刚刚发现的SVN的几个有用的功能
  3. 关于Comparable与Comparator
  4. Android JNI 和 NDK
  5. pwd, cd, ls, touch, mkdir, rmdir, rm
  6. bash 操作 sqlite3
  7. 去块率波 Deblocking filter
  8. Netty4.x中文教程系列(二) Hello World !
  9. Hibernate的配置
  10. css常用伪类记录
  11. hadoop深入研究:(七)——压缩
  12. 创建 OVS vlan100 netwrok - 每天5分钟玩转 OpenStack(137)
  13. SpringMVC @ResponseStatus 的用法
  14. Eureka学习例子
  15. 【线段树】Bzoj1798 [AHOI2009] 维护序列
  16. 浅谈OA系统与Portal门户的区别
  17. 【XSY3139】预言家 数位DP NFA
  18. SQL SERVER数据库级的触发器
  19. openstack网络
  20. javascript删除数组,索引出现问题解决办法。

热门文章

  1. angular2.0学习笔记1.开发环境搭建 (node.js和npm的安装)
  2. MySQL8.0的安装与配置(Windows 10)
  3. Python之路番外(第三篇):Pycharm的使用秘籍
  4. 【转】Linq表达式、Lambda表达式你更喜欢哪个?
  5. 几种开源的TCP/IP协议栈分析
  6. 选数(NOIP2002)
  7. PHP字符串位置相关的函数
  8. 43.Charles抓包(iOS的http/https请求)
  9. 2018.11.01 NOIP训练 梭哈(模拟)
  10. 安装了nodejs后在命令行运行npm报错:Error: Cannot find module &#39;internal/util/types&#39;