forked from matthewsamuel95/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Treediff.cpp
97 lines (93 loc) · 2.18 KB
/
Treediff.cpp
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
#include <bits/stdc++.h>
using namespace std;
multiset<int> *s[50005],*d[50005];
vector< vector<int> > g(50005);
int color[50005];
int size_tree[50005];
int n,m,ans[500005];
int dfs(int u,int par)
{
int mx=-1,mv=-1,v;
for(int i=0;i<g[u].size();i++)
{
v= g[u][i];
if(v==par)
continue;
int sz = dfs(v,u);
if(sz>mx)
{
mx=sz;
mv=v;
}
}
if(mx!=-1)
{
s[u]=s[mv];
d[u]=d[mv];
}
else
{
s[u] = new multiset<int>();
d[u] = new multiset<int>();
(*s[u]).insert(color[u]);
}
for(int i=0;i<g[u].size();i++)
{
v= g[u][i];
if(v==par)
continue;
if(v!= mv && v!= par)
{
for(auto it=(*s[v]).begin();it!= (*s[v]).end();it++)
{
int num = *it;
auto itt= (*s[u]).lower_bound(num);
if(itt==(*s[u]).begin())
{
(*s[u]).insert(*it);
(*d[u]).insert(abs(*itt-*it));
}
else
if(itt==(*s[u]).end())
{
itt--;
(*s[u]).insert(*it);
(*d[u]).insert(abs(*itt-*it));
}
else
{
int num1=*itt;
itt--;
int num2=*itt;
auto ii=(*d[u]).find(num2-num1);
if(ii!=(*d[u]).end())
(*d[u]).erase(ii);
(*s[u]).insert(*it);
(*d[u]).insert(abs(num1-*it));
(*d[u]).insert(abs(num2-*it));
}
}
}
}
if((*s[u]).size()==1)
ans[u]=2147483647;
else
if(u<(n-m+1))
ans[u]=*(*d[u]).begin();
return (int)(*s[u]).size();
}
int main()
{
scanf("%d %d",&n,&m);
for(int i=2;i<=n;i++)
{
int p;
scanf("%d",&p);
g[i].push_back(p);
g[p].push_back(i);
}
for(int i=n-m+1;i<=n;i++) scanf("%d",&color[i]);
int sz=dfs(1,0);
for(int i=1;i<(n-m+1);i++) printf("%d ",ans[i]);
return 0;
}