#P9984. [USACO23DEC] A Graph Problem P

    ID: 11326 远端评测题 1000ms 256MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>线段树USACO并查集2023Kruskal 重构树O2优化哈希 hashing启发式合并

[USACO23DEC] A Graph Problem P

Problem Description

To broaden her math knowledge, Bessie took a graph theory course, but she got stuck on the following problem. Please help her.

You are given a connected undirected graph with nodes numbered 1N1 \dots N and edges numbered 1M1 \dots M (2N21052 \le N \le 2 \cdot 10^5, N1M4105N - 1 \le M \le 4 \cdot 10^5). The following procedure will be performed:

  1. Let the set S={v}S = \{v\}, and the variable h=0h = 0.
  2. While S<N|S| < N, repeat:
    1. Among the edges that have exactly one endpoint in SS, find the one with the smallest index, and denote its index by ee.
    2. Add the endpoint of edge ee that is not in SS into SS.
    3. Update hh to 10h+e10h + e.
  3. Return hmod(109+7)h \bmod (10^9 + 7).

Output all return values of this procedure.

Input Format

The first line contains NN and MM. The next MM lines each contain the endpoints (ae,be)(a_e, b_e) of the ee-th edge. It is guaranteed that the graph is connected and has no multiple edges.

Output Format

Output NN lines. The ii-th line should contain the return value of the procedure when starting from node ii.

3 2
1 2
2 3
12
12
21
5 6
1 2
3 4
2 4
2 3
2 5
1 5
1325
1325
2315
2315
5132
15 14
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
678925929
678925929
678862929
678787329
678709839
678632097
178554320
218476543
321398766
431520989
542453212
653475435
764507558
875540761
986574081

Hint

Sample Explanation 2

Consider starting the procedure from i=3i = 3. First, choose edge 22, so S={3,4}S = \{3, 4\} and h=2h = 2. Then, choose edge 33, so S={2,3,4}S = \{2, 3, 4\} and h=23h = 23. Next, choose edge 11, so S={1,2,3,4}S = \{1, 2, 3, 4\} and h=231h = 231. Finally, choose edge 55, so S={1,2,3,4,5}S = \{1, 2, 3, 4, 5\} and h=2315h = 2315. Therefore, the answer for i=3i = 3 is 23152315.

Sample Explanation 3

Make sure to take the answer modulo 109+710^9 + 7.

Testdata Properties

  • Testdata 44 satisfies N,M2000N, M \le 2000.
  • Testdata 55-66 satisfies N2000N \le 2000.
  • Testdata 77-1010 satisfies N10000N \le 10000.
  • Testdata 1111-1414 satisfies that for all edges, ae+1=bea_e + 1 = b_e.
  • Testdata 1515-2323 has no additional constraints.

Translated by ChatGPT 5