OtherOA #1
Build Monuments
HardJudge-ready
Topics (AI-labelled)Bits & MathTree Traversal
Assign each node of a tree one of k types, so that cities that are directly connected or share a neighbor do not have the same type. This site adds the input protocol and constraints.
Input
The first line contains k n (1≤k≤10^9, 1≤n≤10^5); each of the next n−1 lines is an undirected edge u v, with cities numbered 1..n. The input is guaranteed to form a tree.
Output
Output the number of assignments in which any two cities at distance at most 2 have different types, modulo 1,000,000,007. This site adds this standard input protocol and the constraints.
Example 1
Input:
3 3
1 2
2 3
Output:
6
Example 2
Input:
2 1
Output:
2
Example 3
Input:
4 4
1 2
1 3
1 4
Output:
24
Editorial
Loading solution…