Tree Intersection
Submit Page
Summary
Time Limit: 1 Sec
Memory Limit: 128 Mb
Submitted: 498
Solved: 176
Description
Bobo has a tree with n vertices numbered by1,2,…,n and (n-1) edges. The i-th vertex has color c i, and the i-th edgeconnects vertices a i and b i.Let C(x,y) denotes the set of colors insubtree rooted at vertex x deleting edge (x,y).Bobo would like to know R_i which is thesize of intersection of C(a i,b i) and C(b i,a i) for all 1≤i≤(n-1). (i.e. |C(ai,b i)∩C(b i,a i)|)
Input
The input contains at most 15 sets. Foreach set:
The first line contains an integer n(2≤n≤10 5).
The second line contains n integers c 1,c2,…,c n (1≤c_i≤n).
The i-th of the last (n-1) lines contains 2integers a i,b i (1≤a i,b i≤n).
Output
For each set, (n-1) integers R 1,R 2,…,R n-1.
Sample Input
4
1 2 2 1
1 2
2 3
3 4
5
1 1 2 1 2
1 3
2 3
3 5
4 5
Sample Output
1
2
1
1
1
2
1
题意:给你一棵树,对于每条边,把这条边删了,树分成了两个集合,求这两个集合中共同的颜色数量
解题思路:莫队或线段树启发式合并,关键都是看一个集合是否达到一种颜色的总量
线段树启发式合并:
#include
#include
#include
#include
#include
#include
莫队:
#include
#include
#include
#include
#include
#include