Stern-Bocot Graph Sequence
Description:
Problem statement
You have an undirected graph G with N vertices and M edges. Each side of G is numbered from 1 to M. The weights of the edges of G are terms in a Stern-Bocot sequence whose denominator is the number of the edge. A Stern-Bocot sequence is a sequence defined as follows:
- S1=0/1
- S2=1/1
- S2n=Sn/Sn+1
- S2n+1=Sn+1/Sn
For any two vertices u and v of G, find the minimum sum of the weights of the paths connecting u and v. The sum of the weights of a path is the sum of the weights of the sides in the path. If u and v are not connected, output -1.
Constraint
- 2≤N≤10^5
- 1≤M≤4×10^3
- 2≤Q<10^5
- 1≤ui,vi≤N
- ui≠vi
- G is a simple graph
- All inputs are integers
Input
The input is given from the standard input in the following format:
N M Q
u1 v1 w1
u2 v2 w2
⋮
uM vM wM
a1 b1
a2 b2
⋮
aQ bQ
Output
Output Q lines. In line i, output the minimum sum of the weights of the paths connecting ai and bi. If ai and bi are not connected, output -1. Note that all output is rational.
Test Case
Test Case 1:
4 5 3
1 2 1/2
2 3 1/3
3 4 1/4
4 1 1/5
1 3 1/6
1 4
2 3
3 1
Output 1:
2/1
2/1
2/1
Similar Kata:
Stats:
Created | Feb 4, 2024 |
Warriors Trained | 4 |
Total Skips | 0 |
Total Code Submissions | 14 |
Total Times Completed | 3 |
Python Completions | 3 |
Total Stars | 1 |
% of votes with a positive feedback rating | 50% of 1 |
Total "Very Satisfied" Votes | 0 |
Total "Somewhat Satisfied" Votes | 1 |
Total "Not Satisfied" Votes | 0 |
Total Rank Assessments | 1 |
Average Assessed Rank | 4 kyu |
Highest Assessed Rank | 4 kyu |
Lowest Assessed Rank | 4 kyu |