Little Shino and Path Divisor
Practice
4 (10 votes)
Approved
Data structures
Disjoint set
Medium
Problem
29% Success 1077 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

There is no partial marking in the problem.

Given a weighted undirected tree of N nodes and Q queries. Each query contains an integer D. For each query, find the number of paths in the given tree such that all the edges in the path are divisible by D.

Input Format:
First line contains two space separated integers, N and Q \((1 \le N, Q \le 10^5)\), denoting number of nodes in the tree and number of queries. Next \(N-1\) line contains three space separated integers, X, Y and W \((1 \le X, Y \le N)\) \((1 \le W \le 10^5)\), denoting that there is an edge between X and Y of weight W. Next Q lines contains one integer each, D \((1 \le D \le 10^5)\).

Output Format:
For each query, print number of paths such that all the edges in the path are divisible by D in new line.

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
8 votes
Tags:
Data StructuresDisjoint setMediumString Manipulation
Points:30
8 votes
Tags:
AlgorithmsApprovedData StructuresDepth First SearchDisjoint setMediumOpen
Points:30
4 votes
Tags:
Data StructuresDisjoint setMediumQueue