#P15824. [JOI Open 2014] Factories

[JOI Open 2014] Factories

Description

In IOI Kingdom, there are NN cities numbered from 0 to N1N-1. These cities are connected by N1N-1 roads through which you can pass in both directions. You can travel from any city to any other city by passing through some of these roads.

In IOI Kingdom, there are many companies producing special components. Each company produces only one kind of components. No two companies produce the same kind of components. Each company has at least one factory. Each factory is built in one of the cities. More than one company may have factories in the same city.

Sometimes, a company CAC_A requires components of another company CBC_B (CACBC_A \ne C_B). In this case, they need to transport components from CBC_B to CAC_A. They may transport components from any of the factories of the company CBC_B to any of the factories of the company CAC_A. They need to choose factories appropriately to minimize the distance between factories.

Task

First, the number of cities and the information of roads in IOI Kingdom are given. Then, QQ queries are given. Each query is written in the following form: the company UjU_j having factories in cities Xj,0,,Xj,Sj1X_{j,0}, \dots, X_{j,S_j-1} requires components of the company VjV_j having factories in cities Yj,0,,Yj,Tj1Y_{j,0}, \dots, Y_{j,T_j-1}. Write a program which, for each query, returns the minimum distance to transport the components.

Implementation Details

You are to write a program which implements procedures to answer the queries. Your program should include the header file factories.h by #include "factories.h".

Your program should implement the following procedures.

  • void Init(int N, int A[], int B[], int D[])

    This procedure is called only once in the beginning. The parameter NN is the number of cities in IOI Kingdom. The parameters AA, BB and DD are arrays of length N1N-1. The elements A[i]A[i], B[i]B[i] and D[i]D[i] are three integers Ai,BiA_i, B_i and DiD_i (0iN20 \le i \le N-2) respectively. This means, for each ii (0iN20 \le i \le N-2), there is a road of length DiD_i connecting the city AiA_i and the city BiB_i.

  • long long Query(int S, int X[], int T, int Y[])

    This procedure is called for each of QQ queries. In the query jj, the parameters SS and TT are two integers SjS_j and TjT_j respectively. These are the numbers of cities where the companies Uj,VjU_j, V_j have factories respectively. The parameter XX is an array of length SjS_j. The company UjU_j has factories in cities X[0],X[1],,X[S1]X[0], X[1], \dots, X[S-1]. The parameter YY is an array of length TjT_j. The company VjV_j has factories in cities Y[0],Y[1],,Y[T1]Y[0], Y[1], \dots, Y[T-1]. This procedure should return the minimum distance to transport components from the company VjV_j to the company UjU_j.

Compilation and Test Run

You can download an archive file from the contest webpage which contains a sample grader to test your program. The archive file also contains a sample source file of your program.

A sample grader consists of one source file which is either grader.c or grader.cpp. For example, if your program is factories.c or factories.cpp, you run the following commands to compile your program.

  • C

    gcc -O2 -std=c11 -o grader grader.c factories.c -lm
    
  • C++

    g++ -O2 -std=c++11 -o grader grader.cpp factories.cpp
    

When the compilation succeeds, the executable file grader is generated.

Note that the actual grader is different from the sample grader. The sample grader will be executed as a single process, which will read input data from the standard input and write the results to the standard output.

Input Format

The sample grader reads the following data from the standard input.

  • The first line contains two space separated integers NN, QQ, which means there are NN cities in IOI Kingdom, and QQ queries are given to your program.
  • The (i+1)(i+1)-st line (0iN20 \le i \le N-2) of the following (N1)(N-1) lines contains three space separated integers AiA_i, BiB_i, DiD_i, which means there is a road of length DiD_i connecting the city AiA_i and the city BiB_i.
  • The information of the jj-th query is written from the (3j+1)(3j+1)-st line to the (3j+3)(3j+3)-rd line (0jQ10 \le j \le Q-1) of the following 3Q3Q lines.
    • The (3j+1)(3j+1)-st line (0jQ10 \le j \le Q-1) contains two space separated integers SjS_j and TjT_j (1SjN11 \le S_j \le N-1, 1TjN11 \le T_j \le N-1), which means the companies UjU_j and VjV_j have factories in SjS_j and TjT_j cities respectively.
    • The (3j+2)(3j+2)-nd line (0jQ10 \le j \le Q-1) contains SjS_j space separated integers Xj,0,,Xj,Sj1X_{j,0}, \dots, X_{j,S_j-1}, which means the company UjU_j has factories in the cities Xj,0,,Xj,Sj1X_{j,0}, \dots, X_{j,S_j-1}.
    • The (3j+3)(3j+3)-rd line (0jQ10 \le j \le Q-1) contains TjT_j space separated integers Yj,0,,Yj,Tj1Y_{j,0}, \dots, Y_{j,T_j-1}, which means the company VjV_j has factories in the cities Yj,0,,Yj,Tj1Y_{j,0}, \dots, Y_{j,T_j-1}.

Output Format

When the program terminates successfully, the sample grader writes to the standard output the values returned by Query one per line.

7 3
0 1 4
1 2 4
2 3 5
2 4 6
4 5 5
1 6 3
2 2
0 6
3 4
3 2
0 1 3
4 6
1 1
2
5
12
3
11

Hint

Sample 1 Explanation

These are sample input and sample output of the sample grader.

  • In the query 0, the company U0U_0 has factories in the cities 0, 6, and the company V0V_0 has factories in the cities 3, 4. The distance from the factory of the company V0V_0 in the city 3 to the factory of the company U0U_0 in the city 6 is minimum. The minimum distance is 12.
  • In the query 1, the company U1U_1 has factories in the cities 0, 1, 3, and the company V1V_1 has factories in the cities 4, 6. The distance from the factory of the company V1V_1 in the city 6 to the factory of the company U1U_1 in the city 1 is minimum. The minimum distance is 3.
  • In the query 2, the company U2U_2 has factories in the city 2, and the company V2V_2 has factories in the city 5. The distance from the factory of the company V2V_2 in the city 5 to the factory of the company U2U_2 in the city 2 is minimum. The minimum distance is 11.

Constraints

All input data satisfy the following conditions.

  • 2N5000002 \le N \le 500000.
  • 1Q1000001 \le Q \le 100000.
  • 0AiN10 \le A_i \le N-1 (0iN20 \le i \le N-2).
  • 0BiN10 \le B_i \le N-1 (0iN20 \le i \le N-2).
  • 1Di1000000001 \le D_i \le 100000000 (0iN20 \le i \le N-2).
  • AiBiA_i \ne B_i (1iN21 \le i \le N-2).
  • You can travel from any city to any other city through some of these roads.
  • 1SjN11 \le S_j \le N-1 (0jQ10 \le j \le Q-1).
  • 0Xj,kN10 \le X_{j,k} \le N-1 (0jQ10 \le j \le Q-1, 0kSj10 \le k \le S_j-1).
  • 1TjN11 \le T_j \le N-1 (0jQ10 \le j \le Q-1).
  • 0Yj,kN10 \le Y_{j,k} \le N-1 (0jQ10 \le j \le Q-1, 0kTj10 \le k \le T_j-1).
  • $X_{j,0}, X_{j,1}, \dots, X_{j,S_j-1}, Y_{j,0}, Y_{j,1}, \dots, Y_{j,T_j-1}$ are different from each other (0jQ10 \le j \le Q-1).
  • S0+S1++SQ11000000S_0 + S_1 + \cdots + S_{Q-1} \le 1000000.
  • T0+T1++TQ11000000T_0 + T_1 + \cdots + T_{Q-1} \le 1000000.

Subtask

Subtask 1 [15 points]

The following conditions are satisfied.

  • N5000N \le 5000.
  • Q5000Q \le 5000.

Subtask 2 [18 points]

The following conditions are satisfied.

  • Si10S_i \le 10 (0iQ10 \le i \le Q-1).
  • Ti10T_i \le 10 (0iQ10 \le i \le Q-1).

Subtask 3 [67 points]

There are no additional constraints.