1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| #include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n; int w[N], fa[N]; int gcd(int x, int y) { return y == 0 ? x : gcd(y, x % y); } int ask(int x, int a) { if (x == 0) return -1; if (gcd(w[x], a) != 1) return x; return ask(fa[x], a); } int main() { int k, op, u; scanf("%d%d", &n, &k); for (int i = 1; i <= n; ++i) scanf("%d", &w[i]); for (int i = 1, x, y; i < n; ++i) scanf("%d%d", &x, &y), fa[y] = x; while (k--) { scanf("%d%d", &op, &u); if (op == 1) printf("%d\n", ask(fa[u], w[u])); else scanf("%d", &w[u]); } return 0; }
|