1. private static int gcd(int a, int b) {
    2. return (b == 0) ? a : gcd(b, a % b);
    3. }

    1. private static int qpow(int a, int b, int p) {
    2. int res = (int) 1;
    3. while (b > 0) {
    4. if ((b & 1) == 1) {
    5. res = res * a % p;
    6. }
    7. a = a * a % p;
    8. b >>= 1;
    9. }
    10. return res;
    11. }

    1. private static int find(int x) {
    2. return (int) ((x == f[x]) ? x : (f[x] = find(f[x])));
    3. }
    4. private static void merge(int x, int y) {
    5. int f1 = find(x);
    6. int f2 = find(y);
    7. f[f1] = f2;
    8. }

    1. for (int i = 1; i <= n; i ++ ) {
    2. for (int j = 1; j <= n; j ++ ) {
    3. if (i == j) {
    4. g[i][j] = 0;
    5. } else {
    6. g[i][j] = INF;
    7. }
    8. }
    9. }
    10. private static void floyd() {
    11. for (int k = 1; k <= n; k ++ ) {
    12. for (int i = 1; i <= n; i ++ ) {
    13. for (int j = 1; j <= n; j ++ ) {
    14. g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]);
    15. }
    16. }
    17. }
    18. }

    1. private static int kruskal() {
    2. int res = 0;
    3. int cnt = 0;
    4. Arrays.sort(edges, 1, m + 1);
    5. for (int i = 1; i <= n; i ++ ) {
    6. p[i] = i;
    7. }
    8. for (int i = 1; i <= m; i ++ ) {
    9. int u = edges[i].u;
    10. int v = edges[i].v;
    11. int w = edges[i].w;
    12. int f1 = find(u);
    13. int f2 = find(v);
    14. if (f1 != f2) {
    15. p[f1] = f2;
    16. res += w;
    17. cnt ++ ;
    18. }
    19. }
    20. if (cnt < n - 1) {
    21. return INF;
    22. }
    23. return res;
    24. }
    25. private static int find(int x) {
    26. return (x == p[x]) ? x : (p[x] = find(p[x]));
    27. }
    28. class node implements Comparable<node> {
    29. public int u;
    30. public int v;
    31. public int w;
    32. node(int u, int v, int w) {
    33. this.u = u;
    34. this.v = v;
    35. this.w = w;
    36. }
    37. @Override
    38. public int compareTo(node o) {
    39. return Integer.compare(w, o.w);
    40. }
    41. }