1 && 3 Graph

Time limit4sMemory limit1024 MB

Summary
Answer many shortest-path queries on a special connected graph where fewer than 3 vertices have degree at least 3, exploiting its path/cycle-like structure for efficiency.
Level

Medium6 of 10

Topics
Graph, Shortest path, Implementation
Solved
No attempts yet

Problem

Sehun and Chanwoo have solved many graph problems and developed their own ideas about what makes a good one.

  • Sehun thinks the input graph should be ordinary enough that it needs no special-case handling. In this problem, an ordinary graph is an undirected simple connected graph: there are no duplicate edges, every vertex is connected, and every edge joins two distinct vertices.
  • Chanwoo thinks a graph becomes messy when it has too many high-degree vertices. More precisely, the number of vertices whose degree is at least 33 must be less than 33.

A graph satisfying both conditions is called a 1 && 3 graph. You are given a 1 && 3 graph with VV vertices and EE edges. Write a program that processes QQ queries asking for the shortest distance between two vertices.

Input

The first line contains three integers VV, EE, and QQ: the number of vertices, the number of edges, and the number of queries. (2≤V≤500000;V−1≤E≤500000;1≤Q≤200000)(2 \le V \le 500000; V-1 \le E \le 500000; 1 \le Q \le 200000)

Each of the next EE lines contains three integers xx, yy, and cc, meaning that there is an edge of weight cc between vertices xx and yy. (1≤x,y≤V;1≤c≤109;x≠y)(1 \le x,y \le V; 1 \le c \le 10^9; x \ne y)

Each of the next QQ lines contains two integers aa and bb. This query asks for the shortest distance between vertices aa and bb. (1≤a,b≤V)(1 \le a,b \le V)

All input values are integers, and the given graph is a 1 && 3 graph.

Output

Print QQ lines. For each query, print its answer on its own line, in the same order as the input.

Examples1

  1. Example 1

    Input
    15 18 8
    1 2 5
    2 3 1
    3 4 1
    4 1 5
    1 5 1
    5 6 1
    1 7 1
    7 8 100
    8 9 1
    9 10 1
    1 10 1
    1 15 2
    15 10 100
    10 14 1
    14 13 5
    13 12 5
    12 11 1
    11 10 1
    4 12
    12 14
    2 4
    3 6
    1 10
    7 9
    8 9
    10 15
    
    Expected output
    8
    3
    2
    8
    1
    3
    1
    3