From 88b634d82dac7a06afdd0054cc4440389f3e5fec Mon Sep 17 00:00:00 2001 From: usaco site Date: Thu, 29 Oct 2026 12:46:22 +0000 Subject: [PATCH] Start palindromic-path-queries-in-a-tree --- .../palindromic-path-queries-in-a-tree/.url | 1 + .../driver.h | 16 +++ .../main.cpp | 102 ++++++++++++++++++ .../palindromic-path-queries-in-a-tree.in | 8 ++ .../problem.md | 57 ++++++++++ .../tests/1.in | 4 + .../tests/1.out | 1 + .../tests/2.in | 4 + .../tests/2.out | 1 + 9 files changed, 194 insertions(+) create mode 100644 solving/palindromic-path-queries-in-a-tree/.url create mode 100644 solving/palindromic-path-queries-in-a-tree/driver.h create mode 100644 solving/palindromic-path-queries-in-a-tree/main.cpp create mode 100644 solving/palindromic-path-queries-in-a-tree/palindromic-path-queries-in-a-tree.in create mode 100644 solving/palindromic-path-queries-in-a-tree/problem.md create mode 100644 solving/palindromic-path-queries-in-a-tree/tests/1.in create mode 100644 solving/palindromic-path-queries-in-a-tree/tests/1.out create mode 100644 solving/palindromic-path-queries-in-a-tree/tests/2.in create mode 100644 solving/palindromic-path-queries-in-a-tree/tests/2.out diff --git a/solving/palindromic-path-queries-in-a-tree/.url b/solving/palindromic-path-queries-in-a-tree/.url new file mode 100644 index 0000000..bbd3dca --- /dev/null +++ b/solving/palindromic-path-queries-in-a-tree/.url @@ -0,0 +1 @@ +https://leetcode.com/problems/palindromic-path-queries-in-a-tree/ diff --git a/solving/palindromic-path-queries-in-a-tree/driver.h b/solving/palindromic-path-queries-in-a-tree/driver.h new file mode 100644 index 0000000..d332357 --- /dev/null +++ b/solving/palindromic-path-queries-in-a-tree/driver.h @@ -0,0 +1,16 @@ +// driver.h - generated by 'usaco new' from LeetCode's metadata (edit freely). +// Reads palindromic-path-queries-in-a-tree.in in LeetCode's testcase format, cases back to back: +// one argument per line +// and writes one answer per case to palindromic-path-queries-in-a-tree.out, formatted the way LeetCode +// shows its output. Parsing/printing helpers: leetcode.h. +int main() { + freopen("palindromic-path-queries-in-a-tree.in", "r", stdin); + freopen("palindromic-path-queries-in-a-tree.out", "w", stdout); + for (vector in; lc::read_case(in, 4);) { + auto n = lc::parse(in[0]); + auto edges = lc::parse>>(in[1]); + auto s = lc::parse(in[2]); + auto queries = lc::parse>(in[3]); + cout << lc::show(Solution().palindromePath(n, edges, s, queries)) << '\n'; + } +} diff --git a/solving/palindromic-path-queries-in-a-tree/main.cpp b/solving/palindromic-path-queries-in-a-tree/main.cpp new file mode 100644 index 0000000..8b11e30 --- /dev/null +++ b/solving/palindromic-path-queries-in-a-tree/main.cpp @@ -0,0 +1,102 @@ +#include +using namespace std; +#ifdef LOCAL // set by ./usaco builds only, so this is safe to submit as-is +#include "debugging.h" +#include "leetcode.h" // ListNode, TreeNode +#endif + + +// ----- TYPES ----- +typedef long long ll; +typedef pair pii; +typedef vector vi; + +// ----- FILE IO ----- +#define STDIN(filename) freopen(filename, "r", stdin) +#define STDOUT(filename) freopen(filename, "w", stdout) +#define FASTIO cin.tie(0)->sync_with_stdio(0) +#define STDIO(filename) \ + FASTIO; \ + string filenamee(filename); \ + STDIN((filenamee + ".in").c_str()); \ + STDOUT((filenamee + ".out").c_str()) +#define NL '\n' +#define CM ", " +#define SP " " + +// ----- FOR LOOPS ----- +#define F(i, L, R) for (int i = L; i < R; i++) +#define FE(i, L, R) for (int i = L; i <= R; i++) +#define FD(i, L, R) for (int i = L; i > R; i--) +#define FDE(i, L, R) for (int i = L; i >= R; i--) + +// ----- WEZ ----- +#define takeI(n) \ + int n; \ + cin >> n +#define takeII(n, m) \ + int n, m; \ + cin >> n >> m +#define takeIII(n, m, k) \ + int n, m, k; \ + cin >> n >> m >> k +#define takeL(n) \ + ll n; \ + cin >> n +#define takeS(s) \ + string s; \ + cin >> s +#define takeV(v, n) \ + vi v(n); \ + for (auto &x : v) \ + cin >> x +#define takeVL(v, n) \ + vector v(n); \ + for (auto &x : v) \ + cin >> x +#define whileZ \ + takeI(T); \ + while (T--) +// +// ----- MAX MIN ----- +#define REMAX(a, b) (a) = max((a), (b)) +#define REMIN(a, b) (a) = min((a), (b)) + +// ----- PAIRS ----- +#define fi first +#define se second + +// ----- OUTPUT ----- +#define YESNO(b) cout << ((b) ? "YES" : "NO") << NL + +// ----- DEBUG ----- (compiled out on the judge) +#ifdef LOCAL +#define DBG(vari) cerr << #vari << " = " << (vari) << endl +#define DBG2(a, b) cerr << #a "=" << (a) << CM << #b "=" << (b) << endl +#define printA(a, L, R) FE(i, L, R) cerr << a[i] << (i == R ? '\n' : ' ') +#define printV(a) printA(a, 0, SZ(a) - 1) +#else +#define DBG(vari) +#define DBG2(a, b) +#define printA(a, L, R) +#define printV(a) +#endif + +// ----- STL DS +#define ALL(c) (c).begin(), (c).end() +#define SZ(c) ((int)(c).size()) +#define SORT(c) sort(ALL(c)) +#define RSORT(c) sort(ALL(c), greater<>()) +#define UNIQ(c) SORT(c), (c).erase(unique(ALL(c)), (c).end()) +#define pb push_back + +class Solution { +public: + vector palindromePath(int n, vector>& edges, string s, vector& queries) { + + } +}; + +#ifdef LOCAL // runs the examples; see driver.h +#include "driver.h" +#endif diff --git a/solving/palindromic-path-queries-in-a-tree/palindromic-path-queries-in-a-tree.in b/solving/palindromic-path-queries-in-a-tree/palindromic-path-queries-in-a-tree.in new file mode 100644 index 0000000..6942194 --- /dev/null +++ b/solving/palindromic-path-queries-in-a-tree/palindromic-path-queries-in-a-tree.in @@ -0,0 +1,8 @@ +3 +[[0,1],[1,2]] +"aac" +["query 0 2","update 1 b","query 0 2"] +4 +[[0,1],[0,2],[0,3]] +"abca" +["query 1 2","update 0 b","query 2 3","update 3 a","query 1 3"] diff --git a/solving/palindromic-path-queries-in-a-tree/problem.md b/solving/palindromic-path-queries-in-a-tree/problem.md new file mode 100644 index 0000000..ffe284b --- /dev/null +++ b/solving/palindromic-path-queries-in-a-tree/problem.md @@ -0,0 +1,57 @@ +# 3841. Palindromic Path Queries in a Tree + +You are given an undirected tree with `n` nodes labeled 0 to `n - 1`. This is represented by a 2D array `edges` of length `n - 1`, where `edges[i] = [ui, vi]` indicates an undirected edge between nodes `ui` and `vi`. + +You are also given a string `s` of length `n` consisting of lowercase English letters, where `s[i]` represents the character assigned to node `i`. + +You are also given a string array `queries`, where each `queries[i]` is either: + +- `"update ui c"`: Change the character at node `ui` to `c`. Formally, update `s[ui] = c`. +- `"query ui vi"`: Determine whether the string formed by the characters on the **unique** path from `ui` to `vi` (inclusive) can be **rearranged** into a **palindrome**. + +Return a boolean array `answer`, where `answer[j]` is `true` if the `j^th` query of type `"query ui vi"​​​​​​​` can be rearranged into a **palindrome**, and `false` otherwise. + +**Example 1:** + +**Input:** n = 3, edges = [[0,1],[1,2]], s = "aac", queries = ["query 0 2","update 1 b","query 0 2"] + +**Output:** [true,false] + +**Explanation:** + +- `"query 0 2"`: Path `0 → 1 → 2` gives `"aac"`, which can be rearranged to form `"aca"`, a palindrome. Thus, `answer[0] = true`. +- `"update 1 b"`: Update node 1 to `'b'`, now `s = "abc"`. +- `"query 0 2"`: Path characters are `"abc"`, which cannot be rearranged to form a palindrome. Thus, `answer[1] = false`. + +Thus, `answer = [true, false]`. + +**Example 2:** + +**Input:** n = 4, edges = [[0,1],[0,2],[0,3]], s = "abca", queries = ["query 1 2","update 0 b","query 2 3","update 3 a","query 1 3"] + +**Output:** [false,false,true] + +**Explanation:** + +- `"query 1 2"`: Path `1 → 0 → 2` gives `"bac"`, which cannot be rearranged to form a palindrome. Thus, `answer[0] = false`. +- `"update 0 b"`: Update node 0 to `'b'`, now `s = "bbca"`. +- `"query 2 3"`: Path `2 → 0 → 3` gives `"cba"`, which cannot be rearranged to form a palindrome. Thus, `answer[1] = false`. +- `"update 3 a"`: Update node 3 to `'a'`, `s = "bbca"`. +- `"query 1 3"`: Path `1 → 0 → 3` gives `"bba"`, which can be rearranged to form `"bab"`, a palindrome. Thus, `answer[2] = true`. + +Thus, `answer = [false, false, true]`. + +**Constraints:** + +- `1 <= n == s.length <= 5 * 10^4` +- `edges.length == n - 1` +- `edges[i] = [ui, vi]` +- `0 <= ui, vi <= n - 1` +- `s` consists of lowercase English letters. +- The input is generated such that `edges` represents a valid tree. +- `1 <= queries.length <= 5 * 10^4`​​​​​​​ + + - `queries[i] = "update ui c"` or + - `queries[i] = "query ui vi"` + - `0 <= ui, vi <= n - 1` + - `c` is a lowercase English letter. diff --git a/solving/palindromic-path-queries-in-a-tree/tests/1.in b/solving/palindromic-path-queries-in-a-tree/tests/1.in new file mode 100644 index 0000000..76999f2 --- /dev/null +++ b/solving/palindromic-path-queries-in-a-tree/tests/1.in @@ -0,0 +1,4 @@ +3 +[[0,1],[1,2]] +"aac" +["query 0 2","update 1 b","query 0 2"] diff --git a/solving/palindromic-path-queries-in-a-tree/tests/1.out b/solving/palindromic-path-queries-in-a-tree/tests/1.out new file mode 100644 index 0000000..b29ad08 --- /dev/null +++ b/solving/palindromic-path-queries-in-a-tree/tests/1.out @@ -0,0 +1 @@ +[true,false] diff --git a/solving/palindromic-path-queries-in-a-tree/tests/2.in b/solving/palindromic-path-queries-in-a-tree/tests/2.in new file mode 100644 index 0000000..b22e1e6 --- /dev/null +++ b/solving/palindromic-path-queries-in-a-tree/tests/2.in @@ -0,0 +1,4 @@ +4 +[[0,1],[0,2],[0,3]] +"abca" +["query 1 2","update 0 b","query 2 3","update 3 a","query 1 3"] diff --git a/solving/palindromic-path-queries-in-a-tree/tests/2.out b/solving/palindromic-path-queries-in-a-tree/tests/2.out new file mode 100644 index 0000000..b08abb4 --- /dev/null +++ b/solving/palindromic-path-queries-in-a-tree/tests/2.out @@ -0,0 +1 @@ +[false,false,true]