{"id":75,"date":"2023-03-16T15:18:34","date_gmt":"2023-03-16T07:18:34","guid":{"rendered":"https:\/\/solost.cn\/?p=75"},"modified":"2023-03-16T15:18:34","modified_gmt":"2023-03-16T07:18:34","slug":"165","status":"publish","type":"post","link":"https:\/\/solost.cn\/?p=75","title":{"rendered":"AtCoder Beginner Contest 293 &#8211; D &#8211; Tying Rope"},"content":{"rendered":"<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2026\/11\/image-8.png\" alt=\"image.png\" title=\"image.png\"><\/p>\n<h2>\u94fe\u63a5<\/h2>\n<p><a href=\"https:\/\/atcoder.jp\/contests\/abc293\/tasks\/abc293_d\">\u9898\u76ee\u94fe\u63a5<\/a><\/p>\n<h2>solution<\/h2>\n<p>\u8fd9\u9053\u9898\u7b80\u5316\u4e4b\u540e\u5176\u5b9e\u5c31\u662f\u627e\u56fe\u4e2d\u7684\u73af\u548c\u7ebf\uff0c\u6211\u4eec\u5148\u8fdb\u884c\u5e76\u67e5\u96c6\u64cd\u4f5c\uff0c\u628a\u80fd\u8fde\u63a5\u6210\u73af\u7684\u90fd\u8fde\u63a5\u6210\u73af\uff0c\u5269\u4e0b\u7684\u4e0d\u80fd\u8fde\u63a5\u7684\u653e\u90a3\uff0c\u7136\u540e\u7528BFS\u641c\u7d22\u4e00\u4e0b\u8fde\u901a\u5757\u7684\u4e2a\u6570\uff0c\u7528\u8fde\u901a\u5757\u7684\u4e2a\u6570\u51cf\u53bb\u5df2\u7ecf\u6210\u73af\u7684\u4e2a\u6570\u5c31\u662f\u5269\u4e0b\u6ca1\u6709\u6210\u73af\u7684\u4e2a\u6570\u3002\u600e\u4e48\u5224\u65ad\u6210\u6ca1\u6210\u73af\uff0c\u5728\u8fdb\u884c\u5e76\u67e5\u96c6\u7684\u65f6\u5019\uff0c\u5982\u679c\u6211\u4eec\u53d1\u73b0\u4ed6\u4eec\u4fe9\u7684\u7236\u8282\u70b9\u662f\u540c\u4e00\u4e2a\uff0c\u90a3\u4e48\u5c31\u786e\u5b9a\u4ed6\u4eec\u5df2\u7ecf\u8fde\u63a5\u6210\u73af\u4e86\uff0c\u7136\u540eans1++\u3002<\/p>\n<pre><code class=\"lang-cpp\">#include &lt;algorithm&gt;\n#include &lt;bitset&gt;\n#include &lt;cmath&gt;\n#include &lt;cstdio&gt;\n#include &lt;cstdlib&gt;\n#include &lt;cstring&gt;\n#include &lt;fstream&gt;\n#include &lt;iostream&gt;\n#include &lt;map&gt;\n#include &lt;queue&gt;\n#include &lt;set&gt;\n#include &lt;stack&gt;\n#include &lt;unordered_map&gt;\n#include &lt;vector&gt;\n\nusing namespace std;\n#define x first\n#define y second\n#define endl &#039;\\n&#039;\n#define IOS                       \\\n    ios_base::sync_with_stdio(0); \\\n    cin.tie(0);                   \\\n    cout.tie(0);\ntypedef long long ll;\ntypedef pair&lt;int, int&gt; PII;\ntypedef unsigned long long ull;\nconst int INF = 0x3f3f3f3f, mod = 1000000007;\nconst int N = 2e5 + 10;\nint p[N];\nvector&lt;int&gt; size(N, 1), g[N];\nint find(int x)\n{\n    if (x != p[x])\n        p[x] = find(p[x]);\n    return p[x];\n}\nmap&lt;int, int&gt; vis;\nvoid dfs(int x)\n{\n    vis[x] = 1;\n    for (auto i : g[x]) {\n        if (!vis[i]) {\n            dfs(i);\n        }\n    }\n}\nvoid solve()\n{\n    int n, m;\n    cin &gt;&gt; n &gt;&gt; m;\n    for (int i = 1; i &lt;= n; i++)\n        p[i] = i;\n    int ans1 = 0, ans2 = 0;\n    vector&lt;PII&gt; q;\n    while (m--) {\n        int a, c;\n        char b, d;\n        cin &gt;&gt; a &gt;&gt; b &gt;&gt; c &gt;&gt; d;\n        g[a].push_back(c);\n        g[c].push_back(a);\n        int fa = find(a), fc = find(c);\n        if (fa == fc) {\n            ans1++;\n        } else {\n            p[fc] = fa;\n        }\n    }\n    \/\/ cout &lt;&lt; ans2 &lt;&lt; endl;\n    for (int i = 1; i &lt;= n; i++) {\n        if (!vis[i]) {\n            dfs(i);\n            ans2++;\n        }\n    }\n    cout &lt;&lt; ans1 &lt;&lt; &quot; &quot; &lt;&lt; ans2 - ans1 &lt;&lt; endl;\n}\nint main()\n{\n    \/\/ IOS;\n    int t = 1;\n    \/\/ cin &gt;&gt; t;\n    while (t--) {\n        solve();\n    }\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u94fe\u63a5 \u9898\u76ee\u94fe\u63a5 solution \u8fd9\u9053\u9898\u7b80\u5316\u4e4b\u540e\u5176\u5b9e\u5c31\u662f\u627e\u56fe\u4e2d\u7684\u73af\u548c\u7ebf\uff0c\u6211\u4eec\u5148\u8fdb\u884c\u5e76\u67e5\u96c6\u64cd\u4f5c\uff0c\u628a\u80fd\u8fde\u63a5\u6210\u73af\u7684\u90fd\u8fde\u63a5\u6210\u73af\uff0c\u5269\u4e0b\u7684\u4e0d\u80fd\u8fde\u63a5\u7684\u653e\u90a3\uff0c\u7136\u540e\u7528BFS\u641c\u7d22\u4e00\u4e0b\u8fde\u901a\u5757\u7684\u4e2a\u6570\uff0c\u7528\u8fde\u901a\u5757\u7684\u4e2a\u6570\u51cf\u53bb\u5df2\u7ecf\u6210\u73af\u7684\u4e2a\u6570\u5c31\u662f\u5269\u4e0b\u6ca1\u6709\u6210\u73af\u7684\u4e2a\u6570\u3002\u600e\u4e48\u5224\u65ad\u6210\u6ca1\u6210\u73af\uff0c\u5728\u8fdb\u884c\u5e76\u67e5\u96c6\u7684\u65f6\u5019\uff0c\u5982\u679c\u6211\u4eec\u53d1\u73b0\u4ed6\u4eec\u4fe9\u7684\u7236\u8282\u70b9\u662f\u540c\u4e00\u4e2a\uff0c\u90a3\u4e48\u5c31\u786e\u5b9a\u4ed6\u4eec\u5df2\u7ecf\u8fde\u63a5\u6210\u73af\u4e86\uff0c\u7136\u540eans1++\u3002 #include &lt;algorithm&gt; #include &lt;bitset&gt; #include &lt;cmath&gt; #include &lt;cstdio&gt; #include &lt;cstdlib&gt; #include &lt;cstring&gt; #include &lt;fstream&gt; #include &lt;iostream&gt; #include &lt;map&gt; #include &lt;queue&gt; #include &lt;set&gt; #include &lt;stack&gt; #include &lt;unordered_map&gt; #include &lt;vector&gt; using namespace std; #define x first #define y second #define endl &#039;\\n&#039; #define IOS \\ ios_base::sync_with_stdio(0); \\ cin.tie(0); \\ cout.tie(0); typedef long long [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[25,24,12,33],"class_list":["post-75","post","type-post","status-publish","format-standard","hentry","category-solution","tag-bfs","tag-dfs","tag-12","tag-33"],"_links":{"self":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/75","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=75"}],"version-history":[{"count":0,"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/75\/revisions"}],"wp:attachment":[{"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=75"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=75"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=75"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}