F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

Miserable Faith

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 107    Accepted Submission(s): 32


Problem Description
If faith has a color, it must be miserable red.

A tree is an undirected connected graph without cycles.

You are given a tree of $n$ nodes numbered from $1$ to $n$ The root of the tree is a node number $1$.

The color of $i$-th node is $i$.

For each node $u$ and node $v$ which are connected by an edge, the weight of edge is $1$ if the color of node $u$ is different from the color of node $v$ otherwise the weight of edge is $0$.

Let's denote path between node $u$ and node $v$ as $(u,v)$.

Let's denote the number of edges in $(u,v)$ as $d(u,v)$.

Let's denote the sum of weighted edges in $(u,v)$ as $dist(u,v)$.

if $u$ is an ancestor of $v$ and $v$ is an ancestor of $w$ then $u$ is an ancestor of $w$.

$v\in subtree(u)$ if $u$ is an ancestor of $v$.

Let's denote $max_{i=1}^n d(i,u)$ as $FakeDeep_u$, while $dist(i,u)=0$ and $i$ is an ancestor of $u$.

There are $m$ queries. Each query has one of four types:

$1$ $u$ $c$: change the color of nodes in $(u,1)$ to $c$, $c$ is different from any other color in the tree.

$2$ $u$ $v$: print $dist(u,v)$.

$3$ $u$: print $\sum_{i=1}^n dist(i,u),i\in subtree(u)$.

$4$ : print $\sum_{i=1}^n FakeDeep_i$.
 

Input
The first line contains an integer $T$ $( 1\leq T\leq 10)$ representing the number of test cases.

For each test case, there are two integer $n$ $(1\leq n,m\leq 10^5)$ representing the number of nodes in the tree and the number of queries.

For next $n-1$ lines, each line contain two numbers $u,v$, which means that there is an edge between $u$ and $v$.

Each of the next $m$ lines contains $1$ or $2$ or $3$ integers:

$1$ $u$ $c$ $(1\leq u\leq n,1\leq c \leq 10^6)$

$2$ $u$ $v$ $(1\leq u,v\leq n)$

$3$ $u$ $(1\leq u\leq n)$

$4$
 

Output
For each query of type $2,3,4$, output the only line containing just one integer denoting the answer.
 

Sample Input
1 7 7 1 2 1 6 6 7 3 4 1 3 3 5 1 4 8 1 7 4 4 2 5 7 3 3 1 2 3 4
 

Sample Output
4 2 1 3
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2024-05-11 21:18:42, Gzip enabled