This page is still under construction.

Parts of this page are still being built. What you see may change.

Tree

Time limit3sMemory limit256 MB

Summary
Support parent changes, path recoloring, and distinct-color path queries on a dynamic rooted tree.
Level

Medium6 of 10

Topics
Tree, Brute force, Hash map
Solved
No attempts yet

Problem

A tree has NN nodes numbered 0 to N−1N-1. Node 0 is the root, and at the start every other node is a child of node 0. Every edge starts with color 0.

Three operations apply to the tree. They change the shape of the tree or paint its edges.

  1. paint(a, b, c): find the shortest path between node a and node b, then paint every edge on that path with color c.
  2. move(a, b): change the parent of node a to node b. Node b is never inside the subtree rooted at node a. If p is the parent of node a before the change, the new edge (a, b) keeps the color of the original edge (a, p).
  3. count(a, b): find the shortest path between node a and node b, then print how many distinct colors appear on the edges of that path.

A color c is written as an integer.

Consider an initial tree with 6 nodes and the operations move(1,3), move(5,3), paint(5,4,8), move(3,4), paint(0,3,7), count(2,5) applied in that order. Figure 1 is the initial shape. Figures 2 to 4 show, in order, how the shape of the tree and the edge colors change after each operation.

Figure 1. The initial shape

Figure 2. Left: after move(1,3). Right: after move(5,3)

Figure 3. After paint(5,4,8)

Figure 4. Left: after move(3,4). Right: after paint(0,3,7)

The last operation count(2,5) prints 3. As the right half of Figure 4 shows, the edges on the shortest path between node 2 and node 5 carry the colors {0,7,8}\{0, 7, 8\}, which is three distinct colors.

Given the operations in order, write a program that carries out each one efficiently.

Input

The first line contains the number of nodes NN (1≤N≤1051 \le N \le 10^5) and the number of operations KK (1≤K≤3×1051 \le K \le 3 \times 10^5). Each of the next KK lines holds one operation. The first integer on a line is rr (1≤r≤31 \le r \le 3), the type of the operation.

  • If r=1r = 1 the operation is paint, and three more integers follow on the same line: aa, bb (0≤a,b≤N−10 \le a, b \le N-1) and cc (0≤c≤1090 \le c \le 10^9). Here aa and bb are node numbers and cc is a color number.
  • If r=2r = 2 the operation is move, and two more integers follow on the same line: aa (1≤a≤N−11 \le a \le N-1) and bb (0≤b≤N−10 \le b \le N-1). Both are node numbers.
  • If r=3r = 3 the operation is count, and two more integers follow on the same line: aa and bb (0≤a,b≤N−10 \le a, b \le N-1). Both are node numbers.

In the initial tree of NN nodes, node 0 is the root, every other node has node 0 as its parent, and every edge has color 0.

For a paint or a count operation, the shortest path between node a and node b always has length at most 1,000.

Output

For each count operation in the input, print its result on its own line, in the order the operations are given. If a and b are equal, the path has no edges, so print 0.

Examples2

  1. Example 1

    Input
    6 8
    2 1 3
    2 5 3
    1 5 4 8
    3 4 5
    2 3 4
    1 0 3 7
    3 2 5
    3 4 2
    
    Expected output
    1
    3
    2
    
  2. Example 2

    Input
    7 15
    2 3 2
    2 4 3
    2 5 3
    2 6 2
    3 1 6
    1 3 3 2
    1 1 6 5
    1 4 2 3
    1 2 5 4
    1 2 0 7
    3 4 6
    3 4 1
    2 3 2
    3 2 2
    3 5 6
    
    Expected output
    1
    3
    4
    0
    2