-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree_longestpaths.sublime-snippet
78 lines (75 loc) · 1.71 KB
/
tree_longestpaths.sublime-snippet
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
<snippet>
<content><![CDATA[
void dwpath(vector<vector<lint>> &adj,lint *visited,lint s,lint c,lint *downpath)
{
if(visited[s])
return;
visited[s]=1;
lint maxx=-1;
for(auto u:adj[s])
{
if(c!=u)
{
dwpath(adj,visited,u,s,downpath);
maxx=max(maxx,downpath[u]);
}
}
downpath[s]=1+maxx;
}
//give initial par_ans = -1
void dfs(vector<vector<lint>> &adj,lint *visited,lint s,lint c,lint *downpath,lint par_ans,lint *ans)
{
if(visited[s])
return;
visited[s]=1;
vector<lint> pref;
vector<lint> suff;
for(auto u:adj[s])
{
if(u!=c)
{
pref.pb(downpath[u]);
suff.pb(downpath[u]);
}
}
for(lint i=1;i<pref.size();i++)
{
pref[i]=max(pref[i],pref[i-1]);
}
lint n=suff.size();
for(lint i=n-2;i>=0;i--)
{
suff[i]=max(suff[i],suff[i+1]);
}
lint idx=0;
for(auto u:adj[s])
{
if(u!=c)
{
lint maxx=-20;
if(idx!=0)
{
maxx=max(maxx,pref[idx-1]);
}
if(idx!=pref.size()-1)
{
maxx=max(maxx,suff[idx+1]);
}
lint partial=1+max(par_ans,maxx);
dfs(adj,visited,u,s,downpath,partial,ans);
idx++;
}
}
if(pref.size()==0)
{
ans[s]=1+par_ans;
return;
}
ans[s]=1+max(par_ans,pref.back());
}
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>tree_longestpaths</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope>source.python</scope> -->
</snippet>