问题 J: Is it a tree?

内存限制:256 MB 时间限制:3 S 标准输入输出
题目类型:传统 评测方式:文本比较 上传者:
提交:0 通过:0

题目描述

链接:https://ac.nowcoder.com/acm/contest/57361/J
来源:牛客网
Students from Antarctic Penguin Language School are counting connected components.
You are given a tree and a cactus with nnn vertices. The vertices is numbered from 111 to nnn.
We define f(i,j)f(i,j)f(i,j) as if we only keep those vertices in the cactus whose number appears on the path from iii to jjj in the tree and the edges between them, how many connected components will be left.
Vertex 111 is the root. For all 1≤x≤n1\leq x\leq n1xn, you need to calculate:

∑i∈Sx∑j∈Sx,i≤jf(i,j)\sum\limits_{i\in S_x}\sum\limits_{j\in S_x,i \leq j}f(i,j)iSxjSx,ijf(i,j)

in which SxS_xSx means the set of vertices in the subtree of vertex xxx.
A cactus is a simple undirected graph whose nodes are in at most one simple cycle.

输入格式

The first line contains two integers, n,m(1≤n≤5×10^5,1≤m≤10^6)n,m(1\leq n\leq 5\times 10^5,1\leq m\leq 10^6)n,m(1n5×10^5,1m10^6) --- the number of nodes and the number of edges in the cactus.
The next n−1n-1n1 lines, each line contains two integers, describing an edge in the tree.
The next mmm lines, each line contains two integers, describing an edge in the cactus.

输出格式

Output nnn lines, the iii-th line contains an integer, the answer to x=ix=ix=i.

输入样例 复制

6 7
1 2
1 3
2 4
2 5
3 6
1 2
2 3
3 1
1 4
4 5
5 6
6 4

输出样例 复制

30
9
4
1
1
1