Dmaxiya

网络稳定性 参考代码

Apr 4th, 2025
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long LL;
  5. const int Log = 20;
  6. const int maxn = 300000 + 100;
  7. struct Edge {
  8.     int u, v, w;
  9.     Edge() {}
  10.     Edge(int u, int v, int w) : u(u), v(v), w(w) {}
  11. };
  12.  
  13. bool operator<(const Edge &a, const Edge &b) {
  14.     return a.w > b.w;
  15. }
  16.  
  17. struct Node {
  18.     int pos, dis;
  19.     Node() {}
  20.     Node(int pos, int dis) : pos(pos), dis(dis) {}
  21. };
  22.  
  23. int n, m, q, u, v, w, idCnt;
  24. int fa[maxn], id[maxn];
  25. bool vis[maxn];
  26. Edge edge[maxn];
  27. vector<Node> G[maxn];
  28. int st[maxn][Log], disSt[maxn][Log];
  29.  
  30. void init() {
  31.     for (int i = 1; i <= n; ++i) {
  32.         fa[i] = i;
  33.     }
  34. }
  35.  
  36. int Find(int x) {
  37.     return x == fa[x] ? x : fa[x] = Find(fa[x]);
  38. }
  39.  
  40. void union_(int x, int y) {
  41.     fa[Find(x)] = Find(y);
  42. }
  43.  
  44. void dfs(int root, int fa, int dis) {
  45.     vis[root] = true;
  46.     id[root] = ++idCnt;
  47.     st[root][0] = fa;
  48.     disSt[root][0] = dis;
  49.     for(int j = 1; j < Log; ++j) {
  50.         st[root][j] = st[st[root][j - 1]][j - 1];
  51.         disSt[root][j] = min(disSt[root][j - 1], disSt[st[root][j - 1]][j - 1]);
  52.     }
  53.     for (Node &node : G[root]) {
  54.         int pos = node.pos;
  55.         int dis = node.dis;
  56.         if (pos != fa) {
  57.             dfs(pos, root, dis);
  58.         }
  59.     }
  60. }
  61.  
  62. int lca(int x, int y) {
  63.     if(x == y) {
  64.         return x;
  65.     }
  66.     if(id[x] > id[y]) {
  67.         swap(x, y);
  68.     }
  69.     for(int i = Log - 1; i >= 0; --i) {
  70.         if(id[st[y][i]] > id[x]) {
  71.             y = st[y][i];
  72.         }
  73.     }
  74.     return st[y][0];
  75. }
  76.  
  77. int lcaDis(int f, int u) {
  78.     int ret = INT_MAX;
  79.     if (f == u) {
  80.         return ret;
  81.     }
  82.     for (int i = Log - 1; i >= 0; --i) {
  83.         if (id[st[u][i]] > id[f]) {
  84.             ret = min(ret, disSt[u][i]);
  85.             u = st[u][i];
  86.         }
  87.     }
  88.     return min(ret, disSt[u][0]);
  89. }
  90.  
  91. int main() {
  92. #ifdef ExRoc
  93.     freopen("test.txt", "r", stdin);
  94. #endif // ExRoc
  95.     ios::sync_with_stdio(false);
  96.  
  97.     cin >> n >> m >> q;
  98.     for (int i = 0; i < m; ++i) {
  99.         cin >> edge[i].u >> edge[i].v >> edge[i].w;
  100.     }
  101.     init();
  102.     sort(edge, edge + m);
  103.     for (int i = 0; i < m; ++i) {
  104.         u = edge[i].u;
  105.         v = edge[i].v;
  106.         w = edge[i].w;
  107.         if (Find(u) != Find(v)) {
  108.             union_(u, v);
  109.             G[u].push_back(Node(v, w));
  110.             G[v].push_back(Node(u, w));
  111.         }
  112.     }
  113.     for (int i = 1; i <= n; ++i) {
  114.         if (!vis[i]) {
  115.             dfs(i, i, 0);
  116.         }
  117.     }
  118.     while (q--) {
  119.         cin >> u >> v;
  120.         if (Find(u) != Find(v)) {
  121.             cout << -1 << endl;
  122.             continue;
  123.         }
  124.         int f = lca(u, v);
  125.         cout << min(lcaDis(f, u), lcaDis(f, v)) << endl;
  126.     }
  127.  
  128.     return 0;
  129. }
Add Comment
Please, Sign In to add comment