109 年 國立陽明交通大學資訊工程學系碩士班《資料結構與演算法》

📄 以下 2 題共用同一段題幹

[6%] Let GG be a connected undirected simple graph of 6 nodes and 8 edges, and let TT be a depth-first search (DFS) tree of GG rooted at node rr. It is known that rr has degree 5 in GG.

第 1-(1) 題

rr may have degree ____ in TT.

(A) 1
(B) 2
(C) 3
(D) 4
(E) 5

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

連通圖有 66 個節點,因此任何 DFS 樹都含 6−1=56-1=5 條樹邊。原圖有 88 條邊,所以有

8−5=38-5=3

條非樹邊。

在無向圖的 DFS 中,每條非樹邊都連接某個節點與它的祖先;不同 DFS 子樹之間不會有非樹邊。題目又給出 rr 在原圖中的度數為 55,因此 rr 與其餘五個節點都相鄰。

解題方法

設 rr 在 DFS 樹中的度數為 dd,也就是 rr 的子節點數。

rr 連到五個其他節點,其中 dd 條邊是 DFS 樹邊,另外 5−d5-d 條是非樹邊。由於全圖只有 33 條非樹邊,必須有

5−d≤35-d \le 3

所以 d≥2d\ge 2,選項 (A) 的 11 不可能。

再檢查 d=4d=4 與 d=5d=5:

  • 若 d=4d=4,五個非根節點分在四棵以 rr 的子節點為根的子樹中,只有一棵子樹能多出一個節點,其餘三棵都是單一節點。根的子樹之間不能有非樹邊;唯一含兩個節點的子樹中,兩節點間的邊已是樹邊。因此,除了連到 rr 的非樹邊以外,沒有其他非樹邊可用。但要湊足全圖的三條非樹邊,還需要兩條,故不可能。
  • 若 d=5d=5,五個非根節點全是 rr 的直接子節點,各子樹都是單一節點,子樹間不能有非樹邊。可是全圖仍有三條非樹邊,故不可能。

因此只有 d=2d=2 或 d=3d=3 可行。以下給出兩種情形的例子,確認它們確實能形成 DFS 樹:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 1-(2) 題

A node is an articulation point if and only if removing it (and the edges through it) disconnects GG. Is rr always an articulation point in GG for any possible GG?

(A) Yes
(B) Not necessary

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

割點是指移除該頂點及其相連邊後,圖的連通分量數增加。對無向圖的 DFS 樹而言,根節點是割點的條件是:它在 DFS 樹中至少有兩個子節點。

解題方法

根節點 rr 在原圖中的度數為 55,因此它連接其餘全部 55 個節點。移除 rr 及其相連的 55 條邊後,剩下 55 個節點與

8−5=38-5=3

條邊。

要讓 55 個節點保持連通,至少需要 5−1=45-1=4 條邊。因此,移除 rr 後的圖不可能連通,必定分成至少兩個連通分量。原圖 GG 是連通的,所以這些分量原先只能透過 rr 相連;移除 rr 會使 GG 斷開,故 rr 必為割點。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊
📄 以下 3 題共用同一段題幹

[6%] Let GG be an nn-node simple undirected graph. A kk-coloring of GG assigns a value in {1,2,…,k}\{1,2,\ldots,k\} to each node so that no two adjacent nodes have the same assigned value.

第 2-(1) 題

For k=2k=2, the best known algorithm has time complexity ____.

(A) O(n)O(n)
(B) O(n2)O(n^2)
(C) O(n2log⁡n)O(n^2\log n)
(D) O(n3)O(n^3)
(E) superpolynomial time

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

22-著色等價於判斷圖是否為二分圖:能把頂點分成兩群,使每條邊的兩端分屬不同群。可用 BFS 或 DFS 嘗試交替染色;若遇到相鄰頂點已被染成相同顏色,圖就不是二分圖。

解題方法

對每個尚未拜訪的頂點啟動 BFS 或 DFS,因為圖可能不連通。將起點染成一色,並把每個鄰點染成相反色;若檢查到同色相鄰點,便判定無法完成 22-著色。

若圖以鄰接串列表示,演算法時間為 O(n+m)O(n+m),其中 mm 是邊數。題目只用 nn 個頂點表示複雜度;簡單無向圖最多有

m≤n(n−1)2=O(n2)m \leq \frac{n(n-1)}{2}=O(n^2)

因此最壞情況的時間複雜度是 O(n2)O(n^2)。

選項分析

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 2-(2) 題

For k=3k=3, the best known algorithm has time complexity ____.

(A) O(n)O(n)
(B) O(n2)O(n^2)
(C) O(n2log⁡n)O(n^2\log n)
(D) O(n3)O(n^3)
(E) superpolynomial time

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考查圖著色問題的計算複雜度。33-著色是指替圖中每個節點指定 1、2、31、2、3 其中一種顏色,且每一對相鄰節點的顏色都不同。

判斷複雜度的關鍵是:判定一般圖是否能以 33 種顏色著色,是 NP 完全問題。因此,目前已知的精確演算法需要超多項式時間;若存在多項式時間演算法,便可推出 P=NPP=NP。

解題方法

題目問的是「最佳已知演算法」的時間複雜度。由於 33-著色問題是 NP 完全問題,已知的精確演算法不是 O(n)O(n)、O(n2)O(n^2)、O(n2log⁡n)O(n^2\log n) 或 O(n3)O(n^3) 這類多項式時間,而是超多項式時間。

這不代表已證明所有演算法都不可能在多項式時間內解題;目前尚未證明 P≠NPP\ne NP。考試中依據已知複雜度分類,應選「超多項式時間」。

選項分析

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 2-(3) 題

For k=4k=4, the best known algorithm has time complexity ____.

(A) O(n)O(n)
(B) O(n2)O(n^2)
(C) O(n2log⁡n)O(n^2\log n)
(D) O(n3)O(n^3)
(E) superpolynomial time

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

題目考的是固定 k=4k=4 時,一般圖的 kk-著色決定問題之計算複雜度。問題是判斷能否替每個頂點指定 11 至 44 的顏色,並讓每條邊兩端顏色不同。

一般圖的 4-著色問題是 NP 完全問題。這表示目前沒有已知的多項式時間演算法;若找到這類演算法,便能推出 P=NPP=NP。已知的精確演算法仍屬指數時間,例如有 O(1.7159n)O(1.7159^n) 時間的演算法,因此選項中的分類是「超多項式時間」。ESA 2024 研究論文

解題方法

可由 3-著色問題的 NP 完全性說明 4-著色也很難。給定一張圖 HH,新增一個頂點 xx,並讓 xx 與 HH 中每個頂點相鄰:

  • 若 HH 能以 3 種顏色著色,就用第 4 種顏色給 xx,新圖便能以 4 色著色。
  • 若新圖能以 4 色著色,因為 xx 與所有原頂點相鄰,原圖頂點不能使用 xx 的顏色,所以 HH 可用其餘 3 色著色。

因此,若一般圖的 4-著色存在多項式時間演算法,就能用它多項式時間解決 3-著色問題。4-著色問題本身又能在多項式時間內驗證一份著色結果,所以它是 NP 完全問題。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊
📄 以下 4 題共用同一段題幹

[13%] A Hamiltonian cycle of a graph GG is a simple cycle that visits all nodes in GG. Suppose there exists an O(n7)O(n^7)-time algorithm that decides HamC(GG) for any nn-node graph GG.

HamC(GG)

Input: a simple undirected graph GG.

Output: “true,” if GG has a Hamiltonian cycle; “false,” otherwise.

Complete Algorithm 1, an O(n7)O(n^7)-time algorithm that uses HamC at most once to decide HamP2×3_{2\times3} for any nn-node graph GG, for any distinct nodes a1,a2,x1,x2,x3∈Ga_1,a_2,x_1,x_2,x_3\in G.

HamP2×3(G=(V,E),a1,a2,x1,x2,x3)_{2\times3}(G=(V,E),a_1,a_2,x_1,x_2,x_3)

Input: a simple undirected graph G=(V,E)G=(V,E) that contains at least the five distinct nodes a1,a2,x1,x2,x3a_1,a_2,x_1,x_2,x_3.

Output: “true,” if GG has a simple path of length ∣V∣−4|V|-4 that starts at aia_i for some i∈{1,2}i\in\{1,2\}, visits every node in V∖{a1,a2,x1,x2,x3}V\setminus\{a_1,a_2,x_1,x_2,x_3\} exactly once, and finally stops at xjx_j for some j∈{1,2,3}j\in\{1,2,3\}; otherwise, “output false.”

Algorithm 1: HamP2×3(G=(V,E),a1,a2,x1,x2,x3)_{2\times3}(G=(V,E),a_1,a_2,x_1,x_2,x_3)

  1. U←V∪{ℓ1,ℓ2,ℓ3,ℓ4}U\leftarrow V\cup\{\ell_1,\ell_2,\ell_3,\ell_4\};
  2. F←EF\leftarrow E;
  3. /* Add some edges incident to node ℓ1\ell_1 to FF. */
    F←F∪{____}F\leftarrow F\cup\{\_\_\_\_\};
  4. /* Add some edges incident to node ℓ2\ell_2 to FF. */
    F←F∪{____}F\leftarrow F\cup\{\_\_\_\_\};
  5. /* Add some edges incident to node ℓ3\ell_3 to FF. */
    F←F∪{____}F\leftarrow F\cup\{\_\_\_\_\};
  6. /* Add some edges incident to node ℓ4\ell_4 to FF. */
    F←F∪{____}F\leftarrow F\cup\{\_\_\_\_\};
  7. return HamC(H=(U,F)H=(U,F));

第 3-(1) 題

Which of the following undirected edges shall be placed in the missing part ____ of Line 3?

(A) (ℓ1,ℓ2)(\ell_1,\ell_2)
(B) (ℓ1,a1)(\ell_1,a_1)
(C) (ℓ1,a2)(\ell_1,a_2)
(D) (ℓ1,x2)(\ell_1,x_2)

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

要用一次 HamC 判定「是否有一條從 a1a_1 或 a2a_2 出發、走遍其他所有點、停在 x1,x2,x3x_1,x_2,x_3 之一的路徑」,作法是加入輔助點 ℓ1∼ℓ4\ell_1\sim\ell_4,把路徑沒用到的那個 aa、那兩個 xx 以及所有 ℓ\ell 串成一條「回程」,與目標路徑接成一個 Hamiltonian cycle。

回程必須長成:

xj−ℓ−xk−ℓ−xl−ℓ−a另一個−ℓ−aix_j-\ell-x_k-\ell-x_l-\ell-a_{\text{另一個}}-\ell-a_i

四個 ℓ\ell 正好各司其職:

輔助點角色加入的邊
ℓ1\ell_1接兩個 aa(ℓ1,a1),(ℓ1,a2)(\ell_1,a_1),(\ell_1,a_2)
ℓ2\ell_2接 aa 與 xx(ℓ2,a1),(ℓ2,a2),(ℓ2,x1),(ℓ2,x2),(ℓ2,x3)(\ell_2,a_1),(\ell_2,a_2),(\ell_2,x_1),(\ell_2,x_2),(\ell_2,x_3)
ℓ3\ell_3接 xx 與 xx(ℓ3,x1),(ℓ3,x2),(ℓ3,x3)(\ell_3,x_1),(\ell_3,x_2),(\ell_3,x_3)
ℓ4\ell_4接 xx 與 xx(ℓ4,x1),(ℓ4,x2),(ℓ4,x3)(\ell_4,x_1),(\ell_4,x_2),(\ell_4,x_3)

正確性

  • (⇐) 若 GG 有所求路徑 ai⇝xja_i\leadsto x_j,令 aoa_o 為另一個 aa、xk,xlx_k,x_l 為另兩個 xx,則 ai⇝xj−ℓ3−xk−ℓ4−xl−ℓ2−ao−ℓ1−aia_i\leadsto x_j-\ell_3-x_k-\ell_4-x_l-\ell_2-a_o-\ell_1-a_i 是 HH 的 Hamiltonian cycle。
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 3-(2) 題

Which of the following undirected edges shall be placed in the missing part ____ of Line 4?

(A) (ℓ2,a1)(\ell_2,a_1)
(B) (ℓ2,a2)(\ell_2,a_2)
(C) (ℓ2,x2)(\ell_2,x_2)
(D) (ℓ2,x3)(\ell_2,x_3)

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

要用一次 HamC 判定「是否有一條從 a1a_1 或 a2a_2 出發、走遍其他所有點、停在 x1,x2,x3x_1,x_2,x_3 之一的路徑」,作法是加入輔助點 ℓ1∼ℓ4\ell_1\sim\ell_4,把路徑沒用到的那個 aa、那兩個 xx 以及所有 ℓ\ell 串成一條「回程」,與目標路徑接成一個 Hamiltonian cycle。

回程必須長成:

xj−ℓ−xk−ℓ−xl−ℓ−a另一個−ℓ−aix_j-\ell-x_k-\ell-x_l-\ell-a_{\text{另一個}}-\ell-a_i

四個 ℓ\ell 正好各司其職:

輔助點角色加入的邊
ℓ1\ell_1接兩個 aa(ℓ1,a1),(ℓ1,a2)(\ell_1,a_1),(\ell_1,a_2)
ℓ2\ell_2接 aa 與 xx(ℓ2,a1),(ℓ2,a2),(ℓ2,x1),(ℓ2,x2),(ℓ2,x3)(\ell_2,a_1),(\ell_2,a_2),(\ell_2,x_1),(\ell_2,x_2),(\ell_2,x_3)
ℓ3\ell_3接 xx 與 xx(ℓ3,x1),(ℓ3,x2),(ℓ3,x3)(\ell_3,x_1),(\ell_3,x_2),(\ell_3,x_3)
ℓ4\ell_4接 xx 與 xx(ℓ4,x1),(ℓ4,x2),(ℓ4,x3)(\ell_4,x_1),(\ell_4,x_2),(\ell_4,x_3)

正確性

  • (⇐) 若 GG 有所求路徑 ai⇝xja_i\leadsto x_j,令 aoa_o 為另一個 aa、xk,xlx_k,x_l 為另兩個 xx,則 ai⇝xj−ℓ3−xk−ℓ4−xl−ℓ2−ao−ℓ1−aia_i\leadsto x_j-\ell_3-x_k-\ell_4-x_l-\ell_2-a_o-\ell_1-a_i 是 HH 的 Hamiltonian cycle。
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 3-(3) 題

Which of the following undirected edges shall be placed in the missing part ____ of Line 5?

(A) (ℓ3,ℓ1)(\ell_3,\ell_1)
(B) (ℓ3,ℓ2)(\ell_3,\ell_2)
(C) (ℓ3,x1)(\ell_3,x_1)
(D) (ℓ3,x2)(\ell_3,x_2)

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

要用一次 HamC 判定「是否有一條從 a1a_1 或 a2a_2 出發、走遍其他所有點、停在 x1,x2,x3x_1,x_2,x_3 之一的路徑」,作法是加入輔助點 ℓ1∼ℓ4\ell_1\sim\ell_4,把路徑沒用到的那個 aa、那兩個 xx 以及所有 ℓ\ell 串成一條「回程」,與目標路徑接成一個 Hamiltonian cycle。

回程必須長成:

xj−ℓ−xk−ℓ−xl−ℓ−a另一個−ℓ−aix_j-\ell-x_k-\ell-x_l-\ell-a_{\text{另一個}}-\ell-a_i

四個 ℓ\ell 正好各司其職:

輔助點角色加入的邊
ℓ1\ell_1接兩個 aa(ℓ1,a1),(ℓ1,a2)(\ell_1,a_1),(\ell_1,a_2)
ℓ2\ell_2接 aa 與 xx(ℓ2,a1),(ℓ2,a2),(ℓ2,x1),(ℓ2,x2),(ℓ2,x3)(\ell_2,a_1),(\ell_2,a_2),(\ell_2,x_1),(\ell_2,x_2),(\ell_2,x_3)
ℓ3\ell_3接 xx 與 xx(ℓ3,x1),(ℓ3,x2),(ℓ3,x3)(\ell_3,x_1),(\ell_3,x_2),(\ell_3,x_3)
ℓ4\ell_4接 xx 與 xx(ℓ4,x1),(ℓ4,x2),(ℓ4,x3)(\ell_4,x_1),(\ell_4,x_2),(\ell_4,x_3)

正確性

  • (⇐) 若 GG 有所求路徑 ai⇝xja_i\leadsto x_j,令 aoa_o 為另一個 aa、xk,xlx_k,x_l 為另兩個 xx,則 ai⇝xj−ℓ3−xk−ℓ4−xl−ℓ2−ao−ℓ1−aia_i\leadsto x_j-\ell_3-x_k-\ell_4-x_l-\ell_2-a_o-\ell_1-a_i 是 HH 的 Hamiltonian cycle。
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 3-(4) 題

Which of the following undirected edges shall be placed in the missing part ____ of Line 6?

(A) (ℓ4,ℓ3)(\ell_4,\ell_3)
(B) (ℓ4,x1)(\ell_4,x_1)
(C) (ℓ4,x2)(\ell_4,x_2)
(D) (ℓ4,x3)(\ell_4,x_3)

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

要用一次 HamC 判定「是否有一條從 a1a_1 或 a2a_2 出發、走遍其他所有點、停在 x1,x2,x3x_1,x_2,x_3 之一的路徑」,作法是加入輔助點 ℓ1∼ℓ4\ell_1\sim\ell_4,把路徑沒用到的那個 aa、那兩個 xx 以及所有 ℓ\ell 串成一條「回程」,與目標路徑接成一個 Hamiltonian cycle。

回程必須長成:

xj−ℓ−xk−ℓ−xl−ℓ−a另一個−ℓ−aix_j-\ell-x_k-\ell-x_l-\ell-a_{\text{另一個}}-\ell-a_i

四個 ℓ\ell 正好各司其職:

輔助點角色加入的邊
ℓ1\ell_1接兩個 aa(ℓ1,a1),(ℓ1,a2)(\ell_1,a_1),(\ell_1,a_2)
ℓ2\ell_2接 aa 與 xx(ℓ2,a1),(ℓ2,a2),(ℓ2,x1),(ℓ2,x2),(ℓ2,x3)(\ell_2,a_1),(\ell_2,a_2),(\ell_2,x_1),(\ell_2,x_2),(\ell_2,x_3)
ℓ3\ell_3接 xx 與 xx(ℓ3,x1),(ℓ3,x2),(ℓ3,x3)(\ell_3,x_1),(\ell_3,x_2),(\ell_3,x_3)
ℓ4\ell_4接 xx 與 xx(ℓ4,x1),(ℓ4,x2),(ℓ4,x3)(\ell_4,x_1),(\ell_4,x_2),(\ell_4,x_3)

正確性

  • (⇐) 若 GG 有所求路徑 ai⇝xja_i\leadsto x_j,令 aoa_o 為另一個 aa、xk,xlx_k,x_l 為另兩個 xx,則 ai⇝xj−ℓ3−xk−ℓ4−xl−ℓ2−ao−ℓ1−aia_i\leadsto x_j-\ell_3-x_k-\ell_4-x_l-\ell_2-a_o-\ell_1-a_i 是 HH 的 Hamiltonian
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊
📄 以下 2 題共用同一段題幹

[6%] Answer the following two questions about BB-trees. Let tt be the minimum degree of a BB-tree.

第 4-(1) 題

Which of the following statements are (or is) TRUE? ____

(A) Every node can contain at most 2t2t keys.
(B) The depth of every leaf can be different.
(C) There exists one node which may have less than tt children.
(D) Every node has at least t−1t-1 keys.
(E) If n≥1n\geq1, then for any nn-key BB-tree of height hh and minimum degree t≥2t\geq2, the condition log⁡t(2th)≤log⁡t(n+1)\log_t(2t^h)\leq\log_t(n+1) is true.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考查 BB-tree 的最小度數 tt,以及由最少節點數推導樹高的界限。依標準定義:

  • 每個節點最多有 2t−12t-1 個鍵;非根節點至少有 t−1t-1 個鍵。
  • 內部非根節點至少有 tt 個子節點;根節點是例外,若根為內部節點,至少有 22 個子節點。
  • 所有葉節點都在相同深度。
  • 非空 BB-tree 的根至少有 11 個鍵。

解題方法

先用節點容量與根節點的例外判斷 (A) 至 (D),再由高度為 hh 的 BB-tree 在各層最少能有多少節點,推導 (E)。

若樹高為 hh,根至少有 11 個鍵,且若根為內部節點,至少有 22 個子節點。對 i≥1i\geq 1,深度 ii 至少有 2ti−12t^{i-1} 個節點;每個這些節點至少有 t−1t-1 個鍵。因此:

n≥1+∑i=1h2ti−1(t−1)n \geq 1+\sum_{i=1}^{h}2t^{i-1}(t-1) n≥1+2(t−1)th−1t−1=2th−1n \geq 1+2(t-1)\frac{t^h-1}{t-1} =2t^h-1

h=0h=0 時根是葉節點,且非空樹至少有 11 個鍵,同樣符合 n≥2t0−1=1n\geq 2t^0-1=1。因此 n+1≥2thn+1\geq 2t^h。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 4-(2) 題

When a new key is added to a BB-tree, which of the following statements are (or is) TRUE? ____

(A) If the key is inserted into a full node, the full node is split. Then the key must be placed in a new node which is the parent node of the nodes that are split from the full node.
(B) The root node can be split even if it is not full.
(C) When a full node is split, it is split around the median key.
(D) The height of the BB-tree increases if and only if the root node is split.
(E) After a full node is split, it is impossible that another node is split too.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

最小度數為 tt 的 BB-tree 中,非根節點可有 t−1t-1 到 2t−12t-1 個鍵;根節點最多也有 2t−12t-1 個鍵。含有 2t−12t-1 個鍵的節點稱為滿節點。

分裂滿節點時,將中位鍵提升至父節點,其餘鍵分到兩個子節點。插入過程若分裂根節點,會建立新根,因此樹高增加一層。

解題方法

依序檢查各選項是否符合滿節點分裂規則,以及插入時樹高的變化。特別要分清楚:分裂時提升的是原節點的中位鍵;新插入的鍵則依鍵值大小,放入適當的葉節點。

選項分析

(A) 錯誤。 分裂後,中位鍵會被提升到父節點;新插入的鍵則會依大小放入相應的子節點。新插入的鍵不一定放進由分裂產生的父節點。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊
📄 以下 3 題共用同一段題幹

[7%] GG is a weighted graph (Figure 1).

Figure 1: The numbers are weights of the edges.
🖼️【此處有附圖,請對照原卷】

第 5-(1) 題

The minimum spanning tree (MST) of GG is ____.

(A) 🖼️【此處有附圖,見下方】
(B) 🖼️【此處有附圖,見下方】
(C) 🖼️【此處有附圖,見下方】
(D) 🖼️【此處有附圖,見下方】
(E) 🖼️【此處有附圖,見下方】

🖼️ 本題附圖:
第 5-(1) 題附圖

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

**最小生成樹(MST)**必須連接原圖的全部頂點、不含環,並使邊權重總和最小。若原圖有 nn 個頂點,生成樹一定有 n−1n-1 條邊。

本題可用 Kruskal 演算法:依權重由小到大選邊,只要加入該邊不會形成環,就保留它,直到選滿 n−1n-1 條邊。

解題方法

原圖共有 8 個頂點,因此 MST 必須包含 7 條邊。依圖中的位置,將「最左、左上、左下、中央、最下、右上、右下、最右」的頂點,依序記為 v1,v2,…,v8v_1,v_2,\ldots,v_8。

依 Kruskal 演算法選邊;相同權重的邊可依任意順序處理,以下採用能直接對照選項(B)的順序:

權重選入的邊選取理由
11v2v3v_2v_3最小權重,加入後不形成環
22v1v5v_1v_5、v6v7v_6v_7、v6v8v_6v_8三條邊都不形成環,全數選入
33v3v4v_3v_4、v3v5v_3v_5將左側五個頂點連接起來
44v4v6v_4v_6連接左、右兩個連通分量

處理權重 33 的邊時,選入 v3v4v_3v_4、v3v5v_3v_5 後,另一條權重 33 的邊 v1v2v_1v_2 會形成環:

v1→v5→v3→v2→v1,v_1\to v_5\to v_3\to v_2\to v_1,

因此略過。權重 44 的邊 v2v4v_2v_4 也會形成環,略過;v4v6v_4v_6 則能連接左右兩部分,應選入。

以上七條邊正是選項(B),總權重為:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 5-(2) 題

A new graph can be obtained after the weight of an edge of graph GG is changed to 1 so that the weight of the MST of the new graph has the lowest weight among all the possible resulting graphs. The weight of the MST of the new graph is ____.

(A) 17
(B) 18
(C) 13
(D) 14
(E) 15

🖼️ 本題附圖:
第 5-(2) 題附圖

登入後即可作答並保存紀錄。

這一題的完整詳解

讀圖(Figure 1)

把 8 個頂點命名為:左 LL、上 TT、左下 MM、中 CC、下 BB、中右上 PP、右 RR、右下 QQ。14 條邊與權重:

邊權重邊權重
TT–MM1TT–CC4
LL–BB2CC–PP4
PP–RR2LL–RR(上方弧)5
PP–QQ2CC–BB5
LL–TT3LL–MM6
MM–CC3QQ–RR7
MM–BB3BB–QQ9

原圖的 MST(Kruskal):依序取 1,2,2,2,3(L–T),3(M–C)1,2,2,2,3(L\text{–}T),3(M\text{–}C);MM–BB 會成圈略過;TT–CC 成圈略過;取 CC–P(4)P(4) 後 8 點連通。

MST={TM, LB, PR, PQ, LT, MC, CP},w=1+2+2+2+3+3+4=17\text{MST}=\{T M,\ L B,\ P R,\ P Q,\ L T,\ M C,\ C P\},\quad w=1+2+2+2+3+3+4=17
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 5-(3) 題

A new graph can be obtained after an edge of graph GG is deleted so that the weight of the MST of the new graph has the highest weight among all the possible resulting graphs. The weight of the MST of the new graph is ____.

(A) 22
(B) 23
(C) 19
(D) 20
(E) 21

🖼️ 本題附圖:
第 5-(3) 題附圖

登入後即可作答並保存紀錄。

這一題的完整詳解

讀圖(Figure 1)

把 8 個頂點命名為:左 LL、上 TT、左下 MM、中 CC、下 BB、中右上 PP、右 RR、右下 QQ。14 條邊與權重:

邊權重邊權重
TT–MM1TT–CC4
LL–BB2CC–PP4
PP–RR2LL–RR(上方弧)5
PP–QQ2CC–BB5
LL–TT3LL–MM6
MM–CC3QQ–RR7
MM–BB3BB–QQ9

原圖的 MST(Kruskal):依序取 1,2,2,2,3(L–T),3(M–C)1,2,2,2,3(L\text{–}T),3(M\text{–}C);MM–BB 會成圈略過;TT–CC 成圈略過;取 CC–P(4)P(4) 後 8 點連通。

MST={TM, LB, PR, PQ, LT, MC, CP},w=1+2+2+2+3+3+4=17\text{MST}=\{T M,\ L B,\ P R,\ P Q,\ L T,\ M C,\ C P\},\quad w=1+2+2+2+3+3+4=17

解題方法

刪掉非樹邊,MST 不變(17);刪掉樹邊 ee,樹分成兩塊,要改用跨越這兩塊的最便宜非樹邊:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊
📄 以下 3 題共用同一段題幹

[8%] GG is a directed weighted graph (Figure 2). Some edges of GG have negative weights. If there is no specification, an edge or a node can be visited for more than one time. Determine a path with the lowest weight from node AA to node FF under three different conditions.

Figure 2: The arrows indicate the directions. The numbers on the edges are weights.
🖼️【此處有附圖,請對照原卷】

第 6-(1) 題

Condition One: The same node is visited a maximum of two times. The weight of the path under Condition One is ____.

(A) −12-12
(B) −13-13
(C) −29-29
(D) −41-41
(E) −43-43

🖼️ 本題附圖:
第 6-(1) 題附圖

登入後即可作答並保存紀錄。

這一題的完整詳解

讀圖(Figure 2)

雙箭頭的邊代表兩個方向各一條。有向邊與權重:

  • A→BA\to B:−6-6;A↔BA\leftrightarrow B:55(另一條弧,雙向)
  • A→CA\to C:−9-9;A↔CA\leftrightarrow C:22(雙向)
  • B↔CB\leftrightarrow C:1515;B→DB\to D:−17-17;E→CE\to C:−10-10
  • D↔ED\leftrightarrow E:33;D↔FD\leftrightarrow F:1111;E↔FE\leftrightarrow F:77

圖中有負圈,例如 A→B→D→E→C→AA\to B\to D\to E\to C\to A:−6−17+3−10+2=−28-6-17+3-10+2=-28。所以「不設限」的最短路徑是 −∞-\infty,題目才用三種條件限制重複。

左半 {A,B,C}\{A,B,C\} 到右半只有一條入口 B→DB\to D,右半回到左半只有 E→CE\to C;到 FF 只能由 D→F(11)D\to F(11) 或 E→F(7)E\to F(7)。

解題方法(條件一:每個點最多走兩次)

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 6-(2) 題

Condition Two: The same node is visited at most once. The weight of the path under Condition Two is ____.

(A) −10-10
(B) −11-11
(C) −12-12
(D) −13-13
(E) −17-17

🖼️ 本題附圖:
第 6-(2) 題附圖

登入後即可作答並保存紀錄。

這一題的完整詳解

讀圖(Figure 2)

雙箭頭的邊代表兩個方向各一條。有向邊與權重:

  • A→BA\to B:−6-6;A↔BA\leftrightarrow B:55(另一條弧,雙向)
  • A→CA\to C:−9-9;A↔CA\leftrightarrow C:22(雙向)
  • B↔CB\leftrightarrow C:1515;B→DB\to D:−17-17;E→CE\to C:−10-10
  • D↔ED\leftrightarrow E:33;D↔FD\leftrightarrow F:1111;E↔FE\leftrightarrow F:77

圖中有負圈,例如 A→B→D→E→C→AA\to B\to D\to E\to C\to A:−6−17+3−10+2=−28-6-17+3-10+2=-28。所以「不設限」的最短路徑是 −∞-\infty,題目才用三種條件限制重複。

左半 {A,B,C}\{A,B,C\} 到右半只有一條入口 B→DB\to D,右半回到左半只有 E→CE\to C;到 FF 只能由 D→F(11)D\to F(11) 或 E→F(7)E\to F(7)。

解題方法(條件二:每個點最多一次,即簡單路徑)

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 6-(3) 題

Condition Three: The same edge is visited a maximum of two times. The weight of the path under Condition Three is ____.

(A) −50-50
(B) −48-48
(C) −45-45
(D) −44-44
(E) −37-37

🖼️ 本題附圖:
第 6-(3) 題附圖

登入後即可作答並保存紀錄。

這一題的完整詳解

讀圖(Figure 2)

雙箭頭的邊代表兩個方向各一條。有向邊與權重:

  • A→BA\to B:−6-6;A↔BA\leftrightarrow B:55(另一條弧,雙向)
  • A→CA\to C:−9-9;A↔CA\leftrightarrow C:22(雙向)
  • B↔CB\leftrightarrow C:1515;B→DB\to D:−17-17;E→CE\to C:−10-10
  • D↔ED\leftrightarrow E:33;D↔FD\leftrightarrow F:1111;E↔FE\leftrightarrow F:77

圖中有負圈,例如 A→B→D→E→C→AA\to B\to D\to E\to C\to A:−6−17+3−10+2=−28-6-17+3-10+2=-28。所以「不設限」的最短路徑是 −∞-\infty,題目才用三種條件限制重複。

左半 {A,B,C}\{A,B,C\} 到右半只有一條入口 B→DB\to D,右半回到左半只有 E→CE\to C;到 FF 只能由 D→F(11)D\to F(11) 或 E→F(7)E\to F(7)。

解題方法(條件三:每條邊最多走兩次)

  • 進入右半只能走 B→DB\to D,最多兩次;走路最後要停在 FF,所以最多「一次去而復返」加「一次去到 FF」。
    • 去而復返最省:B→D→E→CB\to D\to E\to C,−17+3−10=−24-17+3-10=-24。
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊
📄 以下 3 題共用同一段題幹

[4%] The following questions are about disjoint sets (linked list representation) and disjoint-set forests (forest representation).

(A) Append the smaller list onto the longer list, with ties broken arbitrarily.
(B) Make the root of a disjoint-set forest with smaller rank point to the root of another disjoint-set forest with larger rank.
(C) Make each node of a tree in the disjoint-set forest point to the tree root during a certain operation.
(D) Flatten a disjoint-set forest.
(E) Union two disjoint-set forests into a new forest and set their smallest element as the representative of the new forest.

第 7-(1) 題

What is the purpose of union by rank? ____

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

「按秩合併」(union by rank)是並查集森林的合併策略:合併兩個集合時,比較兩棵樹根節點的秩(rank),將秩較小的根接到秩較大的根底下;兩根秩相同時,任選一根作為新根,並將新根的秩加 11。

秩用來控制樹高。採用按秩合併後,含 nn 個元素的樹高至多為 ⌊log⁡2n⌋\lfloor \log_2 n \rfloor,因此可避免合併操作把森林退化成很長的鏈。

解題方法

題目問按秩合併的目的,直接對照各敘述所描述的操作即可:將較小秩樹的根接到較大秩樹的根,正是按秩合併的規則;其目的在限制樹高。若再搭配路徑壓縮,並查集操作的攤銷時間複雜度可達 O(α(n))O(\alpha(n)),其中 α\alpha 是反阿克曼函數。

選項分析

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 7-(2) 題

What is the purpose of path compression? ____

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

不相交集合(disjoint set)森林以樹表示集合,根節點是代表元。Find(x) 會沿著父節點指標往上走,直到找到根節點。若樹很高,這段路徑就會很長。

路徑壓縮(path compression)是在執行 Find(x) 時,把查找路徑上的節點直接改指向根節點,降低樹高,讓後續的 Find 更快。壓縮的是這次查找走過的路徑,不代表一次操作就把整個森林中的所有節點都改成指向根節點。

解題方法

依序判斷選項描述的集合操作。路徑壓縮的關鍵特徵是:它在尋找代表元時,縮短查找路徑,使沿途節點直接連到根。因此,最符合題意的是選項 C。

選項分析

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 7-(3) 題

What is the purpose of weighted-union heuristic? ____

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

這題考的是鏈結串列表示法中的加權合併(weighted-union heuristic)。每個集合以鏈結串列表示,並讓每個元素都能指向該集合的代表元素。

合併兩個集合時,若把其中一個串列的所有元素都更新為新代表,成本與被更新的串列長度成正比。加權合併的做法是:把較短的串列接到較長的串列後面,讓需要更新代表指標的元素數量較少。

解題方法

對每次合併,選較短的串列作為被接上的串列,只更新其中元素的代表指標。若某元素的代表指標因此改變,該元素所在的集合至少會變成原來的兩倍大:它原先在較短串列中,合併後串列長度至少是它原集合大小的兩倍。

因此,一個元素的代表指標最多改變 ⌊log⁡2n⌋\lfloor \log_2 n \rfloor 次,其中 nn 是元素總數。這使得連結串列表示法的合併操作更有效率,全部合併的代表指標更新成本為 O(nlog⁡n)O(n\log n)。

選項分析

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊
📄 以下 3 題共用同一段題幹

[6%] Consider the formula an+bn3=(1+23)na_n+b_n\sqrt{3}=(1+2\sqrt{3})^n, where nn is a positive integer.

Algorithm 2: P(n)P(n)

  1. if n=1n=1 then
  2. return $(1,2)$
    
  3. else
  4. $(a,b)=P(n-1)$
    
  5. return $(a+6b,2a+b)$
    
  6. end if

第 8-(1) 題

Which of the following statements are (or is) TRUE? ____

(A) a2=1a_2=1.
(B) b3=30b_3=30.
(C) a5=a4+6b4a_5=a_4+6b_4.
(D) b5=2a4+6b4b_5=2a_4+6b_4.
(E) For some finite nn, ana_n is not integral.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考查根式係數的乘法,以及遞迴演算法所對應的數學遞迴式。若

an+bn3=(1+23)na_n+b_n\sqrt{3}=(1+2\sqrt{3})^n

將兩邊乘上 1+231+2\sqrt{3},可得

an+1+bn+13=(an+bn3)(1+23)=(an+6bn)+(2an+bn)3.\begin{aligned} a_{n+1}+b_{n+1}\sqrt{3} &=(a_n+b_n\sqrt{3})(1+2\sqrt{3})\\ &=(a_n+6b_n)+(2a_n+b_n)\sqrt{3}. \end{aligned}

因此係數滿足

an+1=an+6bn,bn+1=2an+bn,a_{n+1}=a_n+6b_n,\qquad b_{n+1}=2a_n+b_n,

且初始值為 a1=1, b1=2a_1=1,\ b_1=2。

解題方法

依照遞迴式逐步計算:

nanbn11221+6(2)=132(1)+2=4313+6(4)=372(13)+4=30437+6(30)=2172(37)+30=1045217+6(104)=8412(217)+104=538\begin{array}{c|r|r} n & a_n & b_n\\ \hline 1 & 1 & 2\\ 2 & 1+6(2)=13 & 2(1)+2=4\\ 3 & 13+6(4)=37 & 2(13)+4=30\\ 4 & 37+6(30)=217 & 2(37)+30=104\\ 5 & 217+6(104)=841 & 2(217)+104=538 \end{array}
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 8-(2) 題

Algorithm 2 has running time ____ in the worst case (as close as possible).

(A) O(log⁡n)O(\log n)
(B) O(n)O(n)
(C) O(nlog⁡n)O(n\log n)
(D) O(n2)O(n^2)
(E) O(log⁡2n)O(\log^2 n)

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

這題考遞迴演算法的時間複雜度。分析時要看每次遞迴把輸入規模縮小多少,以及每次呼叫額外花多少時間。

解題方法

當 n>1n>1 時,演算法呼叫 P(n−1)P(n-1),接著只做固定次數的加法與乘法,因此每次呼叫的額外工作量為 Θ(1)\Theta(1)。

令 T(n)T(n) 表示執行時間,則:

T(n)=T(n−1)+Θ(1),T(1)=Θ(1)T(n)=T(n-1)+\Theta(1),\qquad T(1)=\Theta(1)

從 nn 遞減到 11,共呼叫 P(n),P(n−1),…,P(1)P(n),P(n-1),\ldots,P(1),總計 nn 次。每次工作量為常數,因此:

T(n)=Θ(n)T(n)=\Theta(n)

最接近的選項是 O(n)O(n)。

選項分析

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 8-(3) 題

Which of the following statements are (or is) TRUE? ____

(A) Algorithm 2 is the most efficient algorithm to compute ana_n and bnb_n.
(B) ana_n and bnb_n can be computed in O(log⁡n)O(\log n) time.
(C) (1+23)n(1+2\sqrt{3})^n is not integral. Thus, there is no integral solution for ana_n and bnb_n.
(D) If nn is a positive even integer, then an=an/22+3bn/22a_n=a_{n/2}^2+3b_{n/2}^2.
(E) If nn is odd, then bn=2a⌊n/2⌋b⌊n/2⌋b_n=2a_{\lfloor n/2\rfloor}b_{\lfloor n/2\rfloor}.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

這題考的是代數式的係數表示、遞迴演算法的效率,以及快速冪。

將 a+b3a+b\sqrt{3} 視為一組係數 (a,b)(a,b)。兩個這類表示相乘時:

(a+b3)(c+d3)=(ac+3bd)+(ad+bc)3(a+b\sqrt{3})(c+d\sqrt{3}) =(ac+3bd)+(ad+bc)\sqrt{3}

因此,係數相乘的規則是:

(a,b)⋅(c,d)=(ac+3bd, ad+bc)(a,b)\cdot(c,d)=(ac+3bd,\ ad+bc)

題目給的遞迴正是由

(a+b3)(1+23)=(a+6b)+(2a+b)3(a+b\sqrt{3})(1+2\sqrt{3}) =(a+6b)+(2a+b)\sqrt{3}

推得,所以每次將指數增加 11,就能更新 (an,bn)(a_n,b_n)。

解題方法

直接依照 Algorithm 2 從 P(1)P(1) 算到 P(n)P(n),每次只做固定次數的算術運算,共需 n−1n-1 次更新,時間為 O(n)O(n)。但利用二進位快速冪,可以在 O(log⁡n)O(\log n) 次係數乘法內算出 (an,bn)(a_n,b_n)。

設 m=⌊n/2⌋m=\lfloor n/2\rfloor,先算出

(am+bm3)2=(am2+3bm2)+2ambm3(a_m+b_m\sqrt{3})^2 =(a_m^2+3b_m^2)+2a_mb_m\sqrt{3}

因此:

a2m=am2+3bm2,b2m=2ambma_{2m}=a_m^2+3b_m^2,\qquad b_{2m}=2a_mb_m

若 n=2m+1n=2m+1,再乘上 (1+23)(1+2\sqrt{3}),即可得到:

a2m+1=a2m+6b2m,b2m+1=2a2m+b2ma_{2m+1}=a_{2m}+6b_{2m},\qquad b_{2m+1}=2a_{2m}+b_{2m}

每次將指數折半,遞迴深度為 O(log⁡n)O(\log n),所以能比 Algorithm 2 更有效率地計算。

選項分析

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊
📄 以下 3 題共用同一段題幹

[9%] Let A=⟨a1,a2,…,an⟩A=\langle a_1,a_2,\ldots,a_n\rangle be a sequence of positive integers. We are interested in the subsequence A′=⟨ai1,ai2,…,aik⟩A'=\langle a_{i_1},a_{i_2},\ldots,a_{i_k}\rangle, such that for any 1≤j≤k1\leq j\leq k, ai1+ai2+⋯+aij−1≤6aija_{i_1}+a_{i_2}+\cdots+a_{i_{j-1}}\leq6a_{i_j}, where 1≤i1<i2<⋯<ik≤n1\leq i_1<i_2<\cdots<i_k\leq n. We call this the Prefix-Sum condition. Given a sequence AA, we want to find the largest possible kk.

For example, given 9 positive integers: 10,10,10,10,10,10,10,10,10010,10,10,10,10,10,10,10,100, we can select up to 8 integers: 10,10,10,10,10,10,10,10010,10,10,10,10,10,10,100, and also satisfy the above condition.

Consider the input: 2,5,3,6,2,1,22,5,3,6,2,1,2.

第 9-(1) 題

What is the largest kk?

(A) 6
(B) 4
(C) 3
(D) 7
(E) 5

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

每選入一個數字,後續數字就必須滿足「已選總和不超過目前數字的 6 倍」。因此,對每個選取數量 kk,只需保留能達成該數量的最小總和:總和越小,越容易符合後續數字的條件。

令 dp[k]dp[k] 表示目前處理過的數字中,恰好選取 kk 個數字時可達成的最小總和;若無法達成,記為 ∞\infty。初始值為 dp[0]=0dp[0]=0。

解題方法

依序處理輸入 2,5,3,6,2,1,22,5,3,6,2,1,2。遇到數字 xx 時,若目前恰選 kk 個的最小總和 dp[k]dp[k] 符合 dp[k]≤6xdp[k]\le 6x,就能再選入 xx,形成 k+1k+1 個數字,總和為 dp[k]+xdp[k]+x。表中各欄列出處理完該數字後,各選取數量能達成的最小總和。

已處理數字k=0k=0k=1k=1k=2k=2k=3k=3k=4k=4k=5k=5k=6k=6
無0∞\infty∞\infty∞\infty∞\infty∞\infty∞\infty
202∞\infty∞\infty∞\infty∞\infty∞\infty
5027∞\infty∞\infty∞\infty∞\infty
302510∞\infty∞\infty∞\infty
60251016∞\infty∞\infty
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 9-(2) 題

Consider the sequence ⟨1,1,1,1,1,1,w,2,2,2,3,3,4,4,x,y⟩\langle1,1,1,1,1,1,w,2,2,2,3,3,4,4,x,y\rangle that satisfies the above condition. Which of the following statements are (or is) TRUE?

(A) x≥5x\geq5.
(B) y>5y>5.
(C) The smallest possible ww is 2.
(D) There is a possible solution with w+x+y=12w+x+y=12.
(E) This sequence can be extended to be an infinite sequence that satisfies the above condition.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

設目前項目前的前綴和為 SS,若下一項是 aa,Prefix-Sum condition 等價於

S≤6a⟺a≥⌈S6⌉.S\le 6a \quad\Longleftrightarrow\quad a\ge \left\lceil \frac{S}{6}\right\rceil .

因此,逐項檢查前綴和即可找出 w,x,yw,x,y 的限制。

解題方法

前六項都是 11,其前綴和依序不超過 55,都符合條件。到 ww 前的前綴和為 66,故 w≥1w\ge 1。

接著檢查三個 22。第三個 22 前的前綴和為 10+w10+w,所以

10+w≤6(2)=12⟹w≤2.10+w\le 6(2)=12 \quad\Longrightarrow\quad w\le 2.

其他 22、33、44 的條件不會比這個限制更嚴格。因此

w∈{1,2}.w\in\{1,2\}.

到 xx 前的前綴和為

6+w+3(2)+2(3)+2(4)=26+w.6+w+3(2)+2(3)+2(4)=26+w.

所以

x≥⌈26+w6⌉=5,x\ge \left\lceil\frac{26+w}{6}\right\rceil=5,

其中 w=1w=1 或 w=2w=2 都得到 x≥5x\ge 5。

到 yy 前的前綴和為 26+w+x26+w+x,因此

y≥⌈26+w+x6⌉.y\ge \left\lceil\frac{26+w+x}{6}\right\rceil.
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 9-(3) 題

Denote D(i,s)D(i,s) to be the maximum number of elements that can be selected from the first ii integers in AA with a sum at most ss, and satisfy the Prefix-Sum condition. Which of the following statements are (or is) TRUE?

(A) This problem can be solved with Dynamic Programming.
(B) D(i,s)=max⁡(D(i−1,s),D(i−1,s−ai)+1)D(i,s)=\max(D(i-1,s),D(i-1,s-a_i)+1).
(C) D(i,s)=max⁡(D(i−1,s),D(i−1,min⁡(s−ai,6ai))+1)D(i,s)=\max(D(i-1,s),D(i-1,\min(s-a_i,6a_i))+1).
(D) The answer is D(n,6an)D(n,6a_n).
(E) The time complexity to find the answer is O(nmax⁡i∈{1,2,…,n}{ai})O(n\max_{i\in\{1,2,\ldots,n\}}\{a_i\}).

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

這題把「選取子序列」的限制轉成動態規劃。每加入一個新元素 aia_i,要同時滿足兩件事:

  1. 選取元素的總和不超過狀態上限 ss。
  2. 加入 aia_i 前,已選元素的總和不超過 6ai6a_i。

因此,若要選 aia_i,前面已選元素的總和最多是 min⁡(s−ai,6ai)\min(s-a_i,6a_i)。

解題方法

考慮前 ii 個元素時,對 aia_i 分成「不選」和「選」兩種情況:

  • 不選 aia_i:最多選 D(i−1,s)D(i-1,s) 個。
  • 選 aia_i:前面元素的總和須不超過 s−ais-a_i,也須不超過 6ai6a_i,所以最多選 D(i−1,min⁡(s−ai,6ai))+1D(i-1,\min(s-a_i,6a_i))+1 個。

故遞迴式為

D(i,s)=max⁡(D(i−1,s),D(i−1,min⁡(s−ai,6ai))+1).D(i,s)=\max\left(D(i-1,s),D(i-1,\min(s-a_i,6a_i))+1\right).

若 s<ais<a_i,選取 aia_i 的情況不可行;實作時可略過該分支,或將不可行狀態設為負無限大。

設 M=max⁡iaiM=\max_i a_i。任何非空合法子序列若最後選到 aja_j,其先前元素總和至多為 6aj6a_j,因此整個子序列的總和至多為 7aj≤7M7a_j\leq 7M。所以計算到總和上限 7M7M,即可涵蓋所有可能的合法子序列,答案為 D(n,7M)D(n,7M)。

共有 nn 列、至多 7M+17M+1 個總和狀態,每個狀態只需常數時間,時間複雜度為 O(nM)O(nM);用逐列更新可將空間降為 O(M)O(M)。

選項分析

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 10 題5 分

Which of the following statements are (or is) TRUE? ____

(A) Let α\alpha be the load factor of a hash table. If collisions are resolved by chaining in the hash table, then both the successful search and the unsuccessful search take expected time O(1+α)O(1+\alpha) under the assumption of simple uniform hashing.
(B) The asymptotic time complexity of searching a key in a heap structure is O(log⁡n)O(\log n), where nn is the number of elements in the structure.
(C) Suppose that a hash table has mm slots. The table can store at most mm keys if collision is resolved by chaining.
(D) An AVL tree is a binary search tree.
(E) In a red-black tree, a node has two children and one of the children is red. The node must be black.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考查雜湊表的鏈結法、堆積的搜尋複雜度、AVL 樹的定義,以及紅黑樹的顏色規則。

  • 雜湊表的負載因子為 α=n/m\alpha=n/m,其中 nn 是鍵值數量、mm 是槽位數量。
  • 鏈結法會在每個槽位存放一條鏈結串列,因此每個槽位可容納多個鍵值。
  • 堆積只保證父子節點符合堆積順序,不保證整體符合二元搜尋樹的順序。
  • AVL 樹是具有高度平衡限制的二元搜尋樹。
  • 紅黑樹規定紅色節點的子節點必須是黑色。

解題方法

逐項對照資料結構的定義與複雜度。特別注意搜尋堆積中的任意鍵值,不等同於取得堆積頂端元素;堆積的局部順序不足以像二元搜尋樹那樣排除一半搜尋範圍。

選項分析

(A) 正確。
在簡單均勻雜湊假設下,每個鍵值平均均勻地分布到 mm 個槽位。鏈結法的失敗搜尋,平均需要檢查所選槽位中的鏈結串列,期望檢查數量為 O(α)O(\alpha);若計入雜湊與定位等固定成本,期望時間為 O(1+α)O(1+\alpha)。成功搜尋的期望時間也為 O(1+α)O(1+\alpha)。因此敘述正確。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊
📄 以下 2 題共用同一段題幹

[5%] What is the asymptotic time complexity for the following algorithms to sort nn elements in a descending order?

第 11-(1) 題

BubbleSort has running time complexity ____ in the worst case.

(A) O(log⁡n)O(\log n)
(B) O(nlog⁡n)O(n\log n)
(C) O(n)O(n)
(D) O(n2)O(n^2)
(E) O(1)O(1)

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

氣泡排序會反覆比較相鄰元素,若順序不符合目標,就交換兩者。分析最壞情況時,要計算輸入資料最不利時,演算法執行的比較與交換次數。

本題要求由大到小排序;排序方向會影響比較條件,但不改變氣泡排序在最壞情況下的漸近時間複雜度。

解題方法

每一趟掃描會將尚未排定範圍中的一個元素移至正確位置。最壞情況下,第一趟最多比較 n−1n-1 次,第二趟最多比較 n−2n-2 次,依此類推,直到最後一趟比較 11 次。

因此,比較次數為:

(n−1)+(n−2)+⋯+1=n(n−1)2(n-1)+(n-2)+\cdots+1 =\frac{n(n-1)}{2}

展開後的最高次項為 12n2\frac{1}{2}n^2,所以最壞情況的時間複雜度為 Θ(n2)\Theta(n^2),以選項中的大 OO 表示為 O(n2)O(n^2)。即使實作包含「某一趟沒有交換就提前結束」的檢查,最壞情況仍須進行二次量級的比較與交換。

選項分析

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 11-(2) 題

HeapSort has running time complexity ____ in the worst case.

(A) O(log⁡n)O(\log n)
(B) O(nlog⁡n)O(n\log n)
(C) O(n)O(n)
(D) O(n)O(\sqrt{n})
(E) O(1)O(1)

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考查 HeapSort(堆積排序)的最壞時間複雜度。二元堆積是一種完全二元樹,含有 nn 個元素時,高度為 Θ(log⁡n)\Theta(\log n)。插入或刪除堆頂元素時,至多沿著樹高調整,因此單次操作需要 Θ(log⁡n)\Theta(\log n) 時間。

解題方法

HeapSort 先將 nn 個元素建立成堆積,再反覆取出堆頂元素,並調整剩餘元素,使其重新符合堆積性質。

  • 建立堆積可用自底向上的方法,時間為 Θ(n)\Theta(n)。
  • 每次取出堆頂後,最多需要沿堆積高度調整,時間為 O(log⁡n)O(\log n)。
  • 取出所有元素共需 nn 次,因此這部分的時間為 O(nlog⁡n)O(n\log n)。

合計為:

Θ(n)+Θ(nlog⁡n)=Θ(nlog⁡n)\Theta(n) + \Theta(n\log n) = \Theta(n\log n)

要排成遞減順序,可使用適當方向的堆積;排序方向不影響最壞時間複雜度。因此 HeapSort 的最壞時間複雜度是 O(nlog⁡n)O(n\log n)。

選項分析

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊
📄 以下 3 題共用同一段題幹

[10%] The notation i=1..Ni=1..N means that ii is inside the interval [1,N][1,N]. Consider a two-processor computer system and a large program, which consists of NN modules indexed from 1 to NN. Let aia_i and bib_i be the cost of running module ii on processor 1 and 2, respectively. Assigning two modules to different processors incurs costs due to inter-processor communication. Each module can be assigned to one processor only. Let ci,jc_{i,j} be the communication cost if modules ii and jj are assigned to different processors. We want to assign the modules on the two processors to minimize the total cost.

Table 1: Cost on processors

ii1234
aia_i65104
bib_i41038

第 12-(1) 題

Consider the above table with N=4N=4 and let the communication cost be c1,2=5c_{1,2}=5, c2,3=6c_{2,3}=6, c2,4=2c_{2,4}=2, c3,4=1c_{3,4}=1, and the rest have 0 communication cost. Let XX be the best answer. Which of the following statements are (or is) TRUE?

(A) X mod 10=4X\bmod10=4
(B) ⌊X/10⌋=1\lfloor X/10\rfloor=1
(C) X≤25X\leq25
(D) X mod 2=0X\bmod2=0
(E) None of the above is true.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

這題是兩處理器工作分配的最小化問題。每個模組只能分配到一個處理器,總成本由兩部分組成:

  1. 各模組在所分配處理器上的執行成本。
  2. 相鄰模組分配到不同處理器時產生的通訊成本。

若以 xi∈{1,2}x_i\in\{1,2\} 表示模組 ii 分配到的處理器,總成本為

C(x1,x2,x3,x4)=∑i=14{ai,xi=1bi,xi=2+∑(i,j)ci,j 1[xi≠xj],C(x_1,x_2,x_3,x_4) = \sum_{i=1}^{4} \begin{cases} a_i,&x_i=1\\ b_i,&x_i=2 \end{cases} + \sum_{(i,j)} c_{i,j}\,\mathbf{1}[x_i\ne x_j],

其中 1[xi≠xj]\mathbf{1}[x_i\ne x_j] 在兩模組分配到不同處理器時為 11,否則為 00。

解題方法

題目只有 44 個模組,可把可能分配方式分組計算。觀察通訊成本的連線:(1,2)(1,2)、(2,3)(2,3)、(2,4)(2,4)、(3,4)(3,4)。先固定模組 22、33 的處理器,再分別選擇模組 11、44 的最佳處理器。

模組 11 只和模組 22 有通訊成本:

  • 若模組 22 在處理器 2,模組 11 選處理器 2 的成本為 44;選處理器 1 則成本為 6+5=116+5=11,故最佳成本為 44。
  • 若模組 22 在處理器 1,模組 11 選處理器 1 的成本為 66;選處理器 2 則成本為 4+5=94+5=9,故最佳成本為 66。

模組 44 的最佳成本則需計入它與模組 22、33 的通訊成本:

模組 2模組 3模組 4 在處理器 1 的成本模組 4 在處理器 2 的成本最佳成本
224+2+1=74+2+1=78877
214+2=64+2=68+1=98+1=966
124+1=54+1=58+2=108+2=1055
11448844
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 12-(2) 題

We can model this problem with an undirected graph G=(V,E)G=(V,E), where V={p1,p2,v1,…,vN}V=\{p_1,p_2,v_1,\ldots,v_N\} and
E={(p1,vi),(vi,p2):i=1..N}∪{(vi,vj):ci,j>0}E=\{(p_1,v_i),(v_i,p_2):i=1..N\}\cup\{(v_i,v_j):c_{i,j}>0\}. For i=1..Ni=1..N, the weight for edge (p1,vi)(p_1,v_i) is aia_i and the weight for edge (vi,p2)(v_i,p_2) is bib_i. The weight for (vi,vj)∈E(v_i,v_j)\in E is ci,jc_{i,j}. Which of the following statements are (or is) TRUE?

(A) There are 2N2^N possible ways to assign the modules to the two processors.
(B) Let AiA_i be the set of modules assigned to processor ii, for i∈{1,2}i\in\{1,2\}. Then the cost is ∑j∈A1aj+∑j∈A2bj+∑(vi,vj)∈A1×A2ci,j\sum_{j\in A_1}a_j+\sum_{j\in A_2}b_j+\sum_{(v_i,v_j)\in A_1\times A_2}c_{i,j}.
(C) The cost relates to a cut of the graph G=(V,E)G=(V,E) with p1p_1 and p2p_2 on the same side.
(D) The minimum cost relates to a minimum flow from a source to a sink.
(E) Let AiA_i be the set of modules assigned to processor ii, for i∈{1,2}i\in\{1,2\}. Then {p1}∪A1,{p2}∪A2\{p_1\}\cup A_1,\{p_2\}\cup A_2 is a cut.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

每個模組只能指派給處理器 1 或處理器 2,因此共有 2N2^N 種指派。總成本包含兩部分:

  1. 各模組在所屬處理器上的執行成本。
  2. 分配到不同處理器的模組對之間的通訊成本。

令 A1A_1、A2A_2 分別為指派給處理器 1、2 的模組集合,則 A1A_1 與 A2A_2 互斥,且兩者聯集為所有模組。總成本為

∑j∈A1aj+∑j∈A2bj+∑i∈A1, j∈A2ci,j.\sum_{j\in A_1}a_j+\sum_{j\in A_2}b_j+ \sum_{\substack{i\in A_1,\ j\in A_2}}c_{i,j}.

圖論中的割是把頂點分成兩個不相交且聯集為全體頂點的集合;兩側之間的邊稱為割邊。若要用「p1p_1 到 p2p_2 的割」表示兩處理器的指派,p1p_1 和 p2p_2 必須分處割的兩側,割邊的權重總和才代表割容量。

解題方法

先將模組指派寫成集合分割,再逐一核對各選項。圖的頂點集合為 {p1,p2,v1,…,vN}\{p_1,p_2,v_1,\ldots,v_N\},其中模組對應頂點 viv_i。把模組分配到不同處理器所產生的通訊成本,對應到兩側之間的模組邊;因此割的通訊成本部分,會計入所有跨越 A1A_1、A2A_2 的邊。

不過,題目給定的處理器邊權重與執行成本的對應方向不吻合。若採用選項(E)的分割方式 {p1}∪A1\{p_1\}\cup A_1 與 {p2}∪A2\{p_2\}\cup A_2,則 p1p_1 到 viv_i 的邊在 i∈A2i\in A_2 時跨割,產生 aia_i;而 viv_i 到 p2p_2 的邊在 i∈A1i\in A_1 時跨割,產生 bib_i。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 12-(3) 題

Which of the following statements are (or is) TRUE?

(A) The above G=(V,E)G=(V,E) is a bipartite graph.
(B) The minimum cost can be found with the Prim algorithm.
(C) The minimum cost can be found with the Dijkstra algorithm.
(D) The minimum cost can be found with the Edmonds-Karp algorithm.
(E) The minimum cost can be found with the Ford-Fulkerson algorithm.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

這題考的是把「模組分配到兩台處理器,使總成本最小」轉換成最小割問題,再利用最大流最小割定理求解。

最小割把頂點分成包含源點 ss 的集合 SS 與包含匯點 tt 的集合 TT,割的容量是所有由 SS 指向 TT 的邊容量總和。最大流最小割定理指出:最大流的值等於最小割的容量。

解題方法

建立流網路如下:

  • 加入源點 ss、匯點 tt,並為每個模組 ii 建立一個頂點。
  • 加入容量為 bib_i 的邊 s→is\to i,以及容量為 aia_i 的邊 i→ti\to t。
  • 對每對模組 i,ji,j,加入容量為 ci,jc_{i,j} 的雙向通信邊。

將割的 SS 側視為分配到處理器 1,TT 側視為分配到處理器 2:

  • 模組 ii 在 SS 側時,邊 i→ti\to t 跨越割,成本為 aia_i。
  • 模組 ii 在 TT 側時,邊 s→is\to i 跨越割,成本為 bib_i。
  • 模組 i,ji,j 位於不同側時,通信邊跨越割,成本為 ci,jc_{i,j}。
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊
📄 以下 2 題共用同一段題幹

[5%] A key with value 3535 is inserted into a maximum heap shown below. After that, two methods are adopted to traverse the heap.

🖼️【此處有附圖,請對照原卷】

第 13-(1) 題

What are the outputs by the post-order traversal?

(A) 35,27,21,13,19,11,10,9,7,8,1735,27,21,13,19,11,10,9,7,8,17
(B) 35,27,13,9,7,19,8,17,21,11,1035,27,13,9,7,19,8,17,21,11,10
(C) 9,7,13,8,17,19,27,11,10,21,359,7,13,8,17,19,27,11,10,21,35
(D) 9,7,13,8,17,19,27,35,11,10,219,7,13,8,17,19,27,35,11,10,21
(E) 9,13,7,27,8,19,17,35,11,21,109,13,7,27,8,19,17,35,11,21,10

🖼️ 本題附圖:
第 13-(1) 題附圖

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

最大堆插入新鍵值時,先將新值放到完全二元樹的下一個空位,再沿著父節點向上調整;若新值大於父節點,就交換,直到符合最大堆條件。

後序走訪依序為「左子樹、右子樹、根節點」。

解題方法

圖中原有的最大堆包含 1010 個節點:根節點為 2727;其左、右子節點分別為 1919、2121;下一層為 13,17,11,1013,17,11,10;最下層為 9,7,89,7,8。依完全二元樹的排列方式,新鍵值 3535 先放在節點 1717 的右子節點位置。

因為 35>1735>17、35>1935>19、35>2735>27,所以插入後依序向上交換。調整後的堆為:

             35
           /    \
         27      21
        /  \    /  \
      13   19  11  10
     / \   / \
    9   7 8  17

依後序走訪:

  • 左子樹:先走訪 1313 的左右子節點,再走訪 1313,接著走訪 1919 的左右子節點,再走訪 1919,最後走訪 2727,得到 9,7,13,8,17,19,279,7,13,8,17,19,27。
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 13-(2) 題

What are the outputs by the in-order traversal?

(A) 35,27,21,13,19,11,10,9,7,8,1735,27,21,13,19,11,10,9,7,8,17
(B) 35,27,13,9,7,19,8,17,21,11,1035,27,13,9,7,19,8,17,21,11,10
(C) 9,7,13,8,17,19,27,11,10,21,359,7,13,8,17,19,27,11,10,21,35
(D) 9,7,13,8,17,19,27,35,11,10,219,7,13,8,17,19,27,35,11,10,21
(E) 9,13,7,27,8,19,17,35,11,21,109,13,7,27,8,19,17,35,11,21,10

🖼️ 本題附圖:
第 13-(2) 題附圖

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

**最大堆積(max heap)**是完全二元樹,每個父節點的值皆不小於子節點。插入新值時,先放在維持完全二元樹的下一個位置,再與父節點比較,必要時交換並向上調整。

**中序走訪(in-order traversal)**的順序為:

左子樹  →  根節點  →  右子樹\text{左子樹}\;\rightarrow\;\text{根節點}\;\rightarrow\;\text{右子樹}

此規則必須遞迴套用於每棵子樹。最大堆積並非二元搜尋樹,因此中序走訪的結果不必由小到大排列。

解題方法

原圖的根節點為 2727,左右子節點分別為 19、2119、21;1919 的左右子節點為 13、1713、17,2121 的左右子節點為 11、1011、10。另外,1313 的左右子節點為 9、79、7,1717 的左子節點為 88。

1. 插入 3535 並向上調整

依完全二元樹的插入規則,3535 放在 1717 的右子節點。由於 3535 大於沿途的父節點,因此依序交換:

35↔17,35↔19,35↔2735 \leftrightarrow 17,\qquad 35 \leftrightarrow 19,\qquad 35 \leftrightarrow 27

調整後的堆積如下:

          35
        /    \
      27      21
     /  \    /  \
   13   19  11  10
  / \   / \
 9   7 8  17

2. 依「左、根、右」走訪

根節點 3535 的左子樹以 2727 為根:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 14 題5 分

Consider a binary search tree which stores distinct elements. The keys in the left subtree of a node are smaller than those in the right subtree of the node (if any). Denote NIL as a null pointer. pp and rightright are the pointers directing to a node’s parent node and the right child node, respectively. Function Tree_Minimum(nn) returns the minimum node (i.e., the node that has the smallest key) of a subtree rooted at node nn. Assume that xx is a node in the binary search tree. The following code fragment is executed. After that, yy is not equal to NIL.

if right[x]≠NILright[x]\ne NIL
y=Tree_Minimum(right[x])y=Tree\_Minimum(right[x]); return
y=p[x]y=p[x]
while y≠NILy\ne NIL and x=right[y]x=right[y]
do x=yx=y and y=p[y]y=p[y]
return

Which of the following statements are (or is) TRUE? ____

(A) yy must be the successor node of xx.
(B) yy must be the predecessor node of xx.
(C) xx must be the maximum node of a subtree rooted at node yy.
(D) yy must be the minimum node of a subtree rooted at node xx.
(E) yy must be the parent node of xx.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

二元搜尋樹中,節點 xx 的後繼是鍵值大於 xx 的所有節點中鍵值最小的節點。

  • 若 xx 有右子樹,後繼就是右子樹中的最小節點。
  • 若 xx 沒有右子樹,沿父節點往上走,跳過所有「目前節點是父節點右子節點」的路徑;第一個讓目前節點位於其左側的祖先,就是後繼。

題目已指定最後 y≠NILy\ne NIL,表示 xx 確實有後繼。

解題方法

分支判斷程式如何找到 yy:

  1. 若 right[x]≠NILright[x]\ne NIL,程式令 y=Tree_Minimum(right[x])y=Tree\_Minimum(right[x])。右子樹中的節點都大於 xx,其中最小者正是後繼。
  2. 若 right[x]=NILright[x]=NIL,程式從 xx 的父節點往上走。只要目前節點是父節點的右子節點,就繼續上爬;停止時,yy 是第一個使目前節點位於其左側的祖先。該祖先大於 xx,且是往上可找到的最小鍵值,因此是後繼。

所以兩種情況都找到 xx 的後繼。

選項分析

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 15 題5 分

LL is a circular linked list. Initially, LL is empty. The elements 3,4,5,9,3,4,5,9, and 77 are appended to LL one by one. Assume that xx is a node of LL. Denote next[x]next[x] and prev[x]prev[x] as the next and previous nodes of xx, respectively. value[x]value[x] is the element stored in xx. Given that xx stores element 7. Thus, value[next[x]]value[next[x]] and value[prev[x]]value[prev[x]] are 3 and 9, respectively.

What is value[x]value[x] after the following code fragment is executed?

n=1n=1

while n<2020n<2020

  • x=next[next[x]]x=next[next[x]]
  • n=n+1n=n+1

x=prev[prev[prev[x]]]x=prev[prev[prev[x]]]

(A) 3
(B) 4
(C) 5
(D) 9
(E) 7

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

循環鏈結串列的節點會首尾相接,因此沿著 next 前進或沿著 prev 後退,位置都可以用「模節點數」計算。本題共有 55 個節點;每次執行 x=next[next[x]],等於讓 x 沿 next 前進 22 個節點。

解題方法

依序加入元素後,串列順序為:

3→4→5→9→7→33\to4\to5\to9\to7\to3

初始時 xx 指向元素 77。迴圈從 n=1n=1 開始,直到 n=2020n=2020 時停止,因此共執行 20192019 次。每次前進 22 個節點,總共前進:

2×2019=4038 個節點2\times2019=4038\text{ 個節點}

因為串列有 55 個節點,40384038 除以 55 餘 33,所以迴圈後相當於從 77 沿 next 前進 33 個節點:

7→3→4→57\to3\to4\to5

接著執行 x=prev[prev[prev[x]]],從 55 沿 prev 後退 33 個節點:

5→4→3→75\to4\to3\to7
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

其他考古題