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;

}

Codeforces Round #264 (Div. 2) E. Caisa and Tree 树上操作暴力的更多相关文章

  1. Codeforces Round #319 (Div. 1) B. Invariance of Tree 构造

    B. Invariance of Tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/576/ ...

  2. Codeforces Round #264 (Div. 2)

    http://codeforces.com/contest/463 这场是我人生第一场cf啊.. 悲剧处处是啊. 首先,看不懂题,完全理解不了啊.都是wa了好几次才过的 所以a和b这两sb题我做了1个 ...

  3. Codeforces Round #264 (Div. 2) C

    题目: C. Gargari and Bishops time limit per test 3 seconds memory limit per test 256 megabytes input s ...

  4. Codeforces Round #264 (Div. 2) D. Gargari and Permutations 多序列LIS+dp好题

    http://codeforces.com/contest/463/problem/D 求k个序列的最长公共子序列. k<=5 肯定 不能直接LCS 网上题解全是图论解法...我就来个dp的解法 ...

  5. Codeforces Round #264 (Div. 2) C. Gargari and Bishops 主教攻击

    http://codeforces.com/contest/463/problem/C 在一个n∗n的国际象棋的棋盘上放两个主教,要求不能有位置同时被两个主教攻击到,然后被一个主教攻击到的位置上获得得 ...

  6. Codeforces Round #264 (Div. 2) C Gargari and Bishops 【暴力】

    称号: 意甲冠军:给定一个矩阵,每格我们有一个数,然后把两个大象,我希望能够吃的对角线上的所有数字.我问两个最大的大象可以吃值. 分析:这种想法是暴力的主题,计算出每一格放象的话能得到多少钱,然后求出 ...

  7. Codeforces Round #264 (Div. 2) D

    题意: 给出最多5个序列,问这几个序列的最长公共子序列的长度是多少. solution : 脑抽级别我是,第一个序列每个数字位置固定,这样只要维护一个k-1维的偏序集就好了.然后在保证每个位置合法的情 ...

  8. Codeforces Round #379 (Div. 2) E. Anton and Tree 缩点 直径

    E. Anton and Tree 题目连接: http://codeforces.com/contest/734/problem/E Description Anton is growing a t ...

  9. Codeforces Round #339 (Div. 2) A. Link/Cut Tree 水题

    A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostis ...

随机推荐

  1. hdu 5129 (枚举) The E-pang Palace

    题目;http://acm.hdu.edu.cn/showproblem.php?pid=5128. 给你n个点,问能否组成两个不相交的与坐标轴平行的矩形,能就输出两矩形的面积和,不能就输出一个字符串 ...

  2. Android.HowToDefineCustomView

    Custom View Errors E1 在使用自定义CustomView时,出现以下runtime error: Android.View.InflateException: Binary XML ...

  3. Luogu 2051[AHOI2009]中国象棋 - DP

    Description 在 $n * m$ 的格子上放若干个炮, 使得每个炮都不能攻击到其他炮 Solution 定义数组f[ i ][ j ][ k ] 表示到了第 i 行, 已经有2个炮的列数为 ...

  4. 性能监控工具——Cacti安装文档

    一.Cacti安装说明 1.安装说明 一般性的安装说明,详细的操作系统具体的安装说明可用于Linux. 2.服务器安装要求 RRDTool 1.2.x或更高版本 MySQL 4.1.x或5.x更高版本 ...

  5. C++ 的虚析构函数

    当一个基类的指针指向一个派生类的对象,并用该基类的指针去删除或者析构派生类对象时,如果基类的析构函数不是声明为虚函数,那么在析构时基类的析构函数将会被直接调用,派生类的析构函数应为没被调用而导致内存泄 ...

  6. VisualStudio2017密钥(key)我随便输入一下居然通过了?????????!!!!!!!!!!!!

    VisualStudio2017密钥(key) 2017年12月30日 14:41:38 阅读数:5048 professional:KBJFW-NXHK6-W4WJM-CRMQB-G3CDH ent ...

  7. js实现环形菜单效果

    点击中间的圆点,会弹出环形菜单,效果图: 代码: <!DOCTYPE html> <html> <head> <meta charset="UTF- ...

  8. 通过SD卡来安装Linux系统

    一.制作SD启动卡(安装Linux)步骤: 烧写原理:superboot-6410.bin(bootloader)+内核镜像文件+根文件系统 1.将SD卡插入USB接口的读卡器,并插在PC的USB口 ...

  9. socket.io的connect连接时不断的进行自动连接,并产生错误net::ERR_EMPTY_RESPONSE

    socket = io.connect('http://192.168.1.200:9043?uuid=333'); 执行上面的语句时,产生下面的错误: 后来经过排查,是由于项目的jdk版本过低引起的 ...

  10. Delegate,Action,Func,匿名方法,匿名委托,事件 (转载)

    Delegate,Action,Func,匿名方法,匿名委托,事件 (转载) 一.委托Delegate 一般的方法(Method)中,我们的参数总是string,int,DateTime...这些基本 ...