{"id":1188,"date":"2021-05-11T22:56:00","date_gmt":"2021-05-11T14:56:00","guid":{"rendered":"http:\/\/www.zyhcoding.club\/?p=1188"},"modified":"2021-05-12T17:00:12","modified_gmt":"2021-05-12T09:00:12","slug":"%e5%8f%af%e6%8c%81%e4%b9%85%e5%8c%96%e5%b9%b6%e6%9f%a5%e9%9b%86","status":"publish","type":"post","link":"http:\/\/www.zyhcoding.club\/index.php\/2021\/05\/11\/%e5%8f%af%e6%8c%81%e4%b9%85%e5%8c%96%e5%b9%b6%e6%9f%a5%e9%9b%86\/","title":{"rendered":"\u53ef\u6301\u4e45\u5316\u5e76\u67e5\u96c6"},"content":{"rendered":"<p>\u5e76\u67e5\u96c6\u4e0d\u7528\u591a\u8bf4\u4e86\uff0c\u5c31\u662f\u5408\u5e76\u4e24\u4e2a\u5b50\u96c6+\u8def\u5f84\u538b\u7f29\uff0c\u5728\u5e76\u67e5\u96c6\u4e0a\u52a0\u4e0a\u53ef\u6301\u4e45\u5316\u7684\u8bdd\uff0c\u5c31\u662f\u8bf4\u8981\u6709\u5386\u53f2\u7248\u672c\u7684\u5b58\u5728\uff0c\u6bcf\u66f4\u6539\u4e00\u6b21\u96c6\u5408\uff0c\u5c31\u8981\u65b0\u5efa\u4e00\u4e2a\u5e76\u67e5\u96c6\u3002\u4e0d\u53ef\u80fd\u628a\u6bcf\u4e00\u4e2a\u70b9\u90fd\u590d\u5236\u4e00\u904d\u6765\u65b0\u5efa\u4e00\u4e2a\u96c6\u5408\uff0c\u56e0\u4e3a\u6bcf\u6b21\u66f4\u6539\u53ea\u4f1a\u4fee\u6539\u4e00\u4e2a\u70b9\u7684\u7236\u4eb2\u662f\u8c01\uff0c\u6240\u4ee5\u53ea\u9700\u4fee\u6539\u8fd9\u4e2a\u70b9\u5c31\u884c\u4e86\uff0c\u8fd9\u4e5f\u662f\u53ef\u6301\u4e45\u5316\u7684\u57fa\u672c\u601d\u60f3\u3002\u56e0\u6b64\uff0c\u53ef\u6301\u4e45\u5316\u5e76\u67e5\u96c6\u65e0\u6cd5\u5728\u4f7f\u7528\u8def\u5f84\u538b\u7f29\uff0c\u56e0\u4e3a\u8def\u5f84\u538b\u7f29\u4f1a\u4fee\u6539\u4e0d\u6b62\u4e00\u4e2a\u70b9\uff0c\u8fd8\u4f1a\u4fee\u6539\u8def\u5f84\u4e0a\u7684\u6240\u6709\u70b9\uff0c\u8fd9\u6837\u7684\u8bdd\u5728\u53ef\u6301\u4e45\u5316\u6570\u636e\u7ed3\u6784\u4e2d\uff0c\u4fee\u6539\u7684\u6570\u636e\u53ef\u80fd\u662f\u88ab\u591a\u4e2a\u7248\u672c\u7684\u5e76\u67e5\u96c6\u5171\u540c\u62e5\u6709\u7684\uff0c\u4fee\u6539\u4f1a\u5f15\u53d1\u66f4\u591a\u7684\u9ebb\u70e6\u3002<\/p>\n<p>\u4e0d\u540c\u4e8e\u539f\u6765\u7684\u5e76\u67e5\u96c6\uff0c\u53ef\u6301\u4e45\u5316\u9700\u8981\u501f\u52a9\u7ebf\u6bb5\u6811\u8fd9\u4e2a\u6570\u636e\u7ed3\u6784\uff0c\u57280\u53f7\u7248\u672c\u7684\u5e76\u67e5\u96c6\u6784\u6210\u7684\u7ebf\u6bb5\u6811\u4e2d\uff0c\u53ea\u6709\u53f6\u5b50\u7ed3\u70b9\u6709\u6743\u503c\uff0c\u5176\u4f59\u7ed3\u70b9\u90fd\u53ea\u4fdd\u5b58\u5de6\u53f3\u5b69\u5b50\u7ed3\u70b9\u4fe1\u606f\u3002\u6bcf\u4e2a\u53f6\u5b50\u7ed3\u70b9\u7684\u6743\u503c\u5c31\u662f\u8fd9\u4e2a\u7ed3\u70b9\u5728\u5e76\u67e5\u96c6\u610f\u4e49\u4e0a\u7684\u7236\u4eb2\uff0c\u4e0d\u662f\u7ebf\u6bb5\u6811\u4e0a\u7684\u3002\u4e5f\u5c31\u662f\u8bf4\u6240\u6709\u53f6\u5b50\u8282\u70b9\u548c\u5176\u6743\u503c\u53ef\u4ee5\u6784\u6210\u4e00\u4e2a\u68ee\u6797\uff0c\u8fd9\u4e2a\u68ee\u6797\u5c31\u662f\u5f53\u524d\u7248\u672c\u7684\u5e76\u67e5\u96c6\u3002<\/p>\n<p>\u90a3\u4e48\u6bcf\u4e00\u6b21\u4fee\u6539\u5c31\u662f\u5408\u5e76\u4e24\u4e2a\u96c6\u5408\uff0c\u4e5f\u5c31\u662f\u5728\u65e7\u7248\u672c\u7684\u7ebf\u6bb5\u6811\u4e2d\u4fee\u6539\u4e00\u4e2a\u53f6\u5b50\u7ed3\u70b9\u7684\u6743\u503c\uff0c\u800c\u4e14\u4fee\u6539\u7684\u90a3\u4e2a\u53f6\u5b50\u7ed3\u70b9\u7684\u6743\u503c\u5728\u6570\u503c\u4e0a\u4e00\u5b9a\u662f\u5b83\u672c\u8eab\u3002\u81f3\u4e8e\u4fee\u6539\u4e24\u4e2a\u96c6\u5408\u4e2d\u54ea\u4e00\u4e2a\u7684\u6839\u7ed3\u70b9\u662f\u53ef\u6301\u4e45\u5316\u5e76\u67e5\u96c6\u7684<strong>\u6838\u5fc3<\/strong>\u3002\u3002\u6211\u4eec\u77e5\u9053\u4e00\u4e2a\u5e76\u67e5\u96c6\u5305\u542b\u4e00\u4e9b\u96c6\u5408\uff0c\u6bcf\u4e2a\u96c6\u5408\u53ef\u4ee5\u8868\u793a\u6210\u4e00\u68f5\u6811\uff0c\u5e76\u4e14\u6709\u4e00\u4e2a\u4ee3\u8868\u8fd9\u4e2a\u96c6\u5408\u7684\u6839\u7ed3\u70b9\uff0c\u800c\u6bcf\u68f5\u6811\u90fd\u4f1a\u6709\u4e00\u4e2a\u6df1\u5ea6\uff0c\u5f53\u6211\u4eec\u4e0d\u91c7\u7528\u8def\u5f84\u538b\u7f29\u65f6\uff0c\u8fd9\u4e2a\u6df1\u5ea6\u5c31\u51b3\u5b9a\u4e86\u6bcf\u6b21\u67e5\u8be2\u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u5f53\u7136\u662f\u6700\u5927\u6df1\u5ea6\u8d8a\u5c0f\u8d8a\u597d\u3002<\/p>\n<p>\u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u6211\u4eec\u7ed9\u6bcf\u4e2a\u53f6\u5b50\u7ed3\u70b9\u5b89\u6392\u4e00\u4e2a\u6df1\u5ea6\u6743\u503c\uff0c\u8fd9\u4e2a\u6743\u503c\u8868\u793a\u8fd9\u4e2a\u7ed3\u70b9\u5728\u5e76\u67e5\u96c6\u610f\u4e49\u4e0b\uff0c\u5176\u6240\u5728\u96c6\u5408\u6240\u6784\u6210\u7684\u6811\u4e2d\uff0c\u8be5\u7ed3\u70b9\u5230\u5b83\u6240\u6709\u7684\u5b69\u5b50\u7ed3\u70b9\u4e2d\u6700\u8fdc\u7684\u90a3\u4e00\u4e2a\u7684\u8ddd\u79bb\uff0c\u5f53\u7136\u8fd9\u4e2a\u6700\u8fdc\u7684\u90a3\u4e00\u4e2a\u80af\u5b9a\u662f\u53f6\u5b50\u7ed3\u70b9\u3002\u56e0\u4e3a\u6211\u4eec\u8bf4\u8fc7\u53ef\u6301\u4e45\u5316\u5e76\u67e5\u96c6\u662f\u5f88\u96be\u53bb\u8def\u5f84\u538b\u7f29\u7684\uff0c\u6240\u4ee5\u5728\u6bcf\u6b21\u5408\u5e76\u4e24\u4e2a\u96c6\u5408\u7684\u65f6\u5019\u5c31\u5e94\u8be5\u5c3d\u53ef\u80fd\u662f\u6240\u6709\u7ed3\u70b9\u7684\u6700\u5927\u6df1\u5ea6\u6743\u503c\u5c3d\u53ef\u80fd\u5730\u5c0f\u3002\u90a3\u4e48\u5bf9\u4e8e\u8981\u5408\u5e76\u7684\u4e24\u4e2a\u96c6\u5408\uff0c\u628a\u6839\u7ed3\u70b9\u6df1\u5ea6\u5c0f\u7684\u90a3\u4e2a\u96c6\u5408\u5e76\u5230\u6df1\u5ea6\u5927\u7684\u4e0a\u9762\u5c31\u884c\u4e86\uff0c\u8fd9\u6837\u6df1\u5ea6\u5927\u7684\u7ed3\u70b9\u6df1\u5ea6\u4e0d\u7528\u6539\u53d8\uff0c\u53ea\u6709\u5728\u8fd9\u4e2a\u6839\u7ed3\u70b9\u7684\u6df1\u5ea6\u4e00\u6837\u7684\u60c5\u51b5\u4e0b\uff0c\u624d\u4f1a\u6709\u6df1\u5ea6+1\u7684\u64cd\u4f5c\u3002<\/p>\n<p>\u53ef\u6301\u4e45\u5316\u5e76\u67e5\u96c6\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a<code class=\"katex-inline\">O(n(logn)^2)<\/code>\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6<code class=\"katex-inline\">O(nlogn)<\/code><\/p>\n<p>\u9898\u76ee<br \/>\n<a href=\"https:\/\/www.luogu.com.cn\/problem\/P3402\">P3402 \u53ef\u6301\u4e45\u5316\u5e76\u67e5\u96c6<\/a><br \/>\n\u5148\u8f93\u5165n\u548cm\uff0c\u8868\u793a\u521d\u59cb\u96c6\u5408\u4e2a\u6570\u548c\u64cd\u4f5c\u6570\u3002<br \/>\n\u4e09\u79cd\u64cd\u4f5c\uff1a<br \/>\n1 a b\uff1a\u5408\u5e76ab\u6240\u5728\u96c6\u5408<br \/>\n2 k\uff1a\u56de\u5230\u7b2ck\u6b21\u64cd\u4f5c\u540e<br \/>\n3 a b\uff1a\u8be2\u95eeab\u662f\u5426\u5728\u4e00\u4e2a\u96c6\u5408\u5185<br \/>\n\u505a\u8fd9\u9898\u7684\u65f6\u5019\uff0c\u7531\u4e8e\u5f53ab\u5728\u4e00\u4e2a\u96c6\u5408\u5185\u65f6\uff0c\u5c31\u4e0d\u7528\u5408\u5e76\uff0c\u5fd8\u4e86\u7ed9<code>root[i]<\/code>\u8d4b\u503c\uff0c\u5bfc\u81f4\u5361\u4e86\u5f88\u4e45\u3002\u3002\u3002\u6211\u8c03\u4ee3\u7801\u7684\u80fd\u529b\u771f\u7684\u662f\u8d8a\u6765\u8d8a\u5e9f\u4e86...<\/p>\n<pre><code class=\"language-c++\">#include &lt;bits\/stdc++.h&gt;\n\nusing namespace std;\nconst int maxn = 1e5+5;\nint n, m;\n\nstruct zyh {\n    int l, r;\n    int fa, deep;\n}tree[maxn*30];\n\nint root[maxn*2], top;       \/\/ root[i]\u8868\u793a\u7b2ci\u4e2a\u7248\u672c\u7684\u6811\u6839\u4f4d\u7f6e\uff0ctop\u8868\u793a\u6570\u636e\u6c60\u4f7f\u7528\u60c5\u51b5\n\nint build(int l, int r) {\n    int tmp = ++top;\n    if (l==r) {\n        tree[tmp].fa = l;\n        tree[tmp].deep = 0;\n        return tmp;\n    }\n    int mid = (l+r) &gt;&gt; 1;\n    tree[tmp].l = build(l, mid);\n    tree[tmp].r = build(mid+1, r);\n    return tmp;\n}\n\nint find(int x, int edt) {       \/\/ \u5bfb\u627eedt\u7248\u672c\u4e0bx\u7684\u6839\u7ed3\u70b9\n    int l=1, r=n, mid, now = root[edt];\n    while (l&lt;r) {\n        mid = (l+r) &gt;&gt; 1;\n        if (x&lt;=mid) {\n            r=mid;\n            now = tree[now].l;\n        }else {\n            l=mid+1;\n            now = tree[now].r;\n        }\n    }\n    if (tree[now].fa != l)  return find(tree[now].fa, edt);\n    return now;\n}\n\/\/ x\u8868\u793a\u6e90\u7ed3\u70b9\uff0c lr\u4e3a\u533a\u95f4\u4fe1\u606f\uff0ck\u4e3a\u8981\u4fee\u6539\u7684\u7ed3\u70b9\uff0cw\u4e3a\u4fee\u6539\u7684\u503c\uff0c\u7136\u540e\u8fd4\u56de\u5728x\u7248\u672c\u4e0a\u4fee\u6539\u540e\u5f97\u5230\u7684\u65b0\u7ed3\u70b9\n\/\/ flag \u8868\u793a\u4fee\u6539\u7684\u6570\u636e\u7c7b\u578b\uff0cflag=1\u8868\u793a\u53ea\u6539fa\uff0cflag=2\u8868\u793a\u53ea\u6539deep\uff0c\u6539\u4e3adeep+1\nint add(int x, int l, int r, int k, int w, int flag) {\n    int tmp = ++top;\n    if (l==r) {\n        if (1==flag) {\n            tree[tmp].fa = w;\n            tree[tmp].deep = tree[x].deep;\n        }else {\n            tree[tmp].fa = tree[x].fa;\n            tree[tmp].deep = tree[x].deep + 1;\n        }\n        return tmp;\n    }\n    int mid = (l+r) &gt;&gt; 1;\n    if (k&lt;=mid) {\n        tree[tmp].r = tree[x].r;\n        tree[tmp].l = add(tree[x].l, l, mid, k, w, flag);\n    }else {\n        tree[tmp].l = tree[x].l;\n        tree[tmp].r = add(tree[x].r, mid+1, r, k, w, flag);\n    }\n    return tmp;\n}\n\nint main() {\n    \/\/freopen(&quot;in.txt&quot;, &quot;r&quot;, stdin);\n    int a, b, opt, k;\n    scanf (&quot;%d %d&quot;, &amp;n, &amp;m);\n    \/\/top = 5;\n    root[0] = build(1, n);\n    for (int i=1; i&lt;=m; i++) {\n        scanf (&quot;%d&quot;, &amp;opt);\n        if (1==opt) {\n            scanf (&quot;%d %d&quot;, &amp;a, &amp;b);\n            int f1 = find(a, i-1), f2 = find(b, i-1);\n            root[i] = root[i-1];\n            if (tree[f1].fa == tree[f2].fa) \n                continue;\n\n            if (tree[f1].deep &gt; tree[f2].deep) {\n                root[i] = add(root[i], 1, n, tree[f2].fa, tree[f1].fa, 1);\n            } else if (tree[f1].deep &lt; tree[f2].deep) {\n                root[i] = add(root[i], 1, n, tree[f1].fa, tree[f2].fa, 1);\n            } else {\n                root[i] = add(root[i], 1, n, tree[f2].fa, tree[f1].fa, 1);\n                root[i] = add(root[i], 1, n, tree[f1].fa, tree[f1].fa, 2);\n            }\n        }else if (2==opt) {\n            scanf (&quot;%d&quot;, &amp;k);\n            root[i] = root[k];\n        }else {\n            root[i] = root[i-1];\n            scanf (&quot;%d %d&quot;, &amp;a, &amp;b);\n            if (tree[find(a, i)].fa == tree[find(b, i)].fa)   puts (&quot;1&quot;);\n            else    puts(&quot;0&quot;);\n        }\n    }\n    return 0;\n}<\/code><\/pre>\n<h6><a href=\"https:\/\/www.luogu.com.cn\/problem\/P4768\">P4768 [NOI2018] \u5f52\u7a0b<\/a><\/h6>\n<p>T\u7ec4\u6570\u636e\uff0c\u6bcf\u7ec4\u6570\u636e\u7ed9\u5b9a\u4e00\u4e2a\u65e0\u5411\u56fe\uff0cn\u4e2a\u70b9\uff0cm\u6761\u8fb9\uff0c\u6bcf\u6761\u8fb9\u6709\u4e24\u4e2a\u5c5e\u6027\uff1a\u957f\u5ea6l\u548c\u6d77\u62d4a\u3002Q\u6b21\u8be2\u95ee\uff0c\u6bcf\u6b21\u8be2\u95ee\u7ed9\u4e00\u4e2a\u8d77\u59cb\u70b9v\u548c\u6d77\u62d4\u9650\u5236p\uff0c\u53ea\u6709\u90a3\u4e9b\u6d77\u62d4\u5927\u4e8ep\u7684\u8fb9\u624d\u53ef\u4ee5\u5f00\u8f66\u8d70\uff0c\u6240\u6709\u7684\u8fb9\u90fd\u53ef\u4ee5\u6b65\u884c\u7ecf\u8fc7\uff0c\u5bf9\u4e8e\u6bcf\u6b21\u8be2\u95ee\uff0c\u8f93\u51fa\u4ece\u7ed3\u70b9v\u5230\u7ed3\u70b91\u9700\u8981\u6b65\u884c\u7684\u6700\u77ed\u8ddd\u79bb\u3002\u8f66\u4e0d\u80fd\u8d70\u6d77\u62d4\u5c0f\u4e8e\u7b49\u4e8ep\u7684\u8fb9\uff0c\u4f46\u662f\u53ef\u4ee5\u6b65\u884c\uff0c\u800c\u4e14\u5f53\u4e0b\u8f66\u5f00\u59cb\u6b65\u884c\u540e\u5c31\u518d\u4e5f\u4e0d\u80fd\u5f00\u8f66\u4e86\u3002\u8be2\u95ee\u4e4b\u95f4\u76f8\u4e92\u72ec\u7acb\u3002<\/p>\n<p>\u6839\u636e\u9898\u610f\uff0c\u6bcf\u4e00\u6b21\u8be2\u95ee\u90fd\u76f8\u5f53\u4e8e\u628a\u6d77\u62d4\u5927\u4e8ep\u7684\u8fb9\u52a0\u5165\u56fe\u4e2d\uff0c\u82e5\u52a0\u5165\u540ev\u548c1\u5728\u540c\u4e00\u4e2a\u8fde\u901a\u5757\u5185\uff0c\u90a3\u4e48\u7b54\u6848\u5c31\u662f0\uff0c\u5426\u5219\u7b54\u6848\u7b49\u4e8e\u8be5\u7ed3\u70b9\u6240\u5728\u8fde\u901a\u5757\u5185\u6240\u6709\u7ed3\u70b9\u6b65\u884c\u52301\u53f7\u7ed3\u70b9\u7684\u6700\u77ed\u8ddd\u79bb\u3002<\/p>\n<p>\u8fd9\u4e2a\u601d\u8def\u662f\u6b63\u786e\u7684\uff0c\u90a3\u4e48\u5b9e\u73b0\u4ee3\u7801\u7684\u8bdd\uff0c\u5c31\u5148\u628a\u56fe\u8f93\u5165\uff0c\u7136\u540e\u6c42\u4e00\u4e2a\u6700\u77ed\u8def(<code>Dijkstra<\/code>)\uff0c\u628a\u6bcf\u4e2a\u70b9\u6b65\u884c\u52301\u53f7\u7ed3\u70b9\u7684\u6700\u77ed\u8ddd\u79bb\u4fdd\u5b58\uff0c\u505a\u4e3a\u4e00\u4e2a\u6743\u503c\u3002\u7136\u540e\u5efa\u7acb\u53ef\u6301\u4e45\u5316\u5e76\u67e5\u96c6\uff0c\u628a\u56fe\u7684\u6240\u6709\u8fb9\u6309\u6d77\u62d4\u4ece\u5927\u5230\u5c0f\u6392\u5e8f\uff0c\u7136\u540e\u4f9d\u6b21\u52a0\u5165\u5230\u56fe\u4e2d\uff0c\u76f8\u5f53\u4e8e\u662f\u5408\u5e76\u4e24\u4e2a\u96c6\u5408\uff0c\u6bcf\u52a0\u5165\u4e00\u6761\u8fb9\u5c31\u65b0\u5efa\u4e00\u4e2a\u7248\u672c\u7684\u5e76\u67e5\u96c6\u3002\u800c\u4e14\u5e76\u67e5\u96c6\u4e2d\u6bcf\u4e2a\u96c6\u5408\u90fd\u8981\u7ef4\u62a4\u6700\u77ed\u8def\u4fe1\u606f(\u5c31\u662f\u8fd9\u4e2a\u96c6\u5408\u4e2d\u7684\u70b9\u6b65\u884c\u52301\u53f7\u7ed3\u70b9\u7684\u6700\u77ed\u8def\u7a0b)\u3002\u5168\u90e8\u5904\u7406\u5b8c\u6210\u4e4b\u540e\uff0c\u5bf9\u4e8e\u6bcf\u6b21\u8be2\u95ee\uff0c\u5148\u4e8c\u5206\u67e5\u627e\u54ea\u4e9b\u8fb9\u662f\u5927\u4e8ep\u7684\uff0c\u6211\u4eec\u9700\u8981\u8bbf\u95ee\u54ea\u4e2a\u7248\u672c\u7684\u5e76\u67e5\u96c6\uff0c\u7136\u540e\u76f4\u63a5\u8f93\u51fav\u6240\u5728\u96c6\u5408\u7684\u6700\u77ed\u8def\u7a0b\u8fd9\u4e2a\u6743\u503c\u5c31\u884c\u4e86\u3002<\/p>\n<pre><code class=\"language-c++\">#include &lt;bits\/stdc++.h&gt;\n\nusing namespace std;\n\nconst int maxn = 2e5+5, maxm = 4e5+5, inf = 2147483647;\n\n\/\/ \u5efa\u56fe\u6240\u7528\u6570\u636e\uff0c\u4ee5\u53ca\u6700\u77ed\u8def\nint n, m;\nvector&lt;int&gt; G[maxn], w[maxn];\nint dist[maxn];\nbool vis[maxn];\ntypedef struct zyh1{\n    int pos;\n    int dist;\n    friend bool operator &lt; (const struct zyh1 &amp;a, const struct zyh1 &amp;b) {\n        return a.dist &gt; b.dist;\n    }\n}node;\npriority_queue&lt;node&gt; q;\n\n\/\/ \u53ef\u6301\u4e45\u5316\u5e76\u67e5\u96c6\u90e8\u5206..\n\/\/ \u628a\u56fe\u4e2d\u8fb9\u7684\u6d77\u62d4\u4ece\u5927\u5230\u5c0f\u6392\u5e8f\u540e\uff0c\u6bcf\u6b21\u52a0\u5165\u4e00\u6761\u8fb9\u5230\u56fe\u4e2d\uff0c\u5e76\u5bf9\u5e76\u67e5\u96c6\u8fdb\u884c\u64cd\u4f5c\u3002\n\/\/ \u5e76\u67e5\u96c6\u8981\u7ef4\u62a4\u7684\u662f \u67d0\u4e2a\u5b50\u96c6\u5408\u4e2d\u6240\u6709\u70b9\u52301\u53f7\u7ed3\u70b9\u7684\u6700\u77eddist\nstruct zyh2 {\n    int l, r;\n    int fa, deep;\n    int dist;\n}tree[maxn*50];\nint top, root[maxm];\nstruct zyh3 {\n    int u, v, l, a;\n    zyh3(int u, int v, int l, int a) {\n        this-&gt;u = u, this-&gt;v = v;\n        this-&gt;l = l, this-&gt;a = a;\n    }\n    zyh3(){}\n}edges[maxm];\nbool cmp(const struct zyh3 &amp;a, const struct zyh3 &amp;b) {\n    return a.a &gt; b.a;\n}\n\ninline int read() {\n    int x=0, f=1;\n    char ch=getchar();\n    while (ch&gt;&#039;9&#039; || ch&lt;&#039;0&#039;) {\n        if (ch==&#039;-&#039;)    f=-1;\n        ch=getchar();\n    }\n    while (ch&gt;=&#039;0&#039; &amp;&amp; ch&lt;=&#039;9&#039;) {\n        x = x*10 + ch-&#039;0&#039;;\n        ch=getchar();\n    }\n    return x*f;\n}\n\nvoid Dijkstra(int s, int dist[], vector&lt;int&gt; G[], vector&lt;int&gt; w[]) {\n    for (int i=1; i&lt;=n; i++)    dist[i] = inf, vis[i] = 0;\n    dist[s] = 0;\n    q.push(node{s, 0});\n    node tmp;\n    while (!q.empty()) {\n        tmp = q.top();    q.pop();\n        if (vis[tmp.pos])   continue;\n        vis[tmp.pos] = 1;\n        for (int i=0; i&lt;G[tmp.pos].size(); i++) {\n            if (dist[G[tmp.pos][i]] &gt; tmp.dist + w[tmp.pos][i]) {\n                dist[G[tmp.pos][i]] = tmp.dist + w[tmp.pos][i];\n                q.push(node{G[tmp.pos][i], dist[G[tmp.pos][i]]});\n            }\n        }\n    }\n    return;\n}\n\nint build(int l, int r) {\n    int tmp = ++top;\n    if (l==r) {\n        tree[tmp].fa = l;\n        tree[tmp].deep = 0;\n        tree[tmp].dist = dist[l];\n        return tmp;\n    }\n    int mid = (l+r) &gt;&gt; 1;\n    tree[tmp].l = build(l, mid);\n    tree[tmp].r = build(mid+1, r);\n    return tmp;\n}\n\nint find (int x, int edt) {\n    int l=1, r=n, mid, now = root[edt];\n    while (l&lt;r) {\n        mid = (l+r) &gt;&gt; 1;\n        if (x&lt;=mid) {\n            r = mid;\n            now = tree[now].l;\n        }else {\n            l = mid+1;\n            now = tree[now].r;\n        }\n    }\n    if (tree[now].fa != l)  return find(tree[now].fa, edt);\n    return now;\n}\n\n\/\/ 3\u79cd\u64cd\u4f5c\uff0c\u4fee\u6539\u67d0\u4e2a\u70b9\u7684\u7236\u4eb2\uff0c\u4fee\u6539\u67d0\u4e2a\u70b9\u7684dist\uff0c\u4fee\u6539\u67d0\u4e2a\u70b9\u7684deep\u548cdist\u3002\nint add(int x, int l, int r, int k, int w, int flag) {\n    int tmp = ++top;\n    if (l==r) {\n        tree[tmp].fa = tree[x].fa;\n        tree[tmp].deep = tree[x].deep;\n        tree[tmp].dist = tree[x].dist;\n        if (1==flag) {\n            tree[tmp].fa = w;\n        }else if (2==flag){\n            tree[tmp].dist = w;\n        }else {\n            tree[tmp].dist = w;\n            tree[tmp].deep += 1;\n        }\n        return tmp;\n    }\n    int mid = (l+r) &gt;&gt; 1;\n    if (k&lt;=mid) {\n        tree[tmp].l = add(tree[x].l, l, mid, k, w, flag);\n        tree[tmp].r = tree[x].r;\n    }else {\n        tree[tmp].l = tree[x].l;\n        tree[tmp].r = add(tree[x].r, mid+1, r, k, w, flag);\n    }\n    return tmp;\n}\n\nint src(int x) {\n    if (edges[1].a &lt;= x) return 0;\n    int l=1, r=m, mid;\n    while (l&lt;r) {\n        mid = ((l+r) &gt;&gt; 1) + ((l+r)&amp;1);\n        if (edges[mid].a&gt;x)    l=mid;\n        else    r = mid-1;\n    }\n    return l;\n}\n\nint main() {\n    int t, u, v, l, a;\n    int Q, K, S;\n    t = read();\n    while (t--) {\n        top = 0;\n        n = read(), m = read();\n        for (int i=1; i&lt;=n; i++) {\n            G[i].clear();\n            w[i].clear();\n        }\n        for (int i=1; i&lt;=m; i++) {\n            u = read(), v = read();\n            l = read(), a = read();\n            G[u].push_back(v);\n            w[u].push_back(l);\n            G[v].push_back(u);\n            w[v].push_back(l);\n            edges[i] = *(new zyh3(u, v, l, a));\n        }\n        Dijkstra(1, dist, G, w);\n\n        sort (edges+1, edges+m+1, cmp);\n        root[0] = build(1, n);\n        for (int i=1; i&lt;=m; i++) {\n            int f1 = find(edges[i].u, i-1), f2 = find(edges[i].v, i-1);\n            root[i] = root[i-1];\n            if (tree[f1].fa == tree[f2].fa)  continue;\n            int tmp = min(tree[f1].dist, tree[f2].dist);\n            if (tree[f1].deep &gt; tree[f2].deep) {\n                root[i] = add(root[i-1], 1, n, tree[f2].fa, tree[f1].fa, 1);\n                root[i] = add(root[i], 1, n, tree[f1].fa, tmp, 2);\n            }else if (tree[f1].deep &lt; tree[f2].deep){\n                root[i] = add(root[i-1], 1, n, tree[f1].fa, tree[f2].fa, 1);\n                root[i] = add(root[i], 1, n, tree[f2].fa, tmp, 2);\n            }else {\n                root[i] = add(root[i-1], 1, n, tree[f2].fa, tree[f1].fa, 1);\n                root[i] = add(root[i], 1, n, tree[f1].fa, tmp, 3);\n            }\n        }\n        Q = read(), K = read(), S = read();\n        int lastans = 0, v, p;\n        for (int i=1; i&lt;=Q; i++) {\n            v = (read() + K*lastans-1)%n+1;\n            p = (read() + K*lastans)%(S+1);\n            int edt = src(p);\n            int f = find(v, edt);\n            lastans = tree[f].dist;\n            printf (&quot;%d\\n&quot;, lastans);\n        }\n    }\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5e76\u67e5\u96c6\u4e0d\u7528\u591a\u8bf4\u4e86\uff0c\u5c31\u662f\u5408\u5e76\u4e24\u4e2a\u5b50\u96c6+\u8def\u5f84\u538b\u7f29\uff0c\u5728\u5e76\u67e5\u96c6\u4e0a\u52a0\u4e0a\u53ef\u6301\u4e45\u5316\u7684\u8bdd\uff0c\u5c31\u662f\u8bf4\u8981\u6709\u5386\u53f2\u7248\u672c\u7684\u5b58\u5728\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[71],"tags":[],"_links":{"self":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/1188"}],"collection":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/comments?post=1188"}],"version-history":[{"count":10,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/1188\/revisions"}],"predecessor-version":[{"id":1192,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/1188\/revisions\/1192"}],"wp:attachment":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/media?parent=1188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/categories?post=1188"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/tags?post=1188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}