Next, we show that WMTO is fixedparameter tractable with respect to the parameter q_{
v
} by extending the dynamic programming algorithm for crosspairfree instances. Formally, q_{
v
} is defined as follows. For a rooted WMTO instance (T = (V, E), P) with root r, let Q denote the set of cross pairs. Moreover, for v ∈ V let Q_{
v
} := P_{
v
} ∩ Q be the set of cross pairs passing through v. With respect to the root r the maximum number q_{
v
} (r) of cross pairs passing through a vertex is given by max_{
v∈V
}Q_{
v
} . Then, q_{
v
} is the minimum value of q_{
v
} (r) over all possible choices r to root T.
Theorem 3. On nvertex trees, WEIGHTED MAXIMUM TREE ORIENTATION with given root can be solved in O( 2 ^{
qv
} ⋅ q_{
v
} ⋅ n^{2}) time, where q_{
v
} denotes the maximum number of cross pairs passing through a vertex.
Proof. The basic idea of the algorithm is to incorporate the cross pairs by trying for every vertex all possibilities to satisfy the cross pairs passing through this vertex. To this end, we extend the matrix S by an additional dimension. As a consequence, the dynamic programming update step becomes significantly more intricate.
Let (T = (V, E), P, ω) be a rooted WMTO instance with root r. For the presentation of the algorithm we use the same notation as in the proof of Theorem 2. In addition, we employ the following definitions.
Let w ∈ V. A possibility to satisfy the cross pairs in Q_{
w
} is represented by a coloring c_{
w
} : Q_{
w
} → {0, 1}, meaning that a cross pair q ∈ Q_{
w
} must be satisfied iff c_{
w
} (q) = 1. Let C_{
w
} denote the set of all 0/1colorings of Q_{
w
} . Note that $\left{C}_{w}\right={2}^{\left{Q}_{w}\right}$. To incorporate the cross pairs, for every v, w ∈ V with v ≺ w or v = w and for every coloring c_{
w
} , the dynamic programming table S contains two entries S(v, w, c_{
w
} ) and S(w, v, c_{
w
} ). Informally speaking, S(v, w, c_{
w
} ) denotes the maximum weight of an orientation of ${T}_{w}^{v}$ under the assumption that all cross pairs q ∈ Q_{
v
} with c_{
w
} (q) = 1 are "satisfiable" and the edges in path_{
T
}(v, w) are oriented from v towards w. Entry S(w, v, c_{
w
} ) is defined analogously, but here we assume that the edges in path_{
T
}(v, w) are oriented from w towards v. For a precise description, we use the following notation.
Clearly, we are interested only in colorings
c_{
w
} of
Q_{
w
} such that any two cross pairs
q,
q' ∈
Q_{
w
} with
c_{
w
} (
q) =
c_{
w
} (
q') = 1 are not in conflict. We call such a coloring
locally feasible. Moreover, we extend the notion "feasible" as follows. As informally described above, we distinguish the two cases that the edges on path
_{
T
}(
v,
w) are oriented
 (1)
from v to w (for entry S(v, w, c_{
w
} )), or
 (2)
from w to v (for entry S(w, v, c_{
w
} )).
For the case (1), a locally feasible coloring c_{
w
} is called feasible if for each cross pair [s, t] ∈ Q_{
w
} with c_{
w
} ([s, t]) = 1 orienting the edges on path_{
T
}(v, w) from v to w and the edges on path_{
T
}(s, t) from s to t is simultaneously possible. Analogously, a locally feasible coloring c_{
w
} is feasible for case (2) when orienting the edges on path_{
T
}(s, t) from s to t does not contradict the orientation of the edges on path_{
T
}(v, w) from w to v.
For a coloring c_{
w
} of Q_{
w
} , we must ensure that in the considered orientations of $\left({T}_{w}^{v},{P}_{w}^{v}\right)$ all cross pairs q ∈ Q_{
w
} with c_{
w
} (q) = 1 are satisfiable. Therefore, we call an orientation of $\left({T}_{w}^{v},{P}_{w}^{v}\right)$consistent with a cross pair [s, t] ∈ Q_{
w
} (note that $\left[s,t\right]\in P\backslash {P}_{w}^{v}$ is allowed) if the common edges of ${T}_{w}^{v}$ and path_{
T
}(s, t) are oriented from s to t. Finally, we call an orientation of $\left({T}_{w}^{v},{P}_{w}^{v}\right)$consistent with a coloring c_{
w
} of Q_{
w
} if the orientation is consistent with each cross pair q ∈ Q_{
w
} with c_{
w
} (q) = 1.
With these notations, we can formally define the meaning of the entries of S. For every two vertices v, w ∈ V with v ≺ w or v = w and for every 0/1coloring c_{
w
} ∈ Q_{
w
} the entry S(v, w, c_{
w
} ) is ∞ if the coloring c_{
w
} is not feasible for case (1). Otherwise, S(v, w, c_{
w
} ) denotes the maximum weight of an orientation of $\left({T}_{w}^{v},{P}_{w}^{v}\right)$ among all orientations of $\left({T}_{w}^{v},{P}_{w}^{v}\right)$ fulfilling the following constraints:

the edges on path_{
T
}(v, w) are oriented from v to w, and

the orientation is consistent with c_{
w
}.
This definition ensures that the orientation is not conflicting with the realization implied by the coloring c_{
w
} and the fixed orientation of path_{
T
}(v, w). The entry S(w, v, c_{
w
} ) is defined analogously with the difference that here we assume that the edges on path_{
T
}(v, w) are oriented from w to v. Note that the cross pairs having only one endpoint in ${T}_{w}^{v}$ are not contained in ${P}_{w}^{v}$, and hence do not contribute to the weight of an orientation of $\left({T}_{w}^{v},{P}_{w}^{v}\right)$. Observe that ${{\displaystyle max}}_{{c}_{r}\in {C}_{r}}S(r\mathrm{,}r\mathrm{,}{c}_{r})$ is the maximum weight of an orientation of the whole instance (T, P, ω) since ${T}_{r}^{r}=T$ and we build the maximum over all colorings of the cross pairs in Q_{
r
} . Next, we provide a strategy to compute the entries of S in accordance with this definition. In the update step, we need to adjust the tables of a vertex w with the tables of its children. Doing so, we have to ensure that we only consider colorings that are not contradictory to each other. Let c_{
u
} denote a coloring of Q_{
u
} and c_{
w
} denote a coloring of Q_{
w
} . We use c_{
u
} c_{
w
} to denote that c_{
u
} and c_{
w
} agree in the coloring of the cross pairs in Q_{
u
} ∩ Q_{
w
} , that is, for all q ∈ Q_{
u
} ∩ Q_{
w
} it holds that c_{
u
} (q) = c_{
w
} (q). Finally, let W_{LCA}(w, c_{
w
} ) denote the sum of the weights of the cross pairs [s, t] ∈ Q_{
w
} with c_{
w
} ([s, t]) = 1 that have w as their least common ancestor.
For the computation of
S, visit each vertex
w ∈
V in a postorder traversal of
T. For each
w consider all vertices
v ∈
V with
v ≺
w or
v =
w. Moreover, let
u_{ℓ}, …,
u_{
l
} denote the children of
w (if
w is not a leaf). Then, for each coloring
c_{
w
} ∈
C_{
w
} , set
S(
v,
w,
c_{
w
} ):= ∞ if
c_{
w
} is infeasible for case (1), otherwise set (omit the sum if
w is a leaf)
$\begin{array}{c}S\left(v,w,{c}_{w}\right):=A\left(v,w\right)+{W}_{\mathsf{\text{LCA}}}\left(w,{c}_{w}\right)\\ \phantom{\rule{1em}{0ex}}\phantom{\rule{1em}{0ex}}\phantom{\rule{1em}{0ex}}\phantom{\rule{1em}{0ex}}\phantom{\rule{1em}{0ex}}\phantom{\rule{1em}{0ex}}\phantom{\rule{1em}{0ex}}\phantom{\rule{1em}{0ex}}+\sum _{i=1}^{\ell}M\left({u}_{i},v,w,{c}_{w}\right)\end{array}$
(8)
where
$\begin{array}{l}M({u}_{i}\mathrm{,}v\mathrm{,}w\mathrm{,}{c}_{w}):=max\{\\ max\{S(v\mathrm{,}{u}_{i}\mathrm{,}{c}_{{u}_{i}})A(v\mathrm{,}w):{c}_{{u}_{i}}\in {\mathcal{C}}_{{u}_{i}}\mathrm{,}{c}_{{u}_{i}}{c}_{w}\}\mathrm{,}\\ max\{S({u}_{i}\mathrm{,}w\mathrm{,}{c}_{{u}_{i}}):{c}_{{u}_{i}}\in {\mathcal{C}}_{{u}_{i}}\mathrm{,}{c}_{{u}_{i}}{c}_{w}\left\}\right\}\mathrm{.}\end{array}$
(9)
and
S(
w,
v,
c_{
w
} ):= ∞ if
c_{
w
} is infeasible for case (2), otherwise set (omit the sum if
w is a leaf)
$\begin{array}{c}S\left(w,v,{c}_{w}\right):=A\left(w,v\right)+{W}_{\mathsf{\text{LCA}}}\left(w,{c}_{w}\right)\\ \phantom{\rule{1em}{0ex}}\phantom{\rule{1em}{0ex}}\phantom{\rule{1em}{0ex}}\phantom{\rule{1em}{0ex}}\phantom{\rule{1em}{0ex}}\phantom{\rule{1em}{0ex}}\phantom{\rule{1em}{0ex}}\phantom{\rule{1em}{0ex}}+\sum _{i=1}^{\ell}M\left({u}_{i},w,v,{c}_{w}\right)\end{array}$
(10)
where
$\begin{array}{l}M({u}_{i}\mathrm{,}w\mathrm{,}v\mathrm{,}{c}_{w}):=max\{\\ max\{S({u}_{i}\mathrm{,}v\mathrm{,}{c}_{{u}_{i}})A(w,v):{c}_{{u}_{i}}\in {\mathcal{C}}_{{u}_{i}}\mathrm{,}{c}_{{u}_{i}}{c}_{w}\}\mathrm{,}\\ max\{S(w\mathrm{,}{u}_{i}\mathrm{,}{c}_{{u}_{i}}):{c}_{{u}_{i}}\in {\mathcal{C}}_{{u}_{i}}\mathrm{,}{c}_{{u}_{i}}{c}_{w}\left\}\right\}\mathrm{.}\end{array}$
(11)
Herein, A is defined exactly as in the proof of Theorem 2.
Correctness. For the correctness, we argue that for v, w ∈ V with v ≺ w and for a coloring c_{
w
}∈ C_{
w
} that is feasible for case (1), the maximum weight of an orientation of $\left({T}_{w}^{v},{P}_{w}^{v}\right)$ consistent with c_{
w
} that orients the edges on path_{
T
}(v, w) from v to w is $A\left(v,w\right)+{W}_{\mathsf{\text{LCA}}}\left(w,{c}_{w}\right)+{\sum}_{i=1}^{\ell}M\left({u}_{i},v,w,{c}_{w}\right)$, and, hence, S(v, w, c_{
w
} ) is computed correctly. To this end, first consider the case that w is a leaf. Then, ${T}_{w}^{v}$ is identical to path_{
T
}(v, w) and Q_{
w
} = ∅. Hence, in this case exactly the source–target pairs $\left[s,t\right]\in P\backslash {P}_{w}^{v}$ with s ≺ t are satisfied whose total weight per definition is A(v, w). Second, consider the case that w is an internal vertex with children u_{1}, …, u_{ℓ}. Moreover, let ${\overrightarrow{T}}_{w}^{v}$ be an optimal orientation consistent with c_{
w
} that orients the edges on path_{
T
}(v, w) from v to w. Assume that this orientation contains for a child u_{
i
} the arc (u_{
i
} , w). Then, with respect to w and u_{
i
} , the subgraph of ${\overrightarrow{T}}_{w}^{v}$ induced by the vertices in ${V}_{{u}_{i}}^{w}$ is an orientation of ${T}_{{u}_{i}}^{w}$ consistent with a coloring ${c}_{{u}_{i}}$ that clearly agrees with c_{
w
} . Thus, the contribution of u_{
i
} to the weight of ${\overrightarrow{T}}_{w}^{v}$ is the maximum $S\left({u}_{i},w,{c}_{{u}_{i}}\right)$ over all ${c}_{{u}_{i}}\in {Q}_{{u}_{i}}$ that agree with c_{
w
} . Similarly, if ${\overrightarrow{T}}_{w}^{v}$ contains the arc (w, u_{
i
} ) for a child u_{
i
} , the subgraph of ${\overrightarrow{T}}_{w}^{v}$ induced by the vertices in ${V}_{{u}_{i}}^{w}$ is an orientation of ${T}_{{u}_{i}}^{w}$ consistent with a coloring ${c}_{{u}_{i}}$ that clearly agrees with c_{
w
} . Thus, the contribution of the pairs in ${P}_{w}^{v}$ with at least one endpoint in ${T}_{{u}_{i}}$ is the maximum of $S\left(v,{u}_{i},{c}_{{u}_{i}}\right)A\left(v,w\right)$ over all ${c}_{{u}_{i}}\in {\mathcal{C}}_{{u}_{i}}$ that agree with c_{
w
} (here, we have to subtract the number of satisfied pairs with both endpoints on path_{
T
}(v, w) that are already accounted for by the term A(v, w) in (8)). Finally, observe that the contribution of the cross pairs q in ${P}_{w}^{v}$ with c_{
w
} (q) = 1 for which w is the least common ancestor are not taken into account in the contributions of the u_{
i
} 's. This is done by the term W_{LCA} in (8). The argumentation for the correctness of the computation of S(w, v, c_{
w
} ) follows analogously.
Running time. Next, we analyze the running time. We use the following notation and implementation details. For w ∈ V let ${Q}_{w}=\left\{{p}_{1}^{w},\dots ,{p}_{{n}_{w}}^{w}\right\}$. A coloring c_{
w
} : Q_{
w
} → {0, 1} is realized by a tuple $\left({c}_{1},\dots ,{c}_{{n}_{w}}\right)\in {\left\{0,1\right\}}^{{n}_{w}}$ with ${c}_{w}\left({p}_{i}^{w}\right)={c}_{i}$ for all 1 ≤ i ≤ n_{
w
} . Moreover, the dynamic programming table S is realized by two tables ${S}_{v,w}^{up}$ and ${S}_{v,w}^{down}$ for every pair v, w ∈ V with v ≺ w or v = w with an entry for every coloring $c\in {\left\{0,1\right\}}^{{n}_{w}}$ where ${S}_{v,w}^{up}\left(c\right)=S\left(w,v,c\right)$ and ${S}_{v,w}^{down}\left(c\right)=S\left(v,w,c\right)$. The table A is computed exactly as in the proof of Theorem 2 in O(n^{2}) time in a preprocessing step. Moreover, note that after O(n) preprocessing time, least common ancestors of the source–target pairs can be found in constant time [12].
To prove the running time, we show that for every pair
v,
w with
v ≺
w or
v =
w the computation in (8) and (10) can be done in
$O{(2}^{{q}_{v}}\cdot {q}_{v}\cdot {{\displaystyle deg}}_{T}(w))$ time. We focus on the computation of (8). The running time analysis for (10) follows by the same arguments. The crucial observation is that the summands in the sum in (8) are independent of each other in the sense that the determination of the maximum for one child
u_{
i
} (the computation of
M(
u_{
i
} ,
v,
w,
c_{
w
} )) does not depend on the decision made for a different child. Hence, for the computation of the entries of
${S}_{v,w}^{down}$ proceed as follows. Consider each child
u of
w one after another. Let
${Q}_{w}=\left\{{q}_{1},\dots ,{q}_{s},{q}_{1}^{\prime},\dots ,{q}_{x}^{\prime}\right\}$ and
${Q}_{u}=\left\{{q}_{1},\dots ,{q}_{s},{q}_{1}^{\prime \prime},\dots ,{q}_{y}^{\prime \prime}\right\}$, that is, {q
_{1}, …,
q_{
s
} } =
Q_{
w
} ∩
Q_{
u
} . The crucial point is that we assume that the tables
${S}_{w,u}^{up}$ and
${S}_{v,u}^{down}$ are sorted in lexicographical order of the colorings
${\left\{0,1\right\}}^{{n}_{u}}$. This ensures that the colorings of
u that agree with a coloring
$c\in {\left\{0,1\right\}}^{{n}_{w}}$ are ordered consecutively in
${S}_{w,u}^{up}$ and
${S}_{v,u}^{down}$. Since the tables
${S}_{w,u}^{up}$ and
${S}_{v,u}^{down}$ contain each at most
${2}^{{q}_{v}}$ entries, the sorting can be achieved in time
$O\left({2}^{{q}_{v}}\right)$ using bucket sort. Then, for each fixed
v,
w, and
u all the values
M(
v,
w,
u,
c) can be computed in
$O\left({2}^{{q}_{v}}{q}_{v}\right)$ time in one iteration over
${S}_{w,u}^{up}$ and
${S}_{v,u}^{down}$ and, hence, for all children of
w the running time is bounded by
$O{(2}^{{q}_{v}}\cdot {q}_{v}\cdot {{\displaystyle deg}}_{T}(w))$. Thus, the overall running time is bounded by
$O\left({\displaystyle \sum _{v\mathrm{,}w\in V}}{2}^{{q}_{v}}\cdot {q}_{v}\cdot {{\displaystyle deg}}_{T}(w)\right)=O{(2}^{{q}_{v}}{q}_{v}{n}^{2})\mathrm{,}$
(12)
since O(∑ _{
w∈V
} deg_{
T
}(w)) = O(n) in trees. □