forked from sunnyshahabuddin/Coding-Ninjas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTheFamilyTreeOfBob.cpp
More file actions
131 lines (127 loc) · 2.57 KB
/
TheFamilyTreeOfBob.cpp
File metadata and controls
131 lines (127 loc) · 2.57 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*"Success isn't permanent, failure isn't fatal,
it's the courage to continue that counts"
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
*/
#include<bits/stdc++.h>
using namespace std;
#define A 1000000007ll
#define D 100000000000000ll
#define B 998244353ll
#define C 1000000000000000000ll
#define pfin(a) printf("%d\n",a);
#define pfln(a) printf("%lld\n",a);
#define pfis(a) printf("%d ",a);
#define pfls(a) printf("%lld ",a);
#define sfi(a) scanf("%d",&a);
#define sfl(a) scanf("%lld",&a);
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define f(i,a,b) for(int i=a;i<b;i++)
#define pb(a) push_back(a);
#define mp(a,b) make_pair(a,b)
#define ll long long
#define F first
#define S second
#define vi vector<ll>
#define vc vector<char>
#define endl "\n"
#define deci fixed<<setprecision(9)
const ll mod = 1e9 + 9;
#define N 1000005
ll arr[N + 1];
vector<ll> v1[N + 1];
ll parent[N + 1][25];
void dfs(ll s, ll par)
{
ll i;
parent[s][0] = par;
for (i = 0; i < v1[s].size(); i++)
{
ll k = v1[s][i];
if (k != par)
{
dfs(k, s);
}
}
}
void twotoithparent(ll n)
{
ll i, j;
for (i = 1; i < 20; i++)
{
for (j = 1; j <= n; j++)
{
if (parent[j][i - 1] != -1)
{
parent[j][i] = parent[parent[j][i - 1]][i - 1];
}
}
}
}
ll kthparent(ll k, ll u)
{
while (k > 0)
{
ll i = log2(k);
if (parent[u][i] != -1)
{
u = parent[u][i];
k = k - (1 << i);
}
else
{
return -1;
break;
}
}
return u;
}
int main()
{
#ifndef ONLINE_JUDGE
// for getting input from input.txt
freopen("input.txt", "r", stdin);
// for writing output to output.txt
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
memset(parent, -1, sizeof(parent));
ll n, k;
sfl(n);
sfl(k);
f(i,1,n+1)
{
sfl(color[i]);
}
f(i, 1, n)
{
ll x, y;
sfl(x);
sfl(y);
v1[x].pb(y);
v1[y].pb(x);
}
dfs(1, -1);
twotoithparent(n);
/* f(i, 1, n + 1)
{
f(j, 0, 6)
{
cout << parent[i][j] << " ";
}
cout << endl;
}*/
f(i, 1, n + 1)
{
ll ans = kparent(k, i);
// cout << ans << endl;
if (color[ans] == color[i])
{
cout << ans << " ";
}
else
{
cout << "-1" << " ";
}
}
}