|
| 1 | +/////////////////////// Template //////////////////////////////////////// |
| 2 | +const initializeGraph = (n) => { let g = []; for (let i = 0; i < n; i++) { g.push([]); } return g; }; |
| 3 | +const packUG = (g, edges) => { for (const [u, v] of edges) { g[u].push(v); g[v].push(u); } }; |
| 4 | +const initialize2DArray = (n, m) => { let d = []; for (let i = 0; i < n; i++) { let t = Array(m).fill(Number.MAX_SAFE_INTEGER); d.push(t); } return d; }; |
| 5 | + |
| 6 | +function DJSet(n) { |
| 7 | + // parent[i] < 0, -parent[i] is the group size which root is i. example: (i -> parent[i] -> parent[parent[i]] -> parent[parent[parent[i]]] ...) |
| 8 | + // parent[i] >= 0, i is not the root and parent[i] is i's parent. example: (... parent[parent[parent[i]]] -> parent[parent[i]] -> parent[i] -> i) |
| 9 | + let parent = Array(n).fill(-1); |
| 10 | + return { find, union, count, equiv, par } |
| 11 | + function find(x) { |
| 12 | + return parent[x] < 0 ? x : parent[x] = find(parent[x]); |
| 13 | + } |
| 14 | + function union(x, y) { |
| 15 | + x = find(x); |
| 16 | + y = find(y); |
| 17 | + if (x != y) { |
| 18 | + if (parent[x] < parent[y]) [x, y] = [y, x]; |
| 19 | + parent[x] += parent[y]; |
| 20 | + parent[y] = x; |
| 21 | + } |
| 22 | + return x == y; |
| 23 | + } |
| 24 | + function count() { // total groups |
| 25 | + return parent.filter(v => v < 0).length; |
| 26 | + } |
| 27 | + function equiv(x, y) { // isConnected |
| 28 | + return find(x) == find(y); |
| 29 | + } |
| 30 | + function par() { |
| 31 | + return parent; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +const isBipartite = (g) => { |
| 36 | + let n = g.length, start = 1, visit = Array(n).fill(false), q = [], color = Array(n).fill(0); // 0: no color, 1: red -1: blue |
| 37 | + for (let i = start; i < n; i++) { |
| 38 | + if (color[i] != 0) continue; |
| 39 | + q.push(i); |
| 40 | + color[i] = 1; |
| 41 | + if (visit[i]) continue; |
| 42 | + while (q.length) { |
| 43 | + let cur = q.shift(); |
| 44 | + if (visit[cur]) continue; |
| 45 | + for (const child of g[cur]) { |
| 46 | + if (color[child] == color[cur]) return false; |
| 47 | + if (color[child]) continue; |
| 48 | + color[child] = -color[cur]; |
| 49 | + q.push(child); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + return true; |
| 54 | +}; |
| 55 | +//////////////////////////////////////////////////////////////////// |
| 56 | +/** |
| 57 | + * @param {number} n |
| 58 | + * @param {number[][]} edges |
| 59 | + * @return {number} |
| 60 | + */ |
| 61 | +const magnificentSets = (n, edges) => { |
| 62 | + let g = initializeGraph(n + 1), ds = new DJSet(n + 1); |
| 63 | + packUG(g, edges); |
| 64 | + if (!isBipartite(g)) return -1; |
| 65 | + let d = initialize2DArray(n + 1, n + 1), res = Array(n + 1).fill(0); |
| 66 | + for (let i = 1; i <= n; i++) d[i][i] = 0; |
| 67 | + for (const [u, v] of edges) { |
| 68 | + d[u][v] = 1; |
| 69 | + d[v][u] = 1; |
| 70 | + ds.union(u, v); |
| 71 | + } |
| 72 | + wf(d); |
| 73 | + for (let i = 1; i <= n; i++) { |
| 74 | + let max = 0; |
| 75 | + for (let j = 1; j <= n; j++) { |
| 76 | + if (d[i][j] >= Number.MAX_SAFE_INTEGER) continue; |
| 77 | + max = Math.max(max, d[i][j]); |
| 78 | + } |
| 79 | + let par = ds.find(i); |
| 80 | + res[par] = Math.max(res[par], max + 1); |
| 81 | + } |
| 82 | + let ans = 0; |
| 83 | + for (let i = 1; i <= n; i++) ans += res[i]; |
| 84 | + return ans; |
| 85 | +}; |
| 86 | + |
| 87 | +const wf = (g) => { |
| 88 | + let n = g.length; |
| 89 | + for (let k = 0; k < n; k++) { |
| 90 | + for (let i = 0; i < n; i++) { |
| 91 | + for (let j = 0; j < n; j++) { |
| 92 | + if (g[i][j] > g[i][k] + g[k][j]) { |
| 93 | + g[i][j] = g[i][k] + g[k][j]; |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | +}; |
0 commit comments