Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Kitty has a tree,T , consisting of n nodes where each node is uniquely labeled from 1 to n . Her friend Alex gave her

Kitty has a tree,T , consisting of n nodes where each node is uniquely labeled from 1 to n . Her friend Alex gave her q sets, where each set contains k distinct nodes. Kitty needs to calculate the following expression on each set:

image text in transcribed

where:

  • {u,v} denotes an unordered pair of nodes belonging to the set.
  • dist ( u,v ) denotes the number of edges on the unique (shortest) path between nodes u and v .

Given T and q sets of k distinct nodes, calculate the expression for each set. For each set of nodes, print the value of the expression modulo 10^9+7 on a new line.

Example

edges=[ [1,2] ,[1,3] ,[1,4] ,[3,5] [3,6], [3,7] ]

queries= [4,5,7]

The graph looks like this:

There are three pairs that can be created from the query set:[4,5] [4,7], [5,7] . The distance from 4 to 5 is 3, from 4 to 7 is 3, and from 5 to 7 is 2.

Now do the summation:

(4.5.dist(4,5)+4.7.dist(4,7)+5.7.dist(5,7)mod(10^9+7) => (4.5.3+4.7.3+5.7.2)mod(10^9+7)=>214

Input Format

The first line contains two space-separated integers, the respective values of n (the number of nodes in tree T) and q (the number of nodes in the query set). Each of the n-1 subsequent lines contains two space-separated integers, and , that describe an undirected edge between nodes a and b. The 2.q subsequent lines define each set over two lines in the following format:

  1. The first line contains an integer,k , the size of the set.
  2. The second line contains k space-separated integers, the set's elements.

Constraints

  • 1 n 2.10^5
  • 1 a , b n
  • 1 q 10^5
  • 1 ki 10^5
  • The sum of ki over all q does not exceed 2.10^5.
  • All elements in each set are distinct.

Subtasks

  • 1 n 2000 for 24% of the maximum score.
  • 1 n 5.10^4 for 45% of the maximum score.
  • 1 n 2.10^5 for 100% of the maximum score.

Output Format

Print q lines of output where each line i contains the expression for the i^th query, modulo 10^9+7.

Sample Input 0

7 3

1 2

1 3

1 4

3

5 3 6

3 7

2

2 4

1

5

3

2 4 5

Sample Output 0

16

0

106

---------write the correct code according to the given

#include

#include

#include

#include

#include

using namespace std;

int main() {

/* Enter your code here. Read input from STDIN. Print output to STDOUT */

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

4th Edition

0805360476, 978-0805360479

More Books

Students also viewed these Databases questions

Question

2. Define communication.

Answered: 1 week ago