Advertisement
Josif_tepe

Untitled

Oct 19th, 2023
680
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. using namespace std;
  5. const int maxn = 2e5 + 10;
  6. int n;
  7. vector<int> graph[maxn];
  8. int dist_A[maxn], dist_B[maxn];
  9. pair<int, int> find_diameter() {
  10.     queue<int> q;
  11.     q.push(0);
  12.     q.push(0);
  13.     vector<bool> visited(n, false);
  14.     visited[0] = true;
  15.     int max_dist = 0, max_node1 = -1;
  16.     while(!q.empty()) {
  17.         int node = q.front();
  18.         q.pop();
  19.         int dist = q.front();
  20.         q.pop();
  21.         if(dist > max_dist) {
  22.             max_dist = dist;
  23.             max_node1 = node;
  24.         }
  25.         for(int neighbour : graph[node]) {
  26.             if(!visited[neighbour]) {
  27.                 visited[neighbour] = true;
  28.                 q.push(neighbour);
  29.                 q.push(dist + 1);
  30.             }
  31.         }
  32.     }
  33.     for(int i = 0; i < n; i++) {
  34.         visited[i] = false;
  35.     }
  36.     max_dist = 0;
  37.     int max_node2 = -1;
  38.     q.push(max_node1);
  39.     q.push(0);
  40.    
  41.     visited[max_node1] = true;
  42.     while(!q.empty()) {
  43.         int node = q.front();
  44.         q.pop();
  45.         int dist = q.front();
  46.         q.pop();
  47.        
  48.         if(dist > max_dist) {
  49.             max_dist = dist;
  50.             max_node2 = node;
  51.         }
  52.         for(int neighbour : graph[node]) {
  53.             if(!visited[neighbour]) {
  54.                 visited[neighbour] = true;
  55.                 q.push(neighbour);
  56.                 q.push(dist + 1);
  57.             }
  58.         }
  59.     }
  60.    
  61.     return make_pair(max_node1, max_node2);
  62. }
  63. int main()
  64. {
  65.     ios_base::sync_with_stdio(false);
  66.     cin >> n;
  67.     if(n == 1) {
  68.         cout << 0 << endl;
  69.         return 0;
  70.     }
  71.     for(int i = 0; i < n - 1; i++) {
  72.         int a, b;
  73.         cin >> a >> b;
  74.         a--;
  75.         b--;
  76.         graph[a].push_back(b);
  77.         graph[b].push_back(a);
  78.     }
  79.     pair<int, int> diameter = find_diameter();
  80.    
  81.     queue<int> q;
  82.     q.push(diameter.first);
  83.     q.push(0);
  84.     vector<bool> visited(n, false);
  85.     visited[diameter.first] = true;
  86.    
  87.     while(!q.empty()) {
  88.         int node = q.front();
  89.         q.pop();
  90.         int dist = q.front();
  91.         q.pop();
  92.         dist_A[node] = dist;
  93.        
  94.         for(int neighbour : graph[node]) {
  95.             if(!visited[neighbour]) {
  96.                 visited[neighbour] = true;
  97.                 q.push(neighbour);
  98.                 q.push(dist + 1);
  99.             }
  100.         }
  101.     }
  102.     for(int i = 0; i < n; i++) {
  103.         visited[i] = false;
  104.     }
  105.     q.push(diameter.second);
  106.     q.push(0);
  107.     visited[diameter.second] = true;
  108.     while(!q.empty()) {
  109.         int node = q.front();
  110.         q.pop();
  111.         int dist = q.front();
  112.         q.pop();
  113.         dist_B[node] = dist;
  114.        
  115.         for(int neighbour : graph[node]) {
  116.             if(!visited[neighbour]) {
  117.                 visited[neighbour] = true;
  118.                 q.push(neighbour);
  119.                 q.push(dist + 1);
  120.             }
  121.         }
  122.     }
  123.     for(int i = 0; i < n; i++) {
  124.         cout << max(dist_A[i], dist_B[i]) << " ";
  125.     }
  126.     return 0;
  127. }
  128.  
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement