|
| 1 | +/*"Success isn't permanent, failure isn't fatal, |
| 2 | + it's the courage to continue that counts" |
| 3 | +⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ |
| 4 | +*/ |
| 5 | +#include<bits/stdc++.h> |
| 6 | +using namespace std; |
| 7 | +#define A 1000000007ll |
| 8 | +#define D 100000000000000ll |
| 9 | +#define B 998244353ll |
| 10 | +#define C 1000000000000000000ll |
| 11 | +#define pfin(a) printf("%d\n",a); |
| 12 | +#define pfln(a) printf("%lld\n",a); |
| 13 | +#define pfis(a) printf("%d ",a); |
| 14 | +#define pfls(a) printf("%lld ",a); |
| 15 | +#define sfi(a) scanf("%d",&a); |
| 16 | +#define sfl(a) scanf("%lld",&a); |
| 17 | +#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) |
| 18 | +#define f(i,a,b) for(int i=a;i<b;i++) |
| 19 | +#define pb(a) push_back(a); |
| 20 | +#define mp(a,b) make_pair(a,b) |
| 21 | +#define ll long long |
| 22 | +#define F first |
| 23 | +#define S second |
| 24 | +#define vi vector<ll> |
| 25 | +#define vc vector<char> |
| 26 | +#define endl "\n" |
| 27 | +#define deci fixed<<setprecision(9) |
| 28 | +const ll mod = 1e9 + 9; |
| 29 | +#define N 1000005 |
| 30 | +ll arr[N + 1]; |
| 31 | +vector<ll> v1[N + 1]; |
| 32 | +ll parent[N + 1][25]; |
| 33 | +void dfs(ll s, ll par) |
| 34 | +{ |
| 35 | + ll i; |
| 36 | + parent[s][0] = par; |
| 37 | + for (i = 0; i < v1[s].size(); i++) |
| 38 | + { |
| 39 | + ll k = v1[s][i]; |
| 40 | + |
| 41 | + if (k != par) |
| 42 | + { |
| 43 | + dfs(k, s); |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | +void twotoithparent(ll n) |
| 48 | +{ |
| 49 | + ll i, j; |
| 50 | + for (i = 1; i < 20; i++) |
| 51 | + { |
| 52 | + for (j = 1; j <= n; j++) |
| 53 | + { |
| 54 | + if (parent[j][i - 1] != -1) |
| 55 | + { |
| 56 | + parent[j][i] = parent[parent[j][i - 1]][i - 1]; |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | +ll kthparent(ll k, ll u) |
| 62 | +{ |
| 63 | + while (k > 0) |
| 64 | + { |
| 65 | + ll i = log2(k); |
| 66 | + if (parent[u][i] != -1) |
| 67 | + { |
| 68 | + u = parent[u][i]; |
| 69 | + k = k - (1 << i); |
| 70 | + } |
| 71 | + else |
| 72 | + { |
| 73 | + return -1; |
| 74 | + break; |
| 75 | + } |
| 76 | + } |
| 77 | + return u; |
| 78 | +} |
| 79 | +int main() |
| 80 | +{ |
| 81 | +#ifndef ONLINE_JUDGE |
| 82 | + // for getting input from input.txt |
| 83 | + freopen("input.txt", "r", stdin); |
| 84 | + // for writing output to output.txt |
| 85 | + freopen("output.txt", "w", stdout); |
| 86 | +#endif |
| 87 | + ios_base::sync_with_stdio(false); |
| 88 | + cin.tie(NULL); |
| 89 | + memset(parent, -1, sizeof(parent)); |
| 90 | + ll n, k; |
| 91 | + sfl(n); |
| 92 | + sfl(k); |
| 93 | + f(i,1,n+1) |
| 94 | + { |
| 95 | + sfl(color[i]); |
| 96 | + } |
| 97 | + f(i, 1, n) |
| 98 | + { |
| 99 | + ll x, y; |
| 100 | + sfl(x); |
| 101 | + sfl(y); |
| 102 | + v1[x].pb(y); |
| 103 | + v1[y].pb(x); |
| 104 | + } |
| 105 | + dfs(1, -1); |
| 106 | + twotoithparent(n); |
| 107 | + /* f(i, 1, n + 1) |
| 108 | + { |
| 109 | + f(j, 0, 6) |
| 110 | + { |
| 111 | + cout << parent[i][j] << " "; |
| 112 | + } |
| 113 | + cout << endl; |
| 114 | + }*/ |
| 115 | + f(i, 1, n + 1) |
| 116 | + { |
| 117 | + |
| 118 | + ll ans = kparent(k, i); |
| 119 | + // cout << ans << endl; |
| 120 | + if (color[ans] == color[i]) |
| 121 | + { |
| 122 | + cout << ans << " "; |
| 123 | + } |
| 124 | + else |
| 125 | + { |
| 126 | + cout << "-1" << " "; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | +} |
0 commit comments