-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlca.codesnippet
71 lines (69 loc) · 1.87 KB
/
lca.codesnippet
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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDECodeSnippetCompletionPrefix</key>
<string>lca</string>
<key>IDECodeSnippetCompletionScopes</key>
<array>
<string>TopLevel</string>
</array>
<key>IDECodeSnippetContents</key>
<string>// LCA
#define MAX_V 100100
#define MAX_LOG_V 32
vector<int> G[MAX_V];
int root;
int parent[MAX_LOG_V][MAX_V]; //親を2^k回たどって到達する頂点(値を通り過ぎると場合は-1とする)
int depth[MAX_V]; //根からの深さ
void dfs(int v, int p, int d){
parent[0][v]=p;
depth[v]=d;
for(auto x: G[v]){
if(x!=p) dfs(x,v,d+1);
}
}
//初期化
void init(int V){
//parent[0]とdepthを初期化
dfs(root,-1,0);
//parentを初期化
REP(k,MAX_LOG_V-1){
REP(v,V){
if(parent[k][v]<0) parent[k+1][v]=-1;
else parent[k+1][v]=parent[k][parent[k][v]];
}
}
}
// uとvのLCAを求める
int lca(int u, int v){
// uとvの高さが同じになるまでおやを辿る
if(depth[u] > depth[v]) swap(u,v);
REP(k,MAX_LOG_V){
if((depth[v]-depth[u])>>k&1){
v=parent[k][v];
}
}
if(u==v) return u;
//二分撫索でLCAを求める
for(int k = MAX_LOG_V-1; k >= 0; k--){
if (parent[k][u] != parent[k][v]){
u=parent[k][u];
v=parent[k][v];
}
}
return parent[0][u];
}
</string>
<key>IDECodeSnippetIdentifier</key>
<string>675E8E54-7E11-446F-88C1-5369C2DE63B1</string>
<key>IDECodeSnippetLanguage</key>
<string>Xcode.SourceCodeLanguage.C-Plus-Plus</string>
<key>IDECodeSnippetTitle</key>
<string>LCA</string>
<key>IDECodeSnippetUserSnippet</key>
<true/>
<key>IDECodeSnippetVersion</key>
<integer>2</integer>
</dict>
</plist>