CF 463E Caisa and Tree (模拟)
来源:互联网 发布:淘宝满减券漏洞 编辑:程序博客网 时间:2024/05/01 06:29
这道E题出奇的简单,直接模拟就行。
先建树,建树的同时记录每个节点的父节点,查询类型为1的时候直接从v的父节点往上找,找到了就直接返回,查询类型为2的时候就直接修改数组的值就好了。
#include<cstdio>#include<algorithm>#include<cstring>#include<cstdlib>#define M(a,b) memset(a,b,sizeof(a))#define ll long long#define rep(i,a,b) for(i=a;i<=b;i++)using namespace std;int a[100005],head[100005],e,v;int fa[100005];bool vst[100005];struct E{ int to,next;}edge[200005];inline void addedge(int u,int v){ edge[e].to=v; edge[e].next=head[u]; head[u]=e++; edge[e].to=u; edge[e].next=head[v]; head[v]=e++;}int gcd(int a,int b){ if(!b) return a; else return gcd(b,a%b);}void build_tree(int u){ vst[u]=1; int i,k; for(i=head[u];i;i=edge[i].next) { k=edge[i].to; if(vst[k]) continue; fa[k]=u; build_tree(k); }}int solve(int u){ if(!u) return -1; if(gcd(a[u],a[v])>1) return u; return solve(fa[u]);}int main(){ int n,q,i,x,y; e=1; scanf("%d%d",&n,&q); rep(i,1,n) scanf("%d",&a[i]); rep(i,2,n) scanf("%d%d",&x,&y),addedge(x,y); build_tree(1); rep(i,1,q) { int type; scanf("%d",&type); if(type==1) { scanf("%d",&v); printf("%d\n",solve(fa[v])); } else { int w; scanf("%d%d",&v,&w); a[v]=w; } } return 0;}
0 0
- CF 463E Caisa and Tree (模拟)
- Codeforces 463E Caisa and Tree(暴力)
- codeforces 463E . Caisa and Tree
- Codeforces 463E Caisa and Tree dfs+分解质因素
- codeforces 463E Caisa and Tree 栈+dfs
- CodeForces - 463E Caisa and Tree (dfs+素因子分解)
- cf 324e Xenia and Tree
- CF 342E Xenia and Tree(分块)
- cf264 div2E题 E. Caisa and Tree(dfs转化为有根树暴力求解)
- Codeforces Round #264 (Div. 2) E. Caisa and Tree 树上操作暴力
- Codeforce 264 B Caisa and Pylons(模拟)
- 463B. Caisa and Pylons
- CF 258E Little Elephant and Tree(DFS+线段树)
- [CF 251E]Tree and Table题解翻译
- CF 518 E Arthur and Questions(贪心,模拟乱搞)
- codeforces-#463A. Caisa and Sugar
- codeforces-#463B. Caisa and Pylons
- Codeforces 463A Caisa and Sugar(水题)
- 关于《Proofs from THE BOOK》chapter22 单调序列问题
- 基于UIScrollView和UIPageControl控件做的用户引导界面
- ANSI和Unicode字符集
- 走进企业级批处理框架--Springbatch
- 百万级访问网站前期的技术准备
- CF 463E Caisa and Tree (模拟)
- 与指向的类型无关;转换要求 reinterpret_cast、C 样式转换或函数样式转换
- Windows环境下Unicode编程总结
- 单链表C语言实现
- zoj 2706 Thermal Death of the Universe(线段树成段更新)
- create file color.xml at res/values and then you can use android:backgroud"@color/what you like"
- 轻松搞定面试中的二叉树题目
- 工作感悟——SQL语句
- android学习--消息机制Handler Loop MesssageQueue