Advertisement
Guest User

2034 Caravans

a guest
Jun 28th, 2024
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.50 KB | Software | 0 0
  1. /*
  2. Author : MadhavCoding
  3. */
  4.  
  5. #include <bits/stdc++.h>
  6. #include <ext/pb_ds/assoc_container.hpp>
  7. #include <ext/pb_ds/tree_policy.hpp>
  8.  
  9. #define in(n) int n; cin>>n;
  10. #define inll(n) ll n; cin>>n;
  11. #define in2(x, y) int x, y; cin>>x>>y;
  12. #define inll2(x, y) ll x, y; cin>>x>>y;
  13. #define ins(s) string s; cin>>s;
  14. #define vi vector<int>
  15. #define vll vector<ll>
  16. #define vpi vector<pair<int, int>>
  17. #define vpl vector<pair<ll, ll>>
  18. #define pi pair<int, int>
  19. #define pll pair<ll, ll>
  20. #define ll long long
  21. #define ld long double
  22. #define pb push_back
  23. #define pbb pop_back()
  24. #define endl "\n"
  25. #define N 1e5
  26. #define MOD 1e9+7
  27.  
  28. // debugging statements
  29. #ifndef ONLINE_JUDGE
  30. #define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
  31. #else
  32. #define debug(x)
  33. #endif
  34.  
  35. using namespace std;
  36. using namespace __gnu_pbds;
  37. typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
  38. typedef tree<ll, ll, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_map;
  39.  
  40. template<class T> void _print(T t) {cerr << t;}
  41. template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.first); cerr << ","; _print(p.second); cerr << "}";}
  42. template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
  43. template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
  44. template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
  45. template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
  46.  
  47. void distance1(vector<vector<int>>& graph, vector<int>& vis, vector<int>& dist, int a)
  48. {
  49.     vis[a] = 1;
  50.     queue<int> q;
  51.     q.push(a);
  52.     while(!q.empty())
  53.     {
  54.         int u = q.front(); q.pop();
  55.         for(int i = 0; i < graph[u].size(); i++)
  56.         {
  57.             int v = graph[u][i];
  58.             if(!vis[v])
  59.             {
  60.                 vis[v] = 1;
  61.                 q.push(v);
  62.                 dist[v] = dist[u]+1;
  63.             }
  64.         }
  65.     }
  66. }
  67.  
  68. int main(int argc, char const *argv[])
  69. {
  70.  
  71. #ifndef ONLINE_JUDGE
  72.     freopen("error.txt", "w", stderr);
  73. #endif
  74.  
  75. ios::sync_with_stdio(0);
  76. cin.tie(0);
  77.  
  78. int t = 1;
  79. while(t--)
  80. {
  81.     in2(v, e);
  82.     vector graph(v, vi());
  83.     for (int i = 0; i < e; i++)
  84.     {
  85.         in2(a,b); a--; b--;
  86.         graph[a].pb(b);
  87.         graph[b].pb(a);
  88.     }
  89.     debug(graph);
  90.  
  91.     in2(s,f); s--; f--; in(r); r--;
  92.    
  93.     vi vis(v);
  94.    
  95.     vi distr(v, INT_MAX); distr[r] = 0;
  96.     distance1(graph, vis, distr, r);
  97.     debug(distr);
  98.     for(int i = 0; i < v; i++) vis[i] = 0;
  99.  
  100.     vi dists(v, INT_MAX); dists[s] = 0;
  101.     distance1(graph, vis, dists, s);
  102.     debug(dists);
  103.     for(int i = 0; i < v; i++) vis[i] = 0;
  104.  
  105.     int mini = dists[f]; debug(mini);
  106.  
  107.     int maxd = 0;
  108.     for(int i = 0; i < v; i++) maxd = max(maxd, distr[i]);
  109.     debug(maxd);
  110.  
  111.     int low = 0, high = maxd;
  112.     while (low <= high)
  113.     {
  114.         for(int i = 0; i < v; i++) vis[i] = 0;
  115.         int mid = low + (high - low)/2; debug(mid);
  116.         for(int i = 0; i < v; i++)
  117.         {
  118.             if(distr[i] <= mid) vis[i] = 1;
  119.         }
  120.         if(vis[s] == 1) {high = mid - 1; continue;}
  121.         vi dist(v, INT_MAX); dist[s] = 0;
  122.         distance1(graph, vis, dist, s);
  123.         debug(dist);
  124.         if(dist[f] == mini) low = mid + 1;
  125.         else high = mid - 1;
  126.     }
  127.    
  128.     cout<<low<<endl;
  129.    
  130. }
  131. return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement