Codeforces Round #264 (Div. 2)E(裸的树询问)

E. Caisa and Tree
time limit per test
10 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Caisa is now at home and his son has a simple task for him.

Given a rooted tree with n vertices, numbered from 1 to n (vertex 1 is the root). Each vertex of the tree has a value. You should answer qqueries. Each query is one of the following:

  • Format of the query is "1 v". Let's write out the sequence of vertices along the path from the root to vertex vu1, u2, ..., uk (u1 = 1; uk = v). You need to output such a vertex ui that gcd(value of ui, value of v) > 1 and i < k. If there are several possible vertices ui pick the one with maximum value of i. If there is no such vertex output -1.
  • Format of the query is "2 v w". You must change the value of vertex v to w.

You are given all the queries, help Caisa to solve the problem.

Input

The first line contains two space-separated integers nq (1 ≤ n, q ≤ 105).

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 2·106), where ai represent the value of node i.

Each of the next n - 1 lines contains two integers xi and yi (1 ≤ xi, yi ≤ nxi ≠ yi), denoting the edge of the tree between vertices xiand yi.

Each of the next q lines contains a query in the format that is given above. For each query the following inequalities hold: 1 ≤ v ≤ n and1 ≤ w ≤ 2·106Note that: there are no more than 50 queries that changes the value of a vertex.

Output

For each query of the first type output the result of the query.

Sample test(s)
input
4 6
10 8 4 3
1 2
2 3
3 4
1 1
1 2
1 3
1 4
2 1 9
1 4
output
-1
1
2
-1
1

题意:两种操作 RT

思路:纯暴力真的能过,也就900多MS,CF好逗,sad~

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
using namespace std;

int fa[100010];
int head[100010];
int edge[100010<<1];
int next[100010<<1];
int ww[100010];
int d;
int n;
int q;

void add(int u,int v)
{
    edge[d]=v;
    next[d]=head[u];
    head[u]=d++;
}

void dfs(int u,int pre)
{
    fa[u]=pre;
    for(int i=head[u];i!=-1;i=next[i])if(edge[i]!=pre)dfs(edge[i],u);
}

int gcd(int a,int b)
{
    if(a
       
       
         1){ maxi=u; break; } u=fa[u]; } return maxi; } int main() { while(scanf("%d%d",&n,&q)!=EOF) { int i; int u,v; memset(head,-1,sizeof(head)); d=0; for(i=1;i<=n;i++)scanf("%d",&ww[i]); for(i=1;i 
         
       
      
      
     
     
    
    
   
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值