提交 0c34f05d 编写于 作者: qq_36480062's avatar qq_36480062

c

上级 f55470b2
......@@ -66,7 +66,7 @@ public class Tarjan求LCA {
for (int i = 0; i < m; i++) {
addq(nextInt(), nextInt(), i);
}//存储询问
tarjan(root, 0);
tarjan(root, -1);
for (int i = 0; i < m; i++) {
bw.write(result[i] + "\n");
}
......
......@@ -102,11 +102,16 @@ public class 祖孙询问 {
h[b] = cnt++;
}
/**
* bfs求深度和lca
*
* @param root
*/
static void bfs(int root) {
Arrays.fill(depth, inf);
depth[0] = 0;
depth[root] = 1;
q.add(root);
depth[root] = 1;//规定根节点深度为1
q.add(root);//加入根节点
while (!q.isEmpty()) {
int t = q.poll();
for (int i = h[t]; i != 0; i = ne[i]) {
......@@ -115,7 +120,7 @@ public class 祖孙询问 {
depth[j] = depth[t] + 1;//bfs拓展
q.add(j);
up[j][0] = t;//记录第一个
for (int k = 1; k <= 17; k++) {//记录后面的up
for (int k = 1; k <= 17; k++) {//记录后面的up,虽然有无效计算,但胜在好写
up[j][k] = up[up[j][k - 1]][k - 1];
}
}
......
......@@ -55,11 +55,11 @@ public class dijkstra {
c = nextInt();
add(a, b, c);
}
Arrays.fill(dis, Integer.MAX_VALUE);
dij(s);
}
private static void dij(int s) throws IOException {
Arrays.fill(dis, Integer.MAX_VALUE);
q.add(new node(s, 0));
dis[s] = 0;
while (!q.isEmpty()) {
......
......@@ -3,7 +3,9 @@ package graph.有向图的强联通分量;
import java.util.Scanner;
/**
* tarjan求强联通分量
* https://www.luogu.com.cn/blog/zyb2624936151/tarjan
*
*/
@SuppressWarnings("all")
public class 受欢迎的牛 {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册