A Graph Problem
To improve her mathematical knowledge, Bessie has been taking a graph theory course and finds herself stumped by the following problem. Please help her!
You are given a connected, undirected graph with vertices labeled 1…N and edges labeled 1…M (2≤N≤2⋅105, N−1≤M≤4⋅105). For each vertex v in the graph, the following process is conducted:
- Let S={v} and h=0.
- While ∣S∣<N,
- Out of all edges with exactly one endpoint in S, let e be the edge with the minimum label.
- Add the endpoint of e not in S to S.
- Set h=10h+e.
- Return h(mod109+7).
Determine all the return values of this process.
Consider starting at i=3. First, we choose edge 2, after which S={3,4} and h=2. Second, we choose edge 3, after which S={2,3,4} and h=23. Third, we choose edge 1, after which S={1,2,3,4} and h=231. Finally, we choose edge 5, after which S={1,2,3,4,5} and h=2315. The answer for i=3 is therefore 2315.
Make sure to output the answers modulo 109+7.
SCORING:
- Input 4: N,M≤2000
- Inputs 5-6: N≤2000
- Inputs 7-10: N≤10000
- Inputs 11-14: ae+1=be for all e
- Inputs 15-23: No additional constraints.
Problem credits: Benjamin Qi
Source: USA Computing Olympiad.
Input Format
The first line contains N and M. Then follow M lines, the eth containing the endpoints (ae,be) of the eth edge (1≤ae<be≤N). It is guaranteed that these edges form a connected graph, and at most one edge connects each pair of vertices.
Output Format
Output N lines, where the ith line should contain the return value of the process starting at vertex i.