-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree_diameter.sublime-snippet
62 lines (61 loc) · 1.34 KB
/
tree_diameter.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
<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;
}
void dfs1(vector<vector<lint>> &adj,lint *visited,lint s,lint c,lint *maxx_tree,lint *downpath)
{
if(visited[s])
return;
visited[s]=1;
vector<lint> temp;
for(auto u:adj[s])
{
if(u!=c)
{
temp.pb(downpath[u]);
}
}
sort(temp.begin(),temp.end());
if(temp.size()==0)
{
maxx_tree[s]=0;
return;
}
else if(temp.size()==1)
{
maxx_tree[s]=1+temp[temp.size()-1];
}
else
{
maxx_tree[s]=2+temp[temp.size()-1]+temp[temp.size()-2];
}
for(auto u:adj[s])
{
if(u!=c)
{
dfs1(adj,visited,u,s,maxx_tree,downpath);
maxx_tree[s]=max(maxx_tree[s],maxx_tree[u]);
}
}
return;
}
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>tree_diameter</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope>source.python</scope> -->
</snippet>