| ps | |
|---|---|
| 링크 | acmicpc.net/… | 
| 출처 | BOJ | 
| 문제 번호 | 15899 | 
| 문제명 | 트리와 색깔 | 
| 레벨 | 플래티넘 2 | 
| 분류 | 
 구간 쿼리  | 
	
| 시간복잡도 | O((n+m)logC) | 
| 인풋사이즈 | n<=200,000, m<=200,000, c<=200,000 | 
| 사용한 언어 | PyPy | 
| 제출기록 | 211420KB / 1048ms | 
| 최고기록 | 1048ms | 
| 해결날짜 | 2021/04/30 | 
"""Solution code for "BOJ 15899. 트리와 색깔".
- Problem link: https://www.acmicpc.net/problem/15899
- Solution link: http://www.teferi.net/ps/problems/boj/15899
This code should be submitted with PyPy3. Python3 gets TLE.
"""
import sys
from teflib import fenwicktree
from teflib import tgraph
MOD = 1_000_000_007
def main():
    N, M, C = [int(x) for x in sys.stdin.readline().split()]
    colors = [int(x) for x in sys.stdin.readline().split()]
    tree = [[] for _ in range(N)]
    for _ in range(N - 1):
        u, v = [int(x) for x in sys.stdin.readline().split()]
        tree[u - 1].append(v - 1)
        tree[v - 1].append(u - 1)
    queries = [[] for _ in range(N)]
    for _ in range(M):
        v, c = [int(x) for x in sys.stdin.readline().split()]
        queries[v - 1].append(c)
    
    answer = 0
    color_set = fenwicktree.OrderStatisticTree(C)
    is_visited = [False] * N
    for node in tgraph.dfs(tree, 0, is_visited=is_visited, 
                           yields_on_leave=True):             
        if not is_visited[node]:
            for c in queries[node]:
                answer -= color_set.count_less_than(c + 1)            
            color_set.add(colors[node])
        else:
            for c in queries[node]:
                answer += color_set.count_less_than(c + 1)    
    print(answer % MOD)
if __name__ == '__main__':
    main()