-
Notifications
You must be signed in to change notification settings - Fork 3
/
heavylight.cpp
182 lines (146 loc) · 3.67 KB
/
heavylight.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <algorithm>
#include <numeric>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cassert>
#include <cmath>
#include <complex>
using namespace std;
const int V = 100000;
vector<int> adj[V]; // adjacency list
int parent[V], heavy[V];
int depth[V], size[V];
int chain[V], head[V];
//Where chain[u] is u's chain number and head[u] is the node closest to root in u's chain.
void DFS(int i)
{
size[i] = 1;
for (int k=0; k<adj[i].size(); ++k)
{
int j = adj[i][k];
if (j == parent[i]) continue;
parent[j] = i;
depth[j] = depth[i] + 1;
DFS(j);
size[i] += size[j];
if (heavy[i] == -1 || size[j] > size[heavy[i]])
heavy[i] = j;
}
}
void heavylight_DFS(int N)
{
memset(heavy, -1, sizeof(heavy));
parent[0] = -1;
depth[0] = 0;
DFS(0);
int c = 0;
for (int i=0; i<N; ++i)
if (parent[i] == -1 || heavy[parent[i]] != i)
{
for (int k = i; k != -1; k = heavy[k])
chain[k] = c, head[k] = i;
c++;
}
}
int q[V], *qf, *qb; // BFS queue
void heavylight_BFS(int N)
{
qf = qb = q;
parent[0] = -1;
depth[0] = 0;
*qb++ = 0;
while (qf < qb)
for (int i=*qf++, k=0; k<adj[i].size(); ++k)
{
int j = adj[i][k];
if (j == parent[i]) continue;
parent[j] = i;
depth[j] = depth[i] + 1;
*qb++ = j;
}
memset(size, 0, sizeof(size));
memset(heavy, -1, sizeof(heavy));
for (int k=N-1; k>0; --k)
{
int j = q[k], i = parent[q[k]];
size[j]++;
size[i] += size[j];
if (heavy[i] == -1 || size[j] > size[heavy[i]])
heavy[i] = j;
}
int c = 0;
for (int i=0; i<N; ++i)
if (parent[i] == -1 || heavy[parent[i]] != i)
{
for (int k = i; k != -1; k = heavy[k])
chain[k] = c, head[k] = i;
c++;
}
}
int lca_1(int i, int j)
{
while (chain[i] != chain[j])
if (depth[head[i]] > depth[head[j]])
i = parent[head[i]];
else
j = parent[head[j]];
return depth[i] < depth[j] ? i : j;
}
int lca_2(int i, int j)
{
while (chain[i] != chain[j])
{
if (depth[head[i]] > depth[head[j]])
swap(i, j);
j = parent[head[j]];
}
if (depth[i] > depth[j])
swap(i, j);
return i;
}
void look_inside(int N) {
int i;
printf("\n");
printf("HEAVY: \n");
printf("(i, j): i----(heavy edge)----j\n\n");
for (i = 0; i < N; i++)
printf("(%d, %d)\n", i, heavy[i]);
printf("\n");
printf("CHAIN: \n");
printf("(i, j): Node i is in group (heavy-path group) number j\n\n");
for (i = 0; i < N; i++)
printf("(%d, %d)\n", i, chain[i]);
printf("\n");
printf("HEAD: \n");
printf("(i, j): Node i goes up all the way to the highest node (j) which is in the same group\n\n");
for (i = 0; i < N; i++)
printf("(%d, %d)\n", i, head[i]);
}
int main() {
int N, i, j;
FILE *fin = fopen("input.txt", "r");
fscanf(fin, "%d", &N);
for (i = 0; i < N; i++)
adj[i].clear();
while (fscanf(fin, "%d%d", &i, &j) != EOF) {
adj[i].push_back(j);
adj[j].push_back(i);
}
//heavylight_DFS(N);
heavylight_BFS(N);
//printf("%d\n", lca_2(12, 16));
//printf("%d\n", lca_2(16, 12));
//printf("%d\n", lca_2(0, 7));
//printf("%d\n", lca_2(0, 24));
printf("%d\n", lca_1(6635, 8590));
//look_inside(N); //I just added it into this program in order to understand more about how it works
}