Advertisement
7oSkaaa

G - Maximum People

Jul 12th, 2022
870
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.68 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define cin_2d(vec, n, m) for(int i = 0; i < n; i++) for(int j = 0; j < m && cin >> vec[i][j]; j++);
  6. #define cout_2d(vec, n, m) for(int i = 0; i < n; i++, cout << "\n") for(int j = 0; j < m && cout << vec[i][j] << " "; j++);
  7. #define cout_map(mp) for(auto& [f, s] : mp) cout << f << "  " << s << "\n";
  8. #define Time cerr << "Time Taken: " << (float)clock() / CLOCKS_PER_SEC << " Secs" << "\n";
  9. #define fixed(n) fixed << setprecision(n)
  10. #define ceil(n, m) (((n) / (m)) + ((n) % (m) ? 1 : 0))
  11. #define fill(vec, value) memset(vec, value, sizeof(vec));
  12. #define Num_of_Digits(n) ((int)log10(n) + 1)
  13. #define mod_combine(a, b, m) (((a % m) * (b % m)) % m)
  14. #define all(vec) vec.begin(), vec.end()
  15. #define rall(vec) vec.rbegin(), vec.rend()
  16. #define sz(x) int(x.size())
  17. #define debug(x) cout << #x << ": " << (x) << "\n";
  18. #define fi first
  19. #define se second
  20. #define ll long long
  21. #define ull unsigned long long
  22. #define Mod  1'000'000'007
  23. #define OO 2'000'000'000
  24. #define EPS 1e-9
  25. #define PI acos(-1)
  26. template < typename T = int > using Pair = pair < T, T >;
  27.  
  28. template < typename T = int > istream& operator >> (istream &in, vector < T > &v) {
  29.     for (auto &x: v) in >> x;
  30.     return in;
  31. }
  32.  
  33. template < typename T = int > ostream& operator << (ostream &out, const vector < T > &v) {
  34.     for (const T &x: v) out << x << ' ';
  35.     return out;
  36. }
  37.  
  38. struct DSU {
  39.    
  40.     vector < int > parent, Gsize;
  41.  
  42.     DSU(int MaxNodes){
  43.         parent.resize(MaxNodes + 5);
  44.         Gsize.resize(MaxNodes + 5);
  45.         for(int i = 1; i <= MaxNodes; i++)
  46.           parent[i] = i, Gsize[i] = 1;
  47.     }
  48.    
  49.     int find_leader(int node){
  50.         return parent[node] = (parent[node] == node ? node : find_leader(parent[node]));
  51.     }
  52.  
  53.     bool is_same_sets(int u, int v){
  54.         return find_leader(u) == find_leader(v);
  55.     }
  56.  
  57.     void union_sets(int u, int v){
  58.         int leader_u = find_leader(u), leader_v = find_leader(v);
  59.         if(leader_u == leader_v) return;
  60.         if(Gsize[leader_u] < Gsize[leader_v]) swap(leader_u, leader_v);
  61.         Gsize[leader_u] += Gsize[leader_v], parent[leader_v] = leader_u;
  62.     }
  63.  
  64.     int get_size(int node){
  65.         return Gsize[find_leader(node)];
  66.     }
  67. };
  68.  
  69. struct Edge {
  70.  
  71.     int u, v, w;
  72.  
  73.     Edge(int U = 0, int V = 0, int W = 0) : u(U), v(V), w(W) {}
  74.  
  75.     bool operator < (const Edge &e) const {
  76.         return w > e.w;
  77.     }
  78.  
  79. };
  80.  
  81. constexpr int N = 1e4;
  82.  
  83. int n, m;
  84. vector < int > dep(N + 10);
  85. vector < vector < Pair < int > > > adj(N + 10);
  86. vector < vector < int > > anc(N + 10, vector < int > (20)), cost(N + 10, vector < int > (20));
  87.  
  88. int k_ancestor(int node, int dist){
  89.     if(dep[node] <= dist) return -1;
  90.     for(int bit = 0; bit < 20; bit++)
  91.         if(dist & (1 << bit))
  92.             node = anc[node][bit];
  93.     return node;
  94. }
  95.  
  96. int combine(int u, int v){
  97.     return min(u, v);
  98. }
  99.  
  100. void dfs(int node, int par, int c){
  101.     dep[node] = dep[par] + 1, anc[node][0] = par, cost[node][0] = c;
  102.     for(int bit = 1; bit < 20; bit++){
  103.         anc[node][bit] = anc[anc[node][bit - 1]][bit - 1];
  104.         cost[node][bit] = min(cost[node][bit - 1], cost[anc[node][bit - 1]][bit - 1]);
  105.     }
  106.     for(auto& [new_node, value] : adj[node])
  107.         if(new_node != par)
  108.             dfs(new_node, node, value);
  109. }
  110.  
  111. int lca(int u, int v){
  112.     if(dep[u] > dep[v])
  113.     swap(u, v);
  114.     v = k_ancestor(v, dep[v] - dep[u]);
  115.     if(u == v) return u;
  116.     for(int bit = 19; bit >= 0; bit--)
  117.     if(anc[u][bit] != anc[v][bit])
  118.         u = anc[u][bit], v = anc[v][bit];
  119.     return anc[u][0];
  120. }
  121.  
  122. int get_cost(int node, int dist){
  123.     if(dep[node] <= dist) return -1;
  124.     int ans = OO;
  125.     for(int bit = 0; bit < 20; bit++)
  126.         if(dist & (1 << bit))
  127.             ans = combine(ans, cost[node][bit]), node = anc[node][bit];
  128.     return ans;
  129. }
  130.  
  131. int query(int u, int v){
  132.     if(dep[u] > dep[v])
  133.     swap(u, v);
  134.     int LCA = lca(u, v);
  135.     return combine(get_cost(u, dep[u] - dep[LCA]), get_cost(v, dep[v] - dep[LCA]));
  136. }
  137.  
  138.  
  139. void Solve(){
  140.     cin >> n >> m;
  141.     vector < Edge > edges(m);
  142.     for(auto& e : edges)
  143.         cin >> e.u >> e.v >> e.w;
  144.     sort(all(edges));
  145.     DSU dsu(n);
  146.     for(auto& e : edges){
  147.         if(dsu.is_same_sets(e.u, e.v))
  148.             continue;
  149.         dsu.union_sets(e.u, e.v);
  150.         adj[e.u].push_back({e.v, e.w}), adj[e.v].push_back({e.u, e.w});
  151.     }
  152.     dfs(1, 0, 0);
  153.     int q;
  154.     cin >> q;
  155.     while(q--){
  156.         int u, v;
  157.         cin >> u >> v;
  158.         cout << query(u, v) << '\n';
  159.     }
  160. }
  161.  
  162. int main(){
  163.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  164.     int t = 1;
  165.     //cin >> t;
  166.     while(t--)
  167.         Solve();
  168.     return 0;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement