{"id":153,"date":"2023-04-25T20:24:29","date_gmt":"2023-04-25T12:24:29","guid":{"rendered":"https:\/\/solost.cn\/?p=153"},"modified":"2023-04-25T20:24:29","modified_gmt":"2023-04-25T12:24:29","slug":"265","status":"publish","type":"post","link":"https:\/\/solost.cn\/?p=153","title":{"rendered":"Codeforces Round 237 (Div. 2) &#8211; C. Restore Graph"},"content":{"rendered":"<h2>\u9898\u76ee\u94fe\u63a5<\/h2>\n<p><a href=\"https:\/\/codeforces.com\/problemset\/problem\/404\/C\">\u94fe\u63a5<\/a><\/p>\n<h2>\u9898\u76ee\u5927\u610f<\/h2>\n<p>\u6709n\u4e2a\u623f\u5b50\uff0c\u6bcf\u4e2a\u623f\u5b50\u5230\u5176\u4ed6\u623f\u5b50\u6700\u591a\u6709k\u6761\u8def\uff0c\u7ed9\u4f60\u7ed9\u51fa\u4e00\u680b\u623f\u5b50\u5230\u5176\u4ed6\u623f\u5b50\u7684\u6700\u77ed\u8ddd\u79bb\uff0c\u8ba9\u4f60\u6784\u9020\u4e00\u4e2a\u56fe\uff0c\u95ee\u4f60\u8fd9\u5f20\u56fe\u6784\u9020\u51fa\u6765\u662f\u4ec0\u4e48\u6837\u7684\uff0c\u8f93\u51faa,b\u5373a-b\u8fb9\u957f\u4e3a1\u7684\u8def\u53ca\u6709\u591a\u5c11\u6761\u8fd9\u6837\u7684\u8fb9.<\/p>\n<h2>\u9898\u89e3<\/h2>\n<p>\u521a\u5f00\u59cb\u60f3\u7684\u662fdfs\u6df1\u641c,\u4f46\u662f\u4e0d\u77e5\u9053\u4e3a\u5565re7,\u7136\u540e\u6362\u4e86\u4e00\u79cd\u5199\u6cd5\u3002\u6700\u540e\u5199\u7684\u4ee3\u7801\u771f\u7684\u8ddf\u8bd7\u4e00\u6837\u4f18\u96c5\u3002<\/p>\n<ul>\n<li>\u5148\u6392\u5e8f<\/li>\n<li>\u7136\u540e\u53cc\u6307\u9488<br \/>\u4ece\u5f53\u524d\u70b9\u5f00\u59cb\u5f80\u540e\u626b\uff0c\u626b\u5230\u4e00\u4e2a\u8ddd\u79bb\u5927\u4e8e\u5f53\u524d\u70b91\u7684\u70b9\uff0c\u7136\u540e\u52a0\u5230\u7b2ci\u4e2a\u70b9\u91cc\uff0c\u7136\u540e\u4e0d\u8981\u8d85\u8fc7\u8fd9\u4e2a\u70b9\u7684\u5ea6\u3002\u521a\u5f00\u59cb\u7684\u70b9\u5373\u4ed6\u81ea\u5df1\u5bb6\uff0c\u51fa\u5ea6\u53ef\u4ee5\u4e3ak,\u5176\u4ed6\u7531\u4e8e\u4ed6\u6709\u4e14\u53ea\u6709\u4e00\u4e2a\u7236\u8282\u70b9\uff0c\u7136\u540e\u51fa\u5ea6\u53ea\u80fd\u662fk-1\uff0c\u5224\u65ad\u4e00\u4e0b\u5373\u53ef\u3002\u6ce8\u610f\uff0c\u4ed6\u53ea\u6709\u4e00\u4e2a\u5bb6\uff0c\u8fd8\u6709\u6bcf\u4e2a\u70b9\u9664\u4e86\u5bb6\u5b83\u5fc5\u987b\u88ab\u7528\u6389\u3002<\/li>\n<\/ul>\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;unordered_set&gt;\n#include &lt;vector&gt;\n\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);\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair&lt;ll, ll&gt; pll;\ntypedef pair&lt;int, int&gt; pii;\ntypedef unsigned long long ull;\n\nconst int INF = 0x3f3f3f3f, mod = 1000000007;\nconst int N = 2e5 + 10;\n\nint n, k, sum, ed;\npii a[N];\nint cnt[N];\nvector&lt;pii&gt; q[N];\nvoid solve()\n{\n    cin &gt;&gt; n &gt;&gt; k;\n    for (int i = 1; i &lt;= n; i++) {\n        int x;\n        cin &gt;&gt; x;\n        cnt[x]++;\n        a[i] = {x, i};\n        ed = max(ed, x);\n    }\n    sort(a + 1, a + n + 1);\n    if(!a[2].x){\n        cout &lt;&lt; &quot;-1\\n&quot;;\n        return;\n    }\n    for (int i = 1, j = 1; i &lt;= n &amp;&amp; j &lt;= n; i++) {\n        while (j &lt;= n &amp;&amp; a[j].x == a[i].x) j++;\n        for (int h = 1; h &lt;= k - (i != 1) &amp;&amp; j &lt;= n; j++, h++) {\n            if (a[j].x == a[i].x + 1) {\n                sum++;\n                cnt[a[j].x]--;\n                q[a[i].y].push_back({a[i].y, a[j].y});\n            }\n            else break;\n        }\n    }\n    for(int i = 1; i &lt;= ed; i++){\n        if(cnt[i]){\n            cout &lt;&lt; &quot;-1\\n&quot;;\n            return;\n        }\n    }\n    cout &lt;&lt; sum &lt;&lt; endl;\n    for(int i = 0; i &lt;= n; i++){\n        for(auto [x, y] : q[i]){\n            cout &lt;&lt; x &lt;&lt; &quot; &quot; &lt;&lt; y &lt;&lt; endl;\n        }\n    }\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>\u9898\u76ee\u94fe\u63a5 \u94fe\u63a5 \u9898\u76ee\u5927\u610f \u6709n\u4e2a\u623f\u5b50\uff0c\u6bcf\u4e2a\u623f\u5b50\u5230\u5176\u4ed6\u623f\u5b50\u6700\u591a\u6709k\u6761\u8def\uff0c\u7ed9\u4f60\u7ed9\u51fa\u4e00\u680b\u623f\u5b50\u5230\u5176\u4ed6\u623f\u5b50\u7684\u6700\u77ed\u8ddd\u79bb\uff0c\u8ba9\u4f60\u6784\u9020\u4e00\u4e2a\u56fe\uff0c\u95ee\u4f60\u8fd9\u5f20\u56fe\u6784\u9020\u51fa\u6765\u662f\u4ec0\u4e48\u6837\u7684\uff0c\u8f93\u51faa,b\u5373a-b\u8fb9\u957f\u4e3a1\u7684\u8def\u53ca\u6709\u591a\u5c11\u6761\u8fd9\u6837\u7684\u8fb9. \u9898\u89e3 \u521a\u5f00\u59cb\u60f3\u7684\u662fdfs\u6df1\u641c,\u4f46\u662f\u4e0d\u77e5\u9053\u4e3a\u5565re7,\u7136\u540e\u6362\u4e86\u4e00\u79cd\u5199\u6cd5\u3002\u6700\u540e\u5199\u7684\u4ee3\u7801\u771f\u7684\u8ddf\u8bd7\u4e00\u6837\u4f18\u96c5\u3002 \u5148\u6392\u5e8f \u7136\u540e\u53cc\u6307\u9488\u4ece\u5f53\u524d\u70b9\u5f00\u59cb\u5f80\u540e\u626b\uff0c\u626b\u5230\u4e00\u4e2a\u8ddd\u79bb\u5927\u4e8e\u5f53\u524d\u70b91\u7684\u70b9\uff0c\u7136\u540e\u52a0\u5230\u7b2ci\u4e2a\u70b9\u91cc\uff0c\u7136\u540e\u4e0d\u8981\u8d85\u8fc7\u8fd9\u4e2a\u70b9\u7684\u5ea6\u3002\u521a\u5f00\u59cb\u7684\u70b9\u5373\u4ed6\u81ea\u5df1\u5bb6\uff0c\u51fa\u5ea6\u53ef\u4ee5\u4e3ak,\u5176\u4ed6\u7531\u4e8e\u4ed6\u6709\u4e14\u53ea\u6709\u4e00\u4e2a\u7236\u8282\u70b9\uff0c\u7136\u540e\u51fa\u5ea6\u53ea\u80fd\u662fk-1\uff0c\u5224\u65ad\u4e00\u4e0b\u5373\u53ef\u3002\u6ce8\u610f\uff0c\u4ed6\u53ea\u6709\u4e00\u4e2a\u5bb6\uff0c\u8fd8\u6709\u6bcf\u4e2a\u70b9\u9664\u4e86\u5bb6\u5b83\u5fc5\u987b\u88ab\u7528\u6389\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;unordered_set&gt; #include &lt;vector&gt; #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); [&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":[55,5,9],"class_list":["post-153","post","type-post","status-publish","format-standard","hentry","category-solution","tag-55","tag-5","tag-9"],"_links":{"self":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/153","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=153"}],"version-history":[{"count":0,"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/153\/revisions"}],"wp:attachment":[{"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}