Skip to content

Commit a484f89

Browse files
committed
2 parents fe7b2c6 + 44cab1f commit a484f89

File tree

1 file changed

+19
-24
lines changed

1 file changed

+19
-24
lines changed

graph-theory.md

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,6 +2045,7 @@ signed main()
20452045
for (auto &x : edges) cin >> x.u >> x.v >> x.w;
20462046

20472047
vec<1, int> dist(n+1, INF), par(n+1, -1);
2048+
dist[1] = 0;
20482049
int x;
20492050
for (int i = 0; i < n; ++i)
20502051
{
@@ -2624,31 +2625,27 @@ In finding succ\(x, k\) it takes O\(k\) time however with preprocessing query be
26242625
// For query, present k as sum of powers of 2 so 11 = 8 + 2 + 1
26252626
// succ(x,11) = succ(succ(succ(x,8),2),1)
26262627
const int MAXN = 2*1e5 + 5;
2627-
vec<2, int> sparseTable(32, MAXN);
2628+
vec<2, int> succ(32, MAXN);
26282629
signed main()
26292630
{
26302631
ios_base::sync_with_stdio(false); cin.tie(NULL);
26312632
int n, q; cin >> n >> q;
2632-
for (int j = 1; j <= n; ++j) cin >> sparseTable[0][j];
2633+
for (int j = 1; j <= n; ++j) cin >> succ[0][j];
26332634
for (int i = 1; i < 32; ++i)
26342635
for (int j = 1; j <= n; ++j)
2635-
sparseTable[i][j] = sparseTable[i-1][sparseTable[i-1][j]];
2636-
2636+
succ[i][j] = succ[i-1][succ[i-1][j]];
2637+
26372638
while (q--)
26382639
{
26392640
int x, k; cin >> x >> k;
2640-
vec<1, int> repre;
2641-
int cur = 1;
2641+
int i = 0;
26422642
while (k)
26432643
{
2644-
if (k&1) repre.push_back(cur);
2645-
k >>= 1; cur <<= 1;
2644+
if (k&1) x = succ[i][x];
2645+
k >>= 1;
2646+
++i;
26462647
}
2647-
reverse(all(repre));
2648-
int res = x;
2649-
for (auto &v : repre)
2650-
res = sparseTable[log2(v)][res];
2651-
cout << res << '\n';
2648+
cout << x << '\n';
26522649
}
26532650
return 0;
26542651
}
@@ -2672,21 +2669,19 @@ Above logic in successor paths can be applied here
26722669
![](.gitbook/assets/image%20%2888%29.png)
26732670
26742671
```cpp
2675-
const int N = 2*1e5;
2676-
int n;
2677-
vector<int> adj[N], toLeaf(N), maxLength(N);
2678-
int DFS(int u = 0, int par = -1, int depth = 0)
2672+
int ans = 0;
2673+
int dfs(int u = 1, int par = -1)
26792674
{
2680-
int mxDepth = 0, secondMxDepth = 0;
2681-
for (auto &v : adj[u])
2675+
int dep1 = 0, dep2 = 0;
2676+
for (const int v : adj[u])
26822677
{
26832678
if (v == par) continue;
2684-
int cur = DFS(v, u, depth+1);
2685-
if (cur > mxDepth) secondMxDepth = mxDepth, mxDepth = cur;
2686-
else if (cur > secondMxDepth) secondMxDepth = cur;
2679+
int curDep = dfs(v, u);
2680+
if (curDep > dep1) dep2 = dep1, dep1 = curDep;
2681+
else if (curDep > dep2) dep2 = curDep;
26872682
}
2688-
maxLength[u] = mxDepth + secondMxDepth + 1;
2689-
return toLeaf[u] = mxDepth+1;
2683+
ans = max(ans, dep1+dep2);
2684+
return dep1+1;
26902685
}
26912686
```
26922687

0 commit comments

Comments
 (0)