Cowherd and Weaver Girl
InterviewTime limit1sMemory limit1024 MB
Given two weighted trees, pick one vertex from each and join them with a unit edge to minimize the sum of distances over all cross pairs.
Problem
Cowherd lives in a weighted undirected tree with vertices, and Weaver Girl lives in a weighted undirected tree with vertices.
The two live in different trees, so they cannot meet. To help the two lovers, the Jade Emperor builds a magpie bridge every year on July 7 so they can meet.
The magpie bridge is an edge of length . The Jade Emperor connects it so that the sum of distances between all vertices of and all vertices of is minimized, which makes it easy for the two to meet.
Tell them which vertices the bridge connects on July 7.
Input
The first line contains , the number of vertices in ().
The next lines each describe an edge of in the form a b c . This means the distance between vertex and vertex of is .
The next line contains , the number of vertices in ().
The next lines each describe an edge of in the form a b c . This means the distance between vertex and vertex of is .
All input values are integers.
Output
The first line prints the vertex of and the vertex of that the bridge connects, separated by a space. If several answers exist, print any of them. The second line prints the sum of distances between all vertices of and all vertices of after the bridge is built.
Hint
The sum of distances between all vertices of and all vertices of is defined as follows.