Optimal Connectivity
Practice
3.6 (19 votes)
Algorithms
Graphs
Life Cycle assessment
Medium
Lca
Problem
38% Success 4242 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

A company has a network of $$N$$ computers. There are $$N - 1$$ links in the network, each link connecting a pair of computers. Each link has a particular time lag in between the two computers. It is assured that all the computers are connected.
The engineers at X assume that the current status of the network is a bit slow and they want to improve it. So they experiment with \(Q\) computer pairs.
For each of these \(Q\) pairs, you are given new direct link's time lag between the pair of computers. If these two computers are directly connected using the new link and some old link is removed from the network, you need to check if the network become better.

A new network is better than older if the sum of all the time lags is strictly lesser in the new network than the old one and the network is still connected.
Note:  All queries are independent. That is, for each of the \(Q\) pairs, old network is the initial given network.

Input Format
The first line contains an integer $$N$$ denoting the total number of computers.
Each of the next $$N-1$$ lines contains a triplet of integers $$(u, v, w)$$ which states that the computer with the number $$u$$ is connected to the computer with the number $$v$$ and the time lag is of $$w$$ units.
The next line contains an integer $$Q$$.
Each of the next $$Q$$ lines contains three integers $$u, v$$ and $$w$$ which states that the new direct link between computer with the number $$u$$ and computer with the number $$v$$ has the time lag of $$w$$ units.

Output Format
For each query, you need to print YES  if you can improve the network by adding this link else print NO.

Input Constraints
\(1 \le N,Q \le 10^5 \\ 1 \le u,v \le N; \; u!=v \\ 1 \le w \le 10^9\)

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
72 votes
Tags:
Medium
Points:30
10 votes
Tags:
Ad-HocAlgorithmsCombinatoricsGraphsMedium普通
Points:30
17 votes
Tags:
AlgorithmsGraphsMedium