Recall that an unrooted tree of size $$$n$$$ is an undirected connected graph with $$$n - 1$$$ edges.
Given an unrooted tree of size $$$n$$$, find two nodes $$$u$$$ and $$$v$$$, such that when the undirected edge $$$(u, v)$$$ is added to the tree, a simple cycle that consists of at least $$$3$$$ nodes is created.
It can be shown that under the constraints of the problem, an answer always exists.
The first line of the input consists of a single integer $$$n$$$ $$$(3 \le n \le 100)$$$, the size of the tree.
On each of the next $$$n - 1$$$ lines, there are two integers $$$u$$$, $$$v$$$ $$$(1 \le u, v \le n)$$$, representing an edge in the tree between nodes $$$u$$$ and $$$v$$$.
It is guaranteed that the input provides a valid tree.
Output exactly two integers $$$u$$$ and $$$v$$$, such that when the edge $$$(u, v)$$$ is added to the tree, there is a cycle of size at least $$$3$$$.
31 22 3
1 3
41 21 33 4
1 4
See the second example after the edge $$$(1, 4)$$$ is added:
Spyrosaliv and Cow the Cow are playing a game on a tree of size $$$n$$$.
Spyrosaliv will start at node $$$s$$$. Spyrosaliv and Cow the Cow will play in alternating turns, with Spyrosaliv playing first.
Suppose Spyrosaliv is on node $$$u$$$. Spyrosaliv has to choose any node $$$v$$$ such that the edge $$$(u, v)$$$ exists and is not blocked, and move there. If such a node doesn't exist, he loses. If $$$v = d$$$, he wins.
If it's Cow the Cow's turn, he will do these two operations in the following order:
If both Spyrosaliv and Cow the Cow play optimally, will Spyrosaliv win the game?
The first line contains exactly three integers: $$$n$$$, $$$s$$$, and $$$d$$$ $$$(2 \le n \le 2 \cdot 10^5$$$, $$$1 \le s, d \le n$$$, $$$s \neq d$$$), the size of the tree, Spyrosaliv's starting node, and Spyrosaliv's destination, respectively.
Each of the next $$$n-1$$$ lines contains two integers $$$u$$$ and $$$v$$$, representing an edge $$$(u, v)$$$ in the tree.
The input is guaranteed to give a valid tree, and it is guaranteed that $$$s \neq d$$$.
Output exactly one string: 'YES' if Spyrosaliv will win and 'NO' otherwise. Note that 'yEs', 'Yes', 'nO', and so on, are also acceptable answers.
5 2 41 42 54 33 5
NO
2 1 21 2
YES
In the first example, the tree is as follows:
Spyrosaliv starts at node $$$2$$$, and wins if he gets to node $$$4$$$. A possible sequence of moves is as follows:
It can be shown that in this example, no matter how Spyrosaliv plays, Cow the Cow has a winning strategy.
Cow the Cow has a tree of size $$$n$$$ rooted at node $$$1$$$. However, Cow the Nerd thinks that Cow the Cow's tree is boring, so he has assigned you the task of painting it.
Specifically, let your current position be $$$u$$$. In a move, you will do one of the following:
For every node $$$u$$$ from $$$1$$$ to $$$n$$$, find out if it is possible to paint all nodes of the tree and escape successfully if you start from node $$$u$$$.
$$$^\dagger$$$A leaf of a rooted tree is a node that is not the root and has no children.
The first line contains exactly one integer $$$n$$$ $$$(2 \le n \le 2 \cdot 10^5)$$$.
The second line contains $$$n - 1$$$ integers $$$p_2, p_3, ..., p_n$$$, where $$$p_i$$$ is the parent of the $$$i$$$-$$$th$$$ node.
It is guaranteed that the input provides a valid tree.
Output a binary string $$$s$$$ of size $$$n$$$, where $$$s_i = 1$$$ if you can win starting from node $$$i$$$, and $$$s_i = 0$$$ otherwise.
51 1 1 1
11111
21
10
61 1 2 3 4
111000
In the first example, no matter which node you start from, there's at least one possible winning sequence of moves.
In the second example, if you start from node $$$2$$$, there's only one possible sequence of moves: paint node $$$2$$$, and since it is a leaf, move to the only unpainted node, $$$1$$$. Node $$$1$$$ is not a leaf, and all of its children have been painted; therefore, you lose.
A beautiful path of a weighted tree is a simple path that visits at least $$$2$$$ nodes, in which the edges it visits have alternating signs. Its cost is the sum of the weights of the edges it visits. A best path is a beautiful path with the maximum cost among all other beautiful paths in the tree.
Spyrosaliv had a weighted tree of size $$$n$$$, and he tried finding the cost of the best path. However, Cow the Cow, nerdy as he is, thought the problem was far too easy.
Therefore, he asked the following question: "In one operation, you can choose any two edges and swap their weights. What is the maximum possible cost of the best path after a finite number of such operations?"
Cow the Cow easily solved the problem, but Spyrosaliv struggled. Can you help Spyrosaliv solve this problem?
The first line contains one integer, $$$n$$$ $$$(2 \le n \le 2 \cdot 10^5)$$$.
In the $$$i$$$-$$$th$$$ of the next $$$n-1$$$ lines, there are three integers $$$u_i$$$, $$$v_i$$$, and $$$w_i$$$ $$$(-10^9 \le w_i \le 10^9, w_i \neq 0)$$$, denoting an edge $$$(u_i, v_i)$$$ in the tree with weight $$$w_i$$$.
The input is guaranteed to provide a valid tree.
Output one integer, the maximum possible cost of the best path after a finite number of swaps.
61 2 32 3 -13 4 24 5 -14 6 -130
4
51 2 82 3 -13 4 -1004 5 10
17
21 2 -10
-10
31 2 1001 3 150
150
For the first example, see the following tree:
The path that visits nodes $$$1, 2, 3$$$ in that order is beautiful with cost $$$2$$$, but the path $$$6, 4, 5$$$ is not, since two consecutive edges have weights with the same sign.
The best path is the path that visits nodes $$$1, 2, 3, 4$$$ in that order, since it is a beautiful path with the maximal cost of $$$4$$$.
In the second example, it is optimal to swap the weights of edges $$$(3, 4)$$$ and $$$(4, 5)$$$ in one operation. Then, the cost of the best path is $$$17$$$. It can be shown that $$$17$$$ is the maximal possible cost of the best path among all possible combinations of operations.
In the third example, there is exactly one beautiful path, which visits nodes $$$1$$$ and $$$2$$$; therefore, it is the best path with cost $$$-10$$$.
Cow Meows has a tree of size $$$n$$$ rooted at node $$$1$$$ and a constant integer $$$k$$$.
However, he is over $$$10^{10^{100}}$$$ years old, and math formulas have started messing with his vision.
Specifically, he thinks that two nodes $$$a$$$ and $$$b$$$ are similar if there exists any node $$$u$$$ that is an ancestor of both $$$a$$$ and $$$b$$$, and $$$dist(a, u) \equiv dist(b, u) \equiv 0 \pmod{k}$$$, where $$$dist(u, v)$$$ represents the number of edges in the simple path from $$$u$$$ to $$$v$$$.
Your goal is to help Cow Meows count the number of pairs $$$(u, v)$$$, $$$1\leq u \lt v \leq n$$$, such that nodes $$$u$$$ and $$$v$$$ are similar.
The first line contains two integers $$$n$$$ and $$$k$$$, $$$(1 \le k \le n \le 2 \cdot 10^5)$$$, the size of the tree and the constant respectively.
The second line contains $$$n-1$$$ integers $$$p_2, p_3, ..., p_n$$$, where $$$p_i$$$ is the parent of the $$$i$$$-$$$th$$$ vertex.
It is guaranteed that the input forms a valid tree.
Output exactly one integer, the number of distinct unordered pairs of similar nodes.
5 21 2 2 4
4
5 51 2 3 4
0
4 41 1 1
0
4 11 1 1
6
In the first example, the tree is the following:
With $$$k = 2$$$, the similar pairs are $$$(5, 2)$$$, $$$(3, 4)$$$, $$$(1, 4)$$$, $$$(1, 3)$$$.
In the final example, all nodes are similar to each other.
You are given a tree of size $$$n$$$ rooted at node $$$1$$$. Each node $$$u$$$ has a value $$$val_u$$$.
Let $$$dis(u, v)$$$ be the sum of the values of the nodes in the simple path from $$$u$$$ to $$$v$$$. Also, let $$$d_u = dis(1, u)$$$. Two nodes $$$u$$$ and $$$v$$$ are considered similar if $$$(d_u + d_v)^2 = (d_u - d_v)^2 = dis(u, v)$$$.
Your goal is to find the number of pairs $$$(u,v)$$$, $$$1 \le u \lt v \le n$$$, such that nodes $$$u$$$ and $$$v$$$ are similar.
The first line of the input contains one integer, $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$.
The second line of the input contains $$$n$$$ integers $$$val_1, val_2, ..., val_n$$$ $$$(-10^9 \le val_i \le 10^9)$$$, where $$$val_i$$$ is the value of the $$$i$$$-$$$th$$$ node.
The third line of the input contains $$$n-1$$$ integers $$$p_2, p_3, ..., p_n$$$ , where $$$p_i$$$ is the parent of the $$$i$$$-$$$th$$$ node.
It is guaranteed that the input forms a valid tree.
Output exactly one integer, the number of pairs $$$(u,v)$$$ $$$(1 \le u \lt v \le n)$$$ such that nodes $$$u$$$ and $$$v$$$ are similar.
60 1 0 -1 0 01 2 2 4 1
9
100 -7 0 5 7 0 -1 0 7 09 2 1 8 2 5 1 8 3
10
For the first example:
The pairs are $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(1, 5)$$$, $$$(1, 6)$$$, $$$(2, 6)$$$, $$$(3, 6)$$$, $$$(4, 6)$$$, $$$(5, 6)$$$.
Take as an example the pair $$$(4, 6)$$$. Because $$$d_4 = 0$$$, $$$d_6 = 0$$$, and $$$dis(4, 6) = 0$$$, then $$$(d_4 + d_6)^2 = (d_4 - d_6)^2 = dis(4, 6) = 0$$$. Therefore, the nodes $$$4$$$ and $$$6$$$ are similar.
There are $$$k$$$ cows playing a game, each positioned on distinct nodes of a tree of size $$$n$$$ rooted at node $$$1$$$. The $$$i$$$-$$$th$$$ cow has been assigned a value, $$$val_i$$$. Every node can be either blocked or not blocked. Initially, no node is blocked. In each second of the game, every cow chooses to make one of the following moves:
If in a second every cow chooses to stay at its current node, the game ends. The last second does not count towards the total amount of seconds the game lasted. Multiple cows may be on the same node by the end of a game. The sum of a node is the sum of the values of the cows on that node when the game ends. The score of the game is the sum of the node with the maximal sum among all other nodes. After a game ends, all cows return to their starting nodes. The goal of the cows is to maximize the score of the game in as little time as possible.
However, SpyrosAliv does not like cows; therefore, he will try to ruin their game.
Specifically, there will be $$$q+1$$$ games. The first game will be played without any changes. Before the $$$i$$$-$$$th$$$ of the next $$$q$$$ games starts, SpyrosAliv will do one of the following:
Note that changes done on game $$$j$$$ are not reverted and affect all following games $$$j, j+1, ..., q, q+1$$$.
For each game, help Cow the Cow calculate the score of the game and how long it lasted if the cows played optimally.
The first line of the input contains two integers, $$$n$$$ and $$$k$$$, $$$(1 \le k \le n \le 2 \cdot 10^5)$$$.
The second line of the input contains $$$k$$$ distinct integers, where the $$$i$$$-$$$th$$$ integer is the position of the $$$i$$$-$$$th$$$ cow.
The third line of the input contains $$$k$$$ integers, $$$val_1, val_2, ..., val_k$$$ $$$(1 \le val_i \le 10^9)$$$, the value of the $$$i$$$-$$$th$$$ cow.
The fourth line of the input contains $$$n-1$$$ integers $$$p_2, p_3, ..., p_n$$$, where $$$p_i$$$ $$$(1 \le p_i \le n)$$$ is the parent of the $$$i$$$-$$$th$$$ node.
The following line contains exactly one integer, $$$q$$$ $$$(0 \le q \le n)$$$, the number of queries.
The $$$i$$$-$$$th$$$ of the next $$$q$$$ lines contains two integers, $$$t_i$$$ and $$$u_i$$$ $$$(1 \le t_i \le 2, 1 \le u_i \le n)$$$, the type of the $$$i$$$-$$$th$$$ query and its node respectively. If $$$t_i = 1$$$, then SpyrosAliv removes the cow from node $$$u_i$$$. If $$$t_i = 2$$$, then SpyrosAliv blocks node $$$u_i$$$.
It is guaranteed that if $$$t_i = 1$$$, there is a cow at node $$$u_i$$$. If $$$t_i = 2$$$, $$$u_i$$$ is not blocked and has no cow.
It is guaranteed that the input provides a valid tree.
Output $$$q + 1$$$ lines.
On the $$$i$$$-$$$th$$$ line, output two integers: the score of the $$$i$$$-$$$th$$$ game and the seconds it lasted if the cows played optimally.
7 42 3 5 75 3 8 21 2 2 4 4 471 22 22 41 51 31 72 5
18 2 13 2 10 1 8 0 3 0 2 0 0 0 0 0
5 43 4 5 21 10 5 75 2 2 152 11 41 32 32 4
23 2 23 2 13 2 12 1 12 1 12 1
5 35 2 41891 54 56455 1 1 452 31 41 52 51 2
7590 2 7590 2 1945 1 54 0 54 0 0 0
On the first example, this is what the tree looks like before the third game:
Green nodes have a cow on them, and their label is the value of their cow. Node $$$2$$$ has been blocked. The optimal moves for the cows are the following:
The game has lasted $$$1$$$ second. Node $$$4$$$ has a sum of $$$10$$$, node $$$3$$$ has a sum of $$$3$$$, and all other nodes have a sum of $$$0$$$. The score of the game is the sum of node $$$4$$$, which is $$$10$$$. The cows needed $$$1$$$ second to achieve this score. It can be shown that $$$10$$$ is the maximal possible score they can achieve, and that they need at least $$$1$$$ second to achieve it.
After the game ends, all cows return to their starting nodes. That means that the image above represents the tree both before and right after the third game.