Advertisement
Josif_tepe

Untitled

Nov 23rd, 2023
808
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <queue>
  6. //#include <bits/stdc++.h>
  7.  
  8. using namespace std;
  9. const int INF = 2e9;
  10. const int maxn = 3e5 + 10;
  11. typedef long long ll;
  12. int n;
  13. ll k;
  14. vector<int> graph[maxn];
  15. int a[maxn];
  16. void brute_force() {
  17.     int S = 0;
  18.  
  19.     for(int i = 0; i < k; i++) {
  20.         vector<bool> visited(n + 1, false);
  21.         visited[S] = true;
  22.         queue<int> q;
  23.         q.push(S);
  24.         q.push(0);
  25.         int max_dist = -INF;
  26.         int next_node = -1;
  27.         while(!q.empty()) {
  28.             int node = q.front();
  29.             q.pop();
  30.             int dist = q.front();
  31.             q.pop();
  32.             if(a[node] - dist > max_dist and S != node) {
  33.                 max_dist = a[node] - dist;
  34.                 next_node = node;
  35.             }
  36.             if(a[node] - dist == max_dist and S != node) {
  37.                 next_node = min(next_node, node);
  38.             }
  39.             for(int neighbour : graph[node]) {
  40.                 if(!visited[neighbour]) {
  41.                     visited[neighbour] = true;
  42.                     q.push(neighbour);
  43.                     q.push(dist + 1);
  44.                 }
  45.             }
  46.         }
  47.         S = next_node;
  48.        
  49.     }
  50.     cout << S + 1 << endl;
  51. }
  52. void solve_subtask2() {
  53.     int S = 0;
  54.     vector<int> cnt(n + 1, 0);
  55.     vector<int> cycle;
  56.     bool cycle_begin = false;
  57.     int till_cycle = 0;
  58.     for(int i = 0; i < 10000; i++) {
  59.        
  60.         vector<bool> visited(n + 1, false);
  61.         visited[S] = true;
  62.         int next_node = -1, max_dist = -INF;
  63.        
  64.         queue<int> q;
  65.         q.push(S);
  66.         q.push(0);
  67.         cnt[S]++;
  68.         if(cnt[S] == 2) {
  69.             cycle_begin = true;
  70. //            cout << S + 1 << endl;
  71.         }
  72.        
  73.         if(cycle_begin) {
  74.             cycle.push_back(S);
  75.         }
  76.         else {
  77.             till_cycle++;
  78.         }
  79.         while(!q.empty()) {
  80.             int node = q.front();
  81.             q.pop();
  82.             int dist = q.front();
  83.             q.pop();
  84.            
  85.             if(a[node] - dist > max_dist and S != node) {
  86.                 max_dist = a[node] - dist;
  87.                 next_node = node;
  88.             }
  89.             if(a[node] - dist == max_dist and S != node) {
  90.                 next_node = min(next_node, node);
  91.             }
  92.             for(int neighbour : graph[node]) {
  93.                 if(!visited[neighbour]) {
  94.                     visited[neighbour] = true;
  95.                     q.push(neighbour);
  96.                     q.push(dist + 1);
  97.                 }
  98.             }
  99.         }
  100.         S = next_node;
  101.         if(cycle_begin and S == cycle[0]) {
  102. //            for(int x : cycle) {
  103. //                cout << x + 1 << " " ;
  104. //            }
  105.             break;
  106.         }
  107.     }
  108. //    cout << endl;
  109. //    cout << "till: " << till_cycle - cycle.size() - 1<< endl;
  110. //    cout << cycle.size() << endl;
  111.     till_cycle =  till_cycle - cycle.size() - 1;
  112.     k -= till_cycle;
  113. //    cout << k << endl;
  114.    
  115.     cout << cycle[(k + 1) % (ll) cycle.size()] + 1 << endl;
  116. }
  117. int main()
  118. {
  119.     ios::sync_with_stdio(false);
  120.     cin >> n >> k;
  121.     for(int i = 0; i < n; i++) {
  122.         cin >> a[i];
  123.     }
  124.    
  125.     for(int i = 1; i < n; i++) {
  126.         int a, b;
  127.         cin >> a >> b;
  128.         a--; b--;
  129.         graph[a].push_back(b);
  130.         graph[b].push_back(a);
  131.     }
  132.    
  133.     if(n <= 1000 and k <= 1000) {
  134.         brute_force();
  135.         return 0;
  136.     }
  137.    
  138.     if(n <= 1000) {
  139.         solve_subtask2();
  140.         return 0;
  141.     }
  142.     return 0;
  143. }
  144.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement