From 69074c9ec49bd206df759fd3f158429e2e4de303 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: Sun, 13 Jul 2025 01:41:24 -0700 Subject: [PATCH 1/5] Create 3615.Longest-Palindromic-Path-in-Graph.cpp --- ...3615.Longest-Palindromic-Path-in-Graph.cpp | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 DFS/3615.Longest-Palindromic-Path-in-Graph/3615.Longest-Palindromic-Path-in-Graph.cpp diff --git a/DFS/3615.Longest-Palindromic-Path-in-Graph/3615.Longest-Palindromic-Path-in-Graph.cpp b/DFS/3615.Longest-Palindromic-Path-in-Graph/3615.Longest-Palindromic-Path-in-Graph.cpp new file mode 100644 index 000000000..bfb5afb00 --- /dev/null +++ b/DFS/3615.Longest-Palindromic-Path-in-Graph/3615.Longest-Palindromic-Path-in-Graph.cpp @@ -0,0 +1,48 @@ +class Solution { + vectoradj[14]; + int memo[14][14][1<<14]; + string label; +public: + int dfs(int u, int v, int mask) { + if (memo[u][v][mask]!=-1) return memo[u][v][mask]; + int ret = 0; + for (int u2: adj[u]) { + if (mask&(1<>& edges, string label) { + this->label = label; + for (auto& e: edges) { + adj[e[0]].push_back(e[1]); + adj[e[1]].push_back(e[0]); + } + memset(memo, -1, sizeof(memo)); + + int ret = 1; + for (int u=0; u Date: Sun, 13 Jul 2025 01:42:54 -0700 Subject: [PATCH 2/5] Update Readme.md --- Readme.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Readme.md b/Readme.md index 68e0d1168..d02bed1d6 100644 --- a/Readme.md +++ b/Readme.md @@ -585,6 +585,7 @@ [2741.Special-Permutations](https://github.com/wisdompeak/LeetCode/tree/master/DFS/2741.Special-Permutations) (M+) [2746.Decremental-String-Concatenation](https://github.com/wisdompeak/LeetCode/tree/master/DFS/2746.Decremental-String-Concatenation) (H-) [3213.Construct-String-with-Minimum-Cost](https://github.com/wisdompeak/LeetCode/tree/master/DFS/3213.Construct-String-with-Minimum-Cost) (H-) +[3615.Longest-Palindromic-Path-in-Graph](https://github.com/wisdompeak/LeetCode/tree/master/DFS/3615.Longest-Palindromic-Path-in-Graph) (H-) * ``hidden matrix`` [489.Robot-Room-Cleaner](https://github.com/wisdompeak/LeetCode/blob/master/DFS/489.Robot-Room-Cleaner) (H) [1778.Shortest-Path-in-a-Hidden-Grid](https://github.com/wisdompeak/LeetCode/tree/master/DFS/1778.Shortest-Path-in-a-Hidden-Grid) (H-) From 615e9482aa6b7b47e007f7793e2d0ece4a4c1a95 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: Sun, 13 Jul 2025 01:51:48 -0700 Subject: [PATCH 3/5] Create Readme.md --- DFS/3615.Longest-Palindromic-Path-in-Graph/Readme.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 DFS/3615.Longest-Palindromic-Path-in-Graph/Readme.md diff --git a/DFS/3615.Longest-Palindromic-Path-in-Graph/Readme.md b/DFS/3615.Longest-Palindromic-Path-in-Graph/Readme.md new file mode 100644 index 000000000..876fe6f92 --- /dev/null +++ b/DFS/3615.Longest-Palindromic-Path-in-Graph/Readme.md @@ -0,0 +1,9 @@ +### 3615.Longest-Palindromic-Path-in-Graph + +考虑到本题的规模很小`n<=14`,我们可以考虑穷举回文路径的构造。 + +首先考虑长度为奇数的路径。我们可以从任意一个节点作为中间元素,不断往两边添加对称字符的节点。因此我们需要记录路径最两端的节点编号u和v。每个回合,对于已知的回文路径(u,v),我们找u的所有邻接节点u2,v的所有邻接节点v2,如果恰好u2和v2的字符相同,那么我们就可以将回文路径扩展成为(u2,v2). 为了提高搜索效率和避免同一个节点被多次使用,我们可以用bit mask记录该路径已经用过了哪些节点,来避免重复的状态。 + +另一种情况是考虑长度为偶数的路径。初始状态时,我们需要遍历所有相邻的节点u和v,如果他们的字符相同,那么就可以从(u,v)开始,做与之前一样的递归搜索。 + +第二种情况耗时更多一些。总的时间复杂度是`o(n^2)*(2^n)`。 From 38b6410141e97c9c84c853d8b46f7ac735ceb789 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: Sun, 13 Jul 2025 01:58:39 -0700 Subject: [PATCH 4/5] Update Readme.md --- DFS/3615.Longest-Palindromic-Path-in-Graph/Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DFS/3615.Longest-Palindromic-Path-in-Graph/Readme.md b/DFS/3615.Longest-Palindromic-Path-in-Graph/Readme.md index 876fe6f92..1dc592508 100644 --- a/DFS/3615.Longest-Palindromic-Path-in-Graph/Readme.md +++ b/DFS/3615.Longest-Palindromic-Path-in-Graph/Readme.md @@ -2,7 +2,7 @@ 考虑到本题的规模很小`n<=14`,我们可以考虑穷举回文路径的构造。 -首先考虑长度为奇数的路径。我们可以从任意一个节点作为中间元素,不断往两边添加对称字符的节点。因此我们需要记录路径最两端的节点编号u和v。每个回合,对于已知的回文路径(u,v),我们找u的所有邻接节点u2,v的所有邻接节点v2,如果恰好u2和v2的字符相同,那么我们就可以将回文路径扩展成为(u2,v2). 为了提高搜索效率和避免同一个节点被多次使用,我们可以用bit mask记录该路径已经用过了哪些节点,来避免重复的状态。 +首先考虑长度为奇数的路径。我们可以从任意一个节点作为中间元素,不断往两边添加对称字符的节点。因此我们需要记录路径最两端的节点编号u和v。每个回合,对于已知的回文路径(u,v),我们找u的所有邻接节点u2,v的所有邻接节点v2,如果恰好u2和v2的字符相同,那么我们就可以将回文路径扩展成为(u2,v2),同时路径长度增2. 为了提高搜索效率和避免同一个节点被多次使用,我们可以用bit mask记录该路径已经用过了哪些节点,来避免重复的状态。 另一种情况是考虑长度为偶数的路径。初始状态时,我们需要遍历所有相邻的节点u和v,如果他们的字符相同,那么就可以从(u,v)开始,做与之前一样的递归搜索。 From fdc9b7d7ad8020a5d31a3e11031c755ac9cd396a Mon Sep 17 00:00:00 2001 From: wisdompeak Date: Sun, 13 Jul 2025 01:58:47 -0700 Subject: [PATCH 5/5] Update 3615.Longest-Palindromic-Path-in-Graph.cpp --- .../3615.Longest-Palindromic-Path-in-Graph.cpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/DFS/3615.Longest-Palindromic-Path-in-Graph/3615.Longest-Palindromic-Path-in-Graph.cpp b/DFS/3615.Longest-Palindromic-Path-in-Graph/3615.Longest-Palindromic-Path-in-Graph.cpp index bfb5afb00..bb03c45b2 100644 --- a/DFS/3615.Longest-Palindromic-Path-in-Graph/3615.Longest-Palindromic-Path-in-Graph.cpp +++ b/DFS/3615.Longest-Palindromic-Path-in-Graph/3615.Longest-Palindromic-Path-in-Graph.cpp @@ -13,7 +13,7 @@ class Solution { if (mask&(1<