有向图的强联通分量
例题:受欢迎的牛
【题目描述】
原题来自:USACO 2003 Fall
每一头牛的愿望就是变成一头最受欢迎的牛。现在有 N 头牛,给你 M 对整数 (A,B),表示牛 A 认为牛 B 受欢迎。这种关系是具有传递性的,如果 A 认为 B 受欢迎,B 认为 C 受欢迎,那么牛 A 也认为牛 C 受欢迎。你的任务是求出有多少头牛被除自己之外的所有牛认为是受欢迎的。
【输入】
第一行两个数 N,M;
接下来 M 行,每行两个数 A,B,意思是 A 认为 B 是受欢迎的(给出的信息有可能重复,即有可能出现多个 A,B)。
【输出】
输出被除自己之外的所有牛认为是受欢迎的牛的数量。
【输入样例】
3 3
1 2
2 1
2 3
【输出样例】
1
【提示】
样例说明
只有第三头牛被除自己之外的所有牛认为是受欢迎的。
数据范围:
对于全部数据,1≤N≤104,1≤M≤5×104。
————————————————————————————————————————————————————
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static int N = 10010;
static int M = 50010;
static int n;
static int m;
static int[] h = new int[N];
static int[] e = new int[M];
static int[] ne = new int[M];
static int idx;
static int[] dfn = new int[N];
static int[] low = new int[N];
static int timestamp;
static int[] stk = new int[N];
static int top;
static boolean[] in_stk = new boolean[N];
static int[] id = new int[N];
static int scc_cnt;
static int[] size = new int[N];
static int[] dout = new int[N];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
Arrays.fill(h, -1);
while (m-- > 0) {
int a = sc.nextInt();
int b = sc.nextInt();
add(a, b);
}
for (int i = 1; i <= n; i++) {
if (dfn[i] == 0) {
tarjan(i);
}
}
for (int i = 1; i <= n; i++) {
for (int j = h[i]; j != -1; j = ne[j]) {
int k = e[j];
int a = id[i];
int b = id[k];
if (a != b) {
dout[a]++;
}
}
}
int zeros = 0, sum = 0;
for (int i = 1; i <= scc_cnt; i++) {
if (dout[i] == 0) {
zeros++;
sum += size[i];
if (zeros > 1) {
sum = 0;
break;
}
}
}
System.out.println(sum);
}
private static void tarjan(int u) {
dfn[u] = ++timestamp;
low[u] = dfn[u];
stk[++top] = u;
in_stk[u] = true;
for (int i = h[u]; i != -1; i = ne[i]) {
int j = e[i];
if (dfn[j] == 0) {
tarjan(j);
low[u] = Math.min(low[u], low[j]);
} else if (in_stk[j]) {
low[u] = Math.min(low[u], dfn[j]);
}
}
if (dfn[u] == low[u]) {
++scc_cnt;
int y;
do {
y = stk[top--];
id[y] = scc_cnt;
size[scc_cnt]++;
} while (y != u);
}
}
private static void add(int a, int b) {
e[idx] = b;
ne[idx] = h[a];
h[a] = idx++;
}
}
例题:367. 学校网络
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static int N = 110;
static int M = 10010;
static int n;
static int[] h = new int[N];
static int[] e = new int[M];
static int[] ne = new int[M];
static int idx;
static int[] dfn = new int[N];
static int[] low = new int[N];
static int timestamp;
static int[] stk = new int[N];
static int top;
static boolean[] in_stk = new boolean[N];
static int[] id = new int[N];
static int scc_cnt;
static int[] din = new int[N];
static int[] dout = new int[N];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
Arrays.fill(h, -1);
for (int i = 1; i <= n; i++) {
while (true) {
int t = sc.nextInt();
if (t == 0) break;
add(i, t);
}
}
for (int i = 1; i <= n; i++) {
if (dfn[i] == 0) tarjan(i);
}
for (int i = 1; i <= n; i++) {
for (int j = h[i]; j != -1; j = ne[j]) {
int k = e[j];
int a = id[i];
int b = id[k];
if (a != b) {
dout[a]++;
din[b]++;
}
}
}
int a = 0, b = 0;
for (int i = 1; i <= scc_cnt; i++) {
if (din[i] == 0) a++;
if (dout[i] == 0) b++;
}
System.out.println(a);
if (scc_cnt == 1) {
System.out.println(0);
} else {
System.out.println(Math.max(a, b));
}
}
private static void tarjan(int u) {
dfn[u] = ++timestamp;
low[u] = dfn[u];
stk[++top] = u;
in_stk[u] = true;
for (int i = h[u]; i != -1; i = ne[i]) {
int j = e[i];
if (dfn[j] == 0) {
tarjan(j);
low[u] = Math.min(low[u], low[j]);
} else if (in_stk[j]) {
low[u] = Math.min(low[u], dfn[j]);
}
}
if (dfn[u] == low[u]) {
++scc_cnt;
int y;
do {
y = stk[top--];
in_stk[y] = false;
id[y] = scc_cnt;
} while (y != u);
}
}
private static void add(int a, int b) {
e[idx] = b;
ne[idx] = h[a];
h[a] = idx++;
}
}
例题:最大半连通子图
【输入样例】
6 6 20070603
1 2
2 1
1 3
2 4
5 6
6 4
【输出样例】
3
3
【提示】
对于 20% 的数据,N≤18;
对于 60% 的数据,N≤104;
对于 100% 的数据,1≤N≤105,1≤M≤106,X≤108。
————————————————————————————————————————————————————
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Main {
static int N = 100010;
static int M = 2000010;
static int n;
static int m;
static int mod;
static int[] h = new int[N];
static int[] hs = new int[N];
static int[] e = new int[M];
static int[] ne = new int[M];
static int idx;
static int[] dfn = new int[N];
static int[] low = new int[N];
static int timestamp;
static int[] stk = new int[N];
static int top;
static boolean[] in_stk = new boolean[N];
static int[] id = new int[N];
static int scc_cnt;
static int[] size = new int[N];
static int[] f = new int[N];
static int[] g = new int[N];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Arrays.fill(h, -1);
Arrays.fill(hs, -1);
n = sc.nextInt();
m = sc.nextInt();
mod = sc.nextInt();
while (m-- > 0) {
int a = sc.nextInt();
int b = sc.nextInt();
add(h, a, b);
}
for (int i = 1; i <= n; i++) {
if (dfn[i] == 0) tarjan(i);
}
//自己实现哈希函数,(u, v) -> u * 10^6 + v
Set<Long> S = new HashSet<>();
for (int i = 1; i <= n; i++) {
for (int j = h[i]; j != -1; j = ne[j]) {
int k = e[j];
int a = id[i];
int b = id[k];
long hash = a * 1000000l + b;
if (a != b && !S.contains(hash)) {
add(hs, a, b);
S.add(hash);
}
}
}
for (int i = scc_cnt; i > 0; i--) {
if (f[i] == 0) {
f[i] = size[i];
g[i] = 1;
}
for (int j = hs[i]; j != -1; j = ne[j]) {
int k = e[j];
if (f[k] < f[i] + size[k]) {
f[k] = f[i] + size[k];
g[k] = g[i];
} else if (f[k] == f[i] + size[k]) {
g[k] = (g[k] + g[i]) % mod;
}
}
}
int maxf = 0, sum = 0;
for (int i = 1; i <= scc_cnt; i++) {
if (f[i] > maxf) {
maxf = f[i];
sum = g[i];
} else if (f[i] == maxf) {
sum = (sum + g[i]) % mod;
}
}
System.out.println(maxf);
System.out.println(sum);
}
private static void tarjan(int u) {
dfn[u] = ++timestamp;
low[u] = dfn[u];
stk[++top] = u;
in_stk[u] = true;
for (int i = h[u]; i != -1; i = ne[i]) {
int j = e[i];
if (dfn[j] == 0) {
tarjan(j);
low[u] = Math.min(low[u], low[j]);
} else if (in_stk[j]) {
low[u] = Math.min(low[u], dfn[j]);
}
}
if (dfn[u] == low[u]) {
++scc_cnt;
int y;
do {
y = stk[top--];//栈顶元素
in_stk[y] = false;
id[y] = scc_cnt;
size[scc_cnt]++;
} while (y != u);
}
}
private static void add(int[] h, int a, int b) {
e[idx] = b;
ne[idx] = h[a];
h[a] = idx++;
}
}
例题:368. 银河
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static int N = 100010;
static int M = 600010;
static int n;
static int m;
static int[] h = new int[N];
static int[] hs = new int[N];
static int[] e = new int[M];
static int[] w = new int[M];
static int[] ne = new int[M];
static int idx;
static int[] dfn = new int[N];
static int[] low = new int[N];
static int timestamp;
static int[] stk = new int[N];
static int top;
static boolean[] in_stk = new boolean[N];
static int[] id = new int[N];
static int scc_cnt;
static int[] size = new int[N];
static int[] dist = new int[N];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
Arrays.fill(h, -1);
Arrays.fill(hs, -1);
for (int i = 1; i <= n; i++) {
add(h, 0, i, 1);
}
while (m-- > 0) {
int t = sc.nextInt();
int a = sc.nextInt();
int b = sc.nextInt();
if (t == 1) {
add(h, b, a, 0);
add(h, a, b, 0);
} else if (t == 2) {
add(h, a, b, 1);
} else if (t == 3) {
add(h, b, a, 0);
} else if (t == 4) {
add(h, b, a, 1);
} else {
add(h, a, b, 0);
}
}
tarjan(0);
boolean success = true;
for (int i = 0; i <= n; i++) {
for (int j = h[i]; j != -1; j = ne[j]) {
int k = e[j];
int a = id[i];
int b = id[k];
if (a == b) {
if (w[j] > 0) {
success = false;
break;
}
} else {
add(hs, a, b, w[j]);
}
}
if (!success) break;
}
if (!success) {
System.out.println(-1);
} else {
for (int i = scc_cnt; i > 0; i--) {
for (int j = hs[i]; j != -1; j = ne[j]) {
int k = e[j];
dist[k] = Math.max(dist[k], dist[i] + w[j]);
}
}
long res = 0;
for (int i = 1; i <= scc_cnt; i++) {
res += (long)dist[i] * size[i];
}
System.out.println(res);
}
}
private static void tarjan(int u) {
dfn[u] = ++timestamp;
low[u] = dfn[u];
stk[++top] = u;
in_stk[u] = true;
for (int i = h[u]; i != -1; i = ne[i]) {
int j = e[i];
if (dfn[j] == 0) {
tarjan(j);
low[u] = Math.min(low[u], low[j]);
} else if (in_stk[j]) {
low[u] = Math.min(low[u], dfn[j]);
}
}
if (dfn[u] == low[u]) {
++scc_cnt;
int y;
do {
y = stk[top--];//栈顶元素
in_stk[y] = false;
id[y] = scc_cnt;
size[scc_cnt]++;
} while (y != u);
}
}
private static void add(int[] h, int a, int b, int c) {
e[idx] = b;
w[idx] = c;
ne[idx] = h[a];
h[a] = idx++;
}
}