|
| 1 | +/* |
| 2 | +
|
| 3 | +
|
| 4 | +-* 2246. Longest Path With Different Adjacent Characters *- |
| 5 | +
|
| 6 | +
|
| 7 | +You are given a tree (i.e. a connected, undirected graph that has no cycles) rooted at node 0 consisting of n nodes numbered from 0 to n - 1. The tree is represented by a 0-indexed array parent of size n, where parent[i] is the parent of node i. Since node 0 is the root, parent[0] == -1. |
| 8 | +
|
| 9 | +You are also given a string s of length n, where s[i] is the character assigned to node i. |
| 10 | +
|
| 11 | +Return the length of the longest path in the tree such that no pair of adjacent nodes on the path have the same character assigned to them. |
| 12 | +
|
| 13 | + |
| 14 | +
|
| 15 | +Example 1: |
| 16 | +
|
| 17 | +
|
| 18 | +Input: parent = [-1,0,0,1,1,2], s = "abacbe" |
| 19 | +Output: 3 |
| 20 | +Explanation: The longest path where each two adjacent nodes have different characters in the tree is the path: 0 -> 1 -> 3. The length of this path is 3, so 3 is returned. |
| 21 | +It can be proven that there is no longer path that satisfies the conditions. |
| 22 | +Example 2: |
| 23 | +
|
| 24 | +
|
| 25 | +Input: parent = [-1,0,0,0], s = "aabc" |
| 26 | +Output: 3 |
| 27 | +Explanation: The longest path where each two adjacent nodes have different characters is the path: 2 -> 0 -> 3. The length of this path is 3, so 3 is returned. |
| 28 | + |
| 29 | +
|
| 30 | +Constraints: |
| 31 | +
|
| 32 | +n == parent.length == s.length |
| 33 | +1 <= n <= 105 |
| 34 | +0 <= parent[i] <= n - 1 for all i >= 1 |
| 35 | +parent[0] == -1 |
| 36 | +parent represents a valid tree. |
| 37 | +s consists of only lowercase English letters. |
| 38 | +
|
| 39 | +
|
| 40 | +
|
| 41 | +
|
| 42 | +
|
| 43 | +
|
| 44 | +*/ |
| 45 | + |
| 46 | +import 'dart:collection'; |
| 47 | +import 'dart:math'; |
| 48 | + |
| 49 | +class Node { |
| 50 | + String c; |
| 51 | + List<Node> next = []; |
| 52 | + Node(this.c); |
| 53 | + |
| 54 | + void add(Node n) { |
| 55 | + n.next.add(this); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +class A { |
| 60 | + int maxi = 0; |
| 61 | + int longestPath(List<int> parent, String s) { |
| 62 | + List<Node> list = List.generate(parent.length, (i) => Node(s[i])); |
| 63 | + for (int i = 1; i < list.length; i++) { |
| 64 | + list[i].add(list[parent[i]]); |
| 65 | + } |
| 66 | + iterate(list[0]); |
| 67 | + return maxi; |
| 68 | + } |
| 69 | + |
| 70 | + int iterate(Node root) { |
| 71 | + if (root == null) { |
| 72 | + return 0; |
| 73 | + } |
| 74 | + int myMax = 1; |
| 75 | + for (Node n in root.next) { |
| 76 | + int count = iterate(n); |
| 77 | + if (n.c != root.c) { |
| 78 | + if (myMax != 1) { |
| 79 | + maxi = max(myMax + count, maxi); |
| 80 | + } |
| 81 | + myMax = max(count + 1, myMax); |
| 82 | + } |
| 83 | + } |
| 84 | + maxi = max(myMax, maxi); |
| 85 | + return myMax; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +class Pair { |
| 90 | + int count, maxCount; |
| 91 | + Pair(this.count, this.maxCount); |
| 92 | +} |
| 93 | + |
| 94 | +class B { |
| 95 | + int longestPath(List<int> parent, String s) { |
| 96 | + List<List<int>> a = List.generate(parent.length, (_) => []); |
| 97 | + for (int i = 1; i < parent.length; i++) { |
| 98 | + a[parent[i]].add(i); |
| 99 | + } |
| 100 | + Pair p = dfs(0, a, s); |
| 101 | + return p.maxCount; |
| 102 | + } |
| 103 | + |
| 104 | + Pair dfs(int root, List<List<int>> a, String s) { |
| 105 | + if (a[root].length == 0) return Pair(1, 1); |
| 106 | + int maxCount = 0, count1 = 0, count2 = 0; |
| 107 | + for (int child in a[root]) { |
| 108 | + Pair pair = dfs(child, a, s); |
| 109 | + maxCount = max(maxCount, pair.maxCount); |
| 110 | + if (s[root] != s[child]) { |
| 111 | + if (count1 < pair.count) { |
| 112 | + count2 = count1; |
| 113 | + count1 = pair.count; |
| 114 | + } else if (pair.count > count2) { |
| 115 | + count2 = pair.count; |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + maxCount = max(maxCount, count1 + count2 + 1); |
| 120 | + return Pair(count1 + 1, maxCount); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +class Solution { |
| 125 | + Map<List<int>, int> _cache = {}; |
| 126 | + int longest(Map<int, List<int>> tree, int root, String s, bool branch) { |
| 127 | + int l = 1; |
| 128 | + List<int> key = [root, branch ? 1 : 0]; |
| 129 | + if (_cache.containsKey(key)) { |
| 130 | + return _cache[key] ?? 0; |
| 131 | + } |
| 132 | + if (branch) { |
| 133 | + int best = 0; |
| 134 | + int sbest = 0; |
| 135 | + for (int j in tree[root] ?? []) { |
| 136 | + if (s[root] != s[j]) { |
| 137 | + int currl = longest(tree, j, s, false); |
| 138 | + List<int> temp = [sbest, best, currl]; |
| 139 | + temp.sort(); |
| 140 | + sbest = temp[1]; |
| 141 | + best = temp[2]; |
| 142 | + } |
| 143 | + } |
| 144 | + l = max(l, 1 + best + sbest); |
| 145 | + _cache[key] = l; |
| 146 | + return l; |
| 147 | + } |
| 148 | + for (int j in tree[root] ?? []) { |
| 149 | + if (s[root] != s[j]) { |
| 150 | + int currl = longest(tree, j, s, false); |
| 151 | + l = max(l, 1 + currl); |
| 152 | + } |
| 153 | + } |
| 154 | + _cache[key] = l; |
| 155 | + return l; |
| 156 | + } |
| 157 | + |
| 158 | + int longestPath(List<int> parent, String s) { |
| 159 | + int n = s.length; |
| 160 | + _cache = {}; |
| 161 | + Map<int, List<int>> tree = Map<int, List<int>>(); |
| 162 | + for (int i = 0; i < n; i++) { |
| 163 | + if (parent[i] != -1) { |
| 164 | + tree.putIfAbsent(parent[i], () => []); |
| 165 | + tree[parent[i]]?.add(i); |
| 166 | + } |
| 167 | + } |
| 168 | + int mpath = 0; |
| 169 | + for (int i = 0; i < n; i++) { |
| 170 | + int l = longest(tree, i, s, true); |
| 171 | + mpath = max(mpath, l); |
| 172 | + } |
| 173 | + return mpath; |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +class D { |
| 178 | + int longestPath(List<int> parent, String s) { |
| 179 | + int n = parent.length, res = 1; |
| 180 | + List<int> cnt = List.filled(n, 0), |
| 181 | + top1 = List.filled(n, 1), |
| 182 | + top2 = List.filled(n, 1); |
| 183 | + for (int i = 1; i < n; ++i) ++cnt[parent[i]]; |
| 184 | + Queue<int> q = Queue(); |
| 185 | + for (int i = 1; i < n; ++i) if (cnt[i] == 0) q.add(i); |
| 186 | + while (q.isNotEmpty && q.first != 0) { |
| 187 | + int i = q.first; |
| 188 | + int p = parent[i]; |
| 189 | + q.removeFirst(); |
| 190 | + int length = 0; |
| 191 | + if (length == 1 + (s[i] != s[p] ? top1[i] : 0)) { |
| 192 | + top1[p] <= length; |
| 193 | + top2[p] = top1[p]; |
| 194 | + top1[p] = length; |
| 195 | + } else |
| 196 | + top2[p] = max(top2[p], length); |
| 197 | + if (--cnt[p] == 0) { |
| 198 | + q.add(p); |
| 199 | + res = max(res, top1[p] + top2[p] - 1); |
| 200 | + } |
| 201 | + } |
| 202 | + return res; |
| 203 | + } |
| 204 | +} |
0 commit comments