codeforces 463E Caisa and Tree 栈+dfs
2014-08-30 23:00
423 查看
题意:对于一棵树,1为根结点,每个结点有一个值,有两种操作:1.将从根结点到结点v的路径中找最接近v的某个结点,满足两个点
对应值的gcd不为1 。 2.将结点v的值修改为w。
思路:我们从根开始dfs,对于当前访问的u,我们其对应的值a[u]分解成质因数的形式,将它在st中的位置存入map<int, stack<int>>
mp中。其中,键值为a [ u ]的质因数。不妨设x为a[ u ]的某个质因数,如果mp[x]的栈中已经有值,说明是从根结点到u的路径中有
和u有最大公约数不为1的值存在,而且那个值在st中的位置就是栈元素所代表的。 所以对于a[ u ]的所有质因数对应的栈中元素取最
大值就是最靠近u的与a[ u ]的gcd不为1的值位置。当u及其子树全部访问完之后我们把st、mp[x](x为a[ u ]的质因数)与u有关的全部
清除。这样经过一次dfs之后,对于cmd=1来说,就可以直接O(1)查询。 对于cmd=2,由于改变的树某些结点的值,所以需要重新
dfs。详见代码:
对应值的gcd不为1 。 2.将结点v的值修改为w。
思路:我们从根开始dfs,对于当前访问的u,我们其对应的值a[u]分解成质因数的形式,将它在st中的位置存入map<int, stack<int>>
mp中。其中,键值为a [ u ]的质因数。不妨设x为a[ u ]的某个质因数,如果mp[x]的栈中已经有值,说明是从根结点到u的路径中有
和u有最大公约数不为1的值存在,而且那个值在st中的位置就是栈元素所代表的。 所以对于a[ u ]的所有质因数对应的栈中元素取最
大值就是最靠近u的与a[ u ]的gcd不为1的值位置。当u及其子树全部访问完之后我们把st、mp[x](x为a[ u ]的质因数)与u有关的全部
清除。这样经过一次dfs之后,对于cmd=1来说,就可以直接O(1)查询。 对于cmd=2,由于改变的树某些结点的值,所以需要重新
dfs。详见代码:
// file name: codeforces463E.cpp // // author: kereo // // create time: 2014年08月30日 星期六 22时02分29秒 // //***********************************// #include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<set> #include<map> #include<vector> #include<stack> #include<cmath> #include<algorithm> using namespace std; typedef long long ll; const int MAXN=100000+100; const int inf=0x3fffffff; #define L(x) (x<<1) #define R(x) (x<<1|1) int n,Q,edge_cnt; int a[MAXN],head[MAXN],ans[MAXN]; map<int,stack<int> >mp; vector<int>fac[MAXN]; vector<int>st; struct Edge { int v; int next; }edge[MAXN*2]; void init(){ edge_cnt=0; memset(head,-1,sizeof(head)); } void addedge(int u,int v){ edge[edge_cnt].v=v; edge[edge_cnt].next=head[u]; head[u]=edge_cnt++; } vector<int> factorize(int x){ //将x分解成质因数 vector<int>vec; for(int i=2;i*i<=x;i++){ if(x % i == 0) vec.push_back(i); while(x % i == 0) x/=i; } if(x>1) vec.push_back(x); return vec; } int top(int x){ if(!mp.count(x) || mp[x].empty()) return -1; return mp[x].top(); } void dfs(int u,int fa){ int Max=-1; for(int i=0;i<fac[u].size();i++) Max=max(Max,top(fac[u][i])); //选最靠后的 ans[u]=Max == -1 ? -1 : st[Max]; //最靠后位置的值 st.push_back(u); for(int i=0;i<fac[u].size();i++) mp[fac[u][i]].push(st.size()-1); for(int i=head[u];i!=-1;i=edge[i].next){ int v=edge[i].v; if(v == fa) continue; dfs(v,u); } st.pop_back(); //访问完u,要clear for(int i=0;i<fac[u].size();i++) mp[fac[u][i]].pop(); } int main() { while(~scanf("%d%d",&n,&Q)){ init(); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); fac[i]=factorize(a[i]); } for(int i=1;i<n;i++){ int u,v; scanf("%d%d",&u,&v); addedge(u,v); addedge(v,u); } dfs(1,1); while(Q--){ int cmd; scanf("%d",&cmd); if(cmd == 1){ int x; scanf("%d",&x); printf("%d\n",ans[x] == -1 ? -1 : ans[x]); } if(cmd == 2){ int x,w; scanf("%d%d",&x,&w); a[x]=w; fac[x]=factorize(w); dfs(1,1); } } } return 0; }
相关文章推荐
- codeforces 463E . Caisa and Tree
- CodeForces - 463E Caisa and Tree (dfs+素因子分解)
- 342E Xenia and Tree——codeforces
- Codeforces 764C或763A Timofey and a tree 【好题】
- codeforces 381 D Alyona and a tree(倍增)(前缀数组)
- [CodeForces-375E]Red and Black Tree
- codeforces 842C. Ilya And The Tree(dfs)
- 矩阵快速幂DP Darth Vader and Tree : CodeForces - 514E
- CodeForces - 842C Ilya And The Tree(树上倍增)
- Codeforces 514E. Darth Vader and Tree DP+矩阵快速幂
- Codeforces 461B - Appleman and Tree 树状DP
- codeforces 258-E. Little Elephant and Tree 线段树,离线
- Codeforces--658C--Bear and Forgotten Tree 3(模拟&&技巧)(好题)
- Codeforces 842C Ilya And The Tree
- codeforces 842C. Ilya And The Tree(dfs)
- CodeForces 682C Alyona and the Tree 搜索
- Codeforces 682C Alyona and the Tree(树形DP)
- CodeForces 750G. New Year and Binary Tree Paths
- Codeforces 741D D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths 树上启发式合并
- Codeforces 653E:Bear and Forgotten Tree 2