To solve this problem, we need to find the longest distance between two devices in a tree where each device has a frequency, and messages can be transmitted between two devices only if the absolute difference in their frequencies is at most 1. The tree is given in the form of network_from and network_to arrays that define the edges.
Step-by-Step Reasoning:
-
Understanding the Tree Structure:
- We are given a tree with
network_nodes nodes.
- Each node has a frequency value from the
frequency array.
- An edge between two nodes is only valid if the absolute difference in their frequencies is ≤ 1.
-
Formulating the Problem:
- The task is to find the longest path length (in terms of the number of edges) where every consecutive pair of nodes along the path has a frequency difference of ≤ 1.
- If no such path exists, return 0.
-
Approach:
- Build the Subgraph: Create a subgraph where only edges with valid frequency differences (≤ 1) are included.
- Find the Longest Path (Diameter):
- For each connected component in the subgraph, find its diameter.
- The diameter of a tree is the longest path between any two nodes in that tree.
- Maintain the Maximum Diameter across all connected components.
-
BFS for Diameter Calculation:
- Use Breadth-First Search (BFS) twice for each connected component:
- First BFS: Start from an arbitrary node to find the farthest node
u.
- Second BFS: Start from node
u to find the farthest node v and determine the distance, which is the diameter of that component.
- Keep track of the maximum diameter found across all components.
-
Edge Cases:
- If no edges are valid, the output should be 0.
- If all edges are valid, the output should be the diameter of the original tree.
Implementation in C++:
Below is the C++ implementation of the solution based on the above reasoning:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
// Function to calculate the maximum distance
int calculateMax(int network_nodes, vector<int> network_from, vector<int> network_to, vector<int> frequency) {
// Adjust node numbering to 0-based
int n = network_nodes;
vector<vector<int>> adj(n, vector<int>());
// Build the adjacency list with allowed edges
for(int i=0;i<network_from.size();i++){
int u = network_from[i] -1;
int v = network_to[i] -1;
if(abs(frequency[u] - frequency[v]) <=1){
adj[u].push_back(v);
adj[v].push_back(u);
}
}
// Initialize visited array
vector<bool> visited(n, false);
// For BFS1: to track seen nodes in BFS1 using timestamps
vector<int> bfs1_seen_time(n, 0);
int bfs1_id =1;
int max_diameter =0;
// Loop through all nodes
for(int node=0; node<n; node++){
if(!visited[node]){
// BFS1: find farthest node from node
// Initialize
queue<pair<int, int>> q;
q.push({node, 0});
bfs1_seen_time[node] = bfs1_id;
int farthest_node = node;
int max_dist =0;
while(!q.empty()){
pair<int, int> current = q.front(); q.pop();
int u = current.first;
int dist = current.second;
if(dist > max_dist){
max_dist = dist;
farthest_node = u;
}
for(auto &v: adj[u]){
if(bfs1_seen_time[v] != bfs1_id){
q.push({v, dist+1});
bfs1_seen_time[v] = bfs1_id;
}
}
}
bfs1_id++;
// BFS2: from farthest_node, find the diameter and mark visited
queue<pair<int, int>> q2;
q2.push({farthest_node, 0});
visited[farthest_node] = true;
int diameter =0;
while(!q2.empty()){
pair<int, int> current = q2.front(); q2.pop();
int u = current.first;
int dist = current.second;
if(dist > diameter){
diameter = dist;
}
for(auto &v: adj[u]){
if(!visited[v]){
q2.push({v, dist+1});
visited[v] = true;
}
}
}
// Update the maximum diameter
max_diameter = max(max_diameter, diameter);
}
}
return max_diameter;
}
// Below is the main function to handle input and output
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int network_nodes, m;
cin >> network_nodes >> m;
vector<int> network_from(m), network_to(m);
for(int i=0;i<m;i++) cin>>network_from[i];
for(int i=0;i<m;i++) cin>>network_to[i];
vector<int> frequency(network_nodes);
for(int i=0;i<network_nodes;i++) cin>>frequency[i];
int result = calculateMax(network_nodes, network_from, network_to, frequency);
cout<<result;
}
Explanation of the Code:
-
Input Handling:
- The code reads the number of nodes, edges, and frequencies.
- It constructs an adjacency list only with edges that satisfy the frequency difference condition.
-
BFS Implementation:
- For each unvisited node, BFS1 is performed to find the farthest node in its connected component.
- BFS2 is then performed from this farthest node to determine the diameter of the component while marking all nodes in this component as visited.
-
Tracking Maximum Diameter:
- The maximum diameter across all connected components is maintained and returned as the result.
-
Edge Cases:
- If no two nodes can communicate (i.e., no valid edges), the result is correctly handled as 0.
This solution efficiently handles trees with up to (2 \times 10^5) nodes by ensuring that each node is visited exactly twice (once in BFS1 and once in BFS2), resulting in an overall time complexity of (O(n)).