Dual Distance DUALDIST
Given a tree with N nodes, answer Q queries of the following type:
a,b (where a≠b) which asks you to calculate ∑Ni=1min(dist(i,a),dist(i,b)) where dist(x,y) is the number of edges on the shortest path between the nodes x and y in the tree.
Note: The input and output of this problem are large, so prefer using fast input/output methods.
Input
The first line contains an integer T, the number of test cases. Then the test cases follow.
Each test case contains N+Q lines of input.
The first line contains two integers N and Q.
The next N−1 lines each contains two integers u, v representing an edge between nodes u and v in the tree.
The next Q lines each contains two integers a, b, the nodes considered for the respective query.
Output
For each query of each test case, output the answer in a new line.
Constraints
1≤T≤8
2≤N≤105
1≤Q≤105
a≠b
The sum of N over all test cases is at most 5⋅105.
The sum of Q over all test cases is at most 5⋅105.
Its guaranteed that the given input is a valid tree.
Subtasks
Subtask #1 (20 points):
2≤N≤103
1≤Q≤103
The sum of N over all test cases is at most 5⋅103.
The sum of Q over all test cases is at most 5⋅103.
Time limit: 0.5 sec.
Subtask #2 (80 points):
original constraints
Time limit: 2 sec.
Sample Input
2
4 2
1 2
2 3
3 4
1 2
3 1
7 1
1 2
1 3
2 4
2 5
3 6
3 7
2 6
Sample Output
3
2
6
Explanation:
Test Case 1:
Query 1: Answer = min(dist(1,1),dist(1,2)) + min(dist(2,1),dist(2,2)) + min(dist(3,1),dist(3,2)) + min(dist(4,1),dist(4,2)) = min(0,1) + min(1,0) + min(2,1) + min(3,2) = 0+0+1+2 = 3.
Query 2: Answer = min(dist(1,1),dist(1,3)) + min(dist(2,1),dist(2,3)) + min(dist(3,1),dist(3,3)) + min(dist(4,1),dist(4,3)) = min(0,2) + min(1,1) + min(2,0) + min(3,1) = 0+1+0+1 = 2.
Program:
#include <iostream>
#include<vector>
#include<queue>
#include <climits>
#include <cstdint>
#include <iterator>
#include <algorithm>
using namespace std;
void bfs(int a,int b,vector<int> graph[],int vertices){
int seen[vertices];
int seen2[vertices];
for(int i=0;i<vertices;i++){
seen[i]=0;
seen2[i]=0;
}
queue<int> queue;
int distance[vertices];
int distance2[vertices];
for(int i=0;i<vertices;i++){
distance[i] = INT_MAX;
distance2[i] = INT_MAX;
}
seen[a]=1;
seen2[b]=1;
queue.push(a);
distance[a]=0;
distance2[b]=0;
while(!queue.empty()){
int pop = queue.front();
queue.pop();
for(int i=0;i<graph[pop].size();i++){
int ver = graph[pop][i];
if(seen[ver]==1) continue;
seen[ver]=1;
queue.push(ver);
distance[ver] = distance[pop]+1;
}
}
queue.push(b);
while(!queue.empty()){
int pop = queue.front();
queue.pop();
for(int i=0;i<graph[pop].size();i++){
int ver = graph[pop][i];
if(seen2[ver]==1) continue;
seen2[ver]=1;
queue.push(ver);
distance2[ver] = distance2[pop]+1;
}
}
int dist = 0;
for(int k=0;k<vertices;k++){
// cout<<distance[k]<<"\n";
// cout<<distance2[k]<<"\n";
dist+=min(distance[k],distance2[k]);
}
cout<<dist<<"\n";
}
int main() {
// your code goes here
int t;
cin>>t;
for(int i=0;i<t;i++){
int n,q;
cin>>n;
cin>>q;
vector<int> graph[n];
for(int j=0;j<n-1;j++){
int a,b;
cin>>a;
cin>>b;
graph[a-1].push_back(b-1);
graph[b-1].push_back(a-1);
}
for(int j=0;j<q;j++){
int a,b;
cin>>a;
cin>>b;
bfs(a-1,b-1,graph,n);
}
}
return 0;
}
June Long Challenge 2021 Solutions
- Maximum Frequent Subarray Sum solution codechef
- Dual Distance solution codechef
- Optimal Xor Set solution codechef
- Minimum Subtree Cover solution codechef
- Minimum Dual Area solution codechef
- Bitwise Tuples solution codechef
- Shortest Route solution codechef
- Bella ciao solution codechef
- Summer Heat solution codechef
March Long Challenge 2021 Solutions
- An Interesting Sequence ISS SOLUTION
- Tree House THOUSES SOLUTION
- Valid Paths VPATH SOLUTION
- Modular Equation MODEQ SOLUTION
- Tic Tac Toe TCTCTOE SOLUTION
- Xor Equality XOREQUAL SOLUTION
- Golf LKDNGOLF SOLUTION
- Solubility SOLBLTY SOLUTION
April Long Challenge 2021 Solutions
- Chef and Dice SDICE Solution
- Worthy Matrix KAVGMAT Solution
- Binary String MEX MEXSTR Solution
- Boolean Game BOOLGAME Solution
- Tree Permutations TREEPERM Solution
- Destroy the EMP Chip CHAOSEMP Solution
- Chef and Pair Flips PAIRFLIP Solution
- String Power STRPOW Solution
- Brahma and Shiva SHRINES Solution
- Water Sort Puzzle (Challenge) WTRSORT Solution
- World Record BOLT Solution
- Strong Language SSCRIPT Solution
- Valid Pair SOCKS1 Solution
Codechef Long Challenge Solutions
February Long Challenge 2021
- 1. Frog Sort Solution Codechef
- 2. Chef and Meetings Solution Codechef
- 3. Maximise Function Solution Codechef
- 4. Highest Divisor Solution Codechef
- 5. Cut the Cake Challenge Solution Codechef
- 6. Dream and the Multiverse Solution Codechef
- 7. Cell Shell Solution Codechef
- 8. Multiple Games Solution Codechef
- 9. Another Tree with Number Theory Solution Codechef
- 10. XOR Sums Solution Codechef
- 11. Prime Game Solution CodeChef
- 12. Team Name Solution Codechef
January Long Challenge 2021
- Chef and Division 3 DIVTHREE SOLUTION Code Chef
- Encoded String DECODEIT SOLUTION Code Chef
- Point Of Impact BILLRD SOLUTION Code Chef
- Fair Elections FAIRELCT SOLUTION Code Chef
- Watching CPL WIPL SOLUTION Code Chef
- Chef and Ants ANTSCHEF SOLUTION Code Chef
- Blackjack BLKJK SOLUTION Code Chef
- And-Or Game ORAND SOLUTION Code Chef
- Stack-Queue Sort (Challenge) SQSORT SOLUTION Code Chef
- Expected Number of SCCs RCTEXSCC SOLUTION Code Chef
- Curious Matrix CURMAT SOLUTION Code Chef
- Cool Subsets COOLSBST SOLUTION Code Chef
- Sequence Creation ARCRT SOLUTION Code Chef
- Greedy Students GRDSTD SOLUTION Code Chef
November Challenge 2020 SOLUTION CodeChef
- Ada and Dishes SOLUTION ADADISH
- Iron Magnet and Wall SOLUTION FEMA2
- Magical Candy Store SOLUTION CNDYGAME
- Unusual Queries SOLUTION UNSQUERS
- Red-Black Boolean Expression SOLUTION RB2CNF
- Chef and the Combination Lock SOLUTION CHEFSSM
- Scalar Product Tree SOLUTION SCALSUM
- Connect on a Grid (Challenge) SOLUTION CONGRID
October Lunchtime 2020 CodeChef SOLUTIONS
- AND Plus OR SOLUTION ANDOR
- Chef and Subtree MEXs SOLUTION SUBMEXS
- Chef Likes Good Sequences SOLUTION GSUB
- Cute Chef Gift SOLUTION COPAR
- Chef Is Just Throwing Random Words SOLUTION SSO
- Counting Spaghetti SOLUTION CDSUMS
- Chef and Edge Flipping SOLUTION EFLIP
RELATED :
- Top Best Keylogger in Python 3 Make One
- What is Hacking?
- Secrets of the Deep Dark Web
- CODECHEF September Lunchtime 2020 SOLUTIONS
- August Lunchtime 2020 SOLUTIONS
Related :
- A. Shandom Ruffle SOLUTION
- B. Pear TreaP SOLUTION
- C. Sneetches and Speeches 3 SOLUTION
- D. The Grim Treaper SOLUTION
- Y. Sneetches and Speeches 1 SOLUTION
- Z. Trick or Treap SOLUTION
- A. Floor Number SOLUTION CODE FORCES
- B. Symmetric Matrix SOLUTION CODE FORCES
- C. Increase and Copy SOLUTION CODE FORCES
- D. Non-zero Segments SOLUTION CODE FORCES
- E. Rock, Paper, Scissors SOLUTION CODE FORCES
- F. Number of Subsequences SOLUTION CODE FORCES
Related :
- Chef and Easy Queries SOLUTIONS CHEFEZQ
- Covid Run SOLUTIONS CVDRUN OCTOBER CHALLENGE
- Positive AND SOLUTIONS POSAND
- Replace for X SOLUTIONS REPLESX
- Village Road Network SOLUTIONS VILLNET
- Random Knapsack SOLUTIONS RANDKNAP
- D-Dimensional MST SOLUTIONS DDIMMST
- Compress all Subsegments SOLUTIONS SEGCOMPR
- Adding Squares SOLUTIONS ADDSQURE
- Inversions SOLUTIONS INVSMOD2 OCOTBER CHALLENGE
- Rooted Minimum Spanning Tree SOLUTIONS ROOTMST