一、Y总视频进度
二、847. 图中点的层次
(1)问题描述
给定一个 nn 个点 mm 条边的有向图,图中可能存在重边和自环。
所有边的长度都是 11,点的编号为 1∼n1∼n。
请你求出 11 号点到 nn 号点的最短距离,如果从 11 号点无法走到 nn 号点,输出 −1−1。
输入格式
第一行包含两个整数 nn 和 mm。
接下来 mm 行,每行包含两个整数 aa 和 bb,表示存在一条从 aa 走到 bb 的长度为 11 的边。
输出格式
输出一个整数,表示 11 号点到 nn 号点的最短距离。
数据范围
1≤n,m≤1051≤n,m≤105
输入样例:
4 5
1 2
2 3
3 4
1 3
1 4
输出样例:
1
(2)代码实现
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
private static final int N = 100010;
private static int idx, n, m;
private static int[] h = new int[N], e = new int[N], ne = new int[N], d = new int[N], q = new int[N];
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String[] str1 = reader.readLine().split(" ");
n = Integer.parseInt(str1[0]);
m = Integer.parseInt(str1[1]);
for (int i = 0; i < h.length; i++) {
h[i] = -1;
}
for (int i = 0; i < m; i++) {
String[] str2 = reader.readLine().split(" ");
add(Integer.parseInt(str2[0]), Integer.parseInt(str2[1]));
}
System.out.println(bfs());
reader.close();
}
private static void add(int a, int b) {
e[idx] = b;
ne[idx] = h[a];
h[a] = idx++;
}
private static int bfs() {
int hh = 0, tt = 0;
q[0] = 1;
for (int i = 0; i < d.length; i++) {
d[i] = -1;
}
d[1] = 0;
while (hh <= tt) {
int t = q[hh++];
for (int i = h[t]; i != -1; i = ne[i]) {
int j = e[i];
if (d[j] == -1) {
d[j] = d[t] + 1;
q[++tt] = j;
}
}
}
return d[n];
}
}
三、846. 树的重心
(1)题目描述
给定一颗树,树中包含 nn 个结点(编号 1∼n1∼n)和 n−1n−1 条无向边。
请你找到树的重心,并输出将重心删除后,剩余各个连通块中点数的最大值。
重心定义:重心是指树中的一个结点,如果将这个点删除后,剩余各个连通块中点数的最大值最小,那么这个节点被称为树的重心。
输入格式
第一行包含整数 nn,表示树的结点数。
接下来 n−1n−1 行,每行包含两个整数 aa 和 bb,表示点 aa 和点 bb 之间存在一条边。
输出格式
输出一个整数 mm,表示将重心删除后,剩余各个连通块中点数的最大值。
数据范围
1≤n≤1051≤n≤105
输入样例
9
1 2
1 7
1 4
2 8
2 5
4 3
3 9
4 6
输出样例:
4
(2)代码实现
import java.io.*;
public class Main {
static final int N = (int)1e5 + 10;
static int n, idx, res;
static int[] h = new int[N], e = new int[N << 1], ne = new int[N << 1];
static boolean[] status = new boolean[N];
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static StreamTokenizer st = new StreamTokenizer(br);
static {
res = N;
}
static void init(int[] h) {
for (int i = 1; i <= n; ++i) {
h[i] = -1;
}
}
static void add(int a, int b) {
e[idx] = b;
ne[idx] = h[a];
h[a] = idx++;
}
static int dfs(int u) {
status[u] = true;
int s, sum = 1, max = 0;
for (int i = h[u]; i > -1; i = ne[i]) {
if (!status[e[i]]) {
s = dfs(e[i]);
max = Math.max(max, s);
sum += s;
}
}
max = Math.max(max, n - sum);
res = Math.min(res, max);
return sum;
}
public static void main(String[] args) throws IOException {
st.nextToken();
n = (int)st.nval;
init(h);
int a, b;
for (int i = 1; i < n; ++i) {
st.nextToken();
a = (int)st.nval;
st.nextToken();
b = (int)st.nval;
add(a, b);
add(b, a);
}
dfs(1);
bw.write(res + "");
bw.close();
}
}
四、2019第十届蓝桥杯 第二题:不同子串
(1)题目描述
一个字符串的非空子串是指字符串中长度至少为 1 的连续的一段字符组成 的串。例如,字符串aaab 有非空子串a, b, aa, ab, aaa, aab, aaab,一共 7 个。 注意在计算时,只算本质不同的串的个数。请问,字符串0100110001010001 有多少个不同的非空子串?
(2)代码实现
public class DifferentSubstring{
public static void main(String[] args) {
String target = "0100110001010001";
Set<String> sub = new HashSet<String>();
for (int step = 0; step <= target.length() - 1; step++) {
for (int beginIndex = 0, endIndex = 1 + step; endIndex <= target.length(); beginIndex++, endIndex++) {
sub.add(target.substring(beginIndex, endIndex));
}
}
System.out.println(sub.size());
}
}