{"id":660,"date":"2013-01-27T17:51:03","date_gmt":"2013-01-27T09:51:03","guid":{"rendered":"http:\/\/www.shuizilong.com\/house\/?p=660"},"modified":"2023-06-06T01:37:43","modified_gmt":"2023-06-05T17:37:43","slug":"spoj-1482-a-short-vacation-in-disneyland","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/house\/archives\/spoj-1482-a-short-vacation-in-disneyland\/","title":{"rendered":"SPOJ 1482. A short vacation in Disneyland"},"content":{"rendered":"<h3>Brief description: <\/h3>\n<p>\u3002\u3002\u6811\u7684\u8def\u5f84\u8986\u76d6\u3002<\/p>\n<p><!--more--><\/p>\n<h3>Analysis: <\/h3>\n<pre class=\"brush: cpp; light: false; title: ; toolbar: true; notranslate\" title=\"\">\r\nconst int N = 10009;\r\n\r\nint hd&#x5B;N], suc&#x5B;N*2], to&#x5B;N*2], deg&#x5B;N]; VI adj&#x5B;N];\r\nint f0&#x5B;N], f1&#x5B;N], p0&#x5B;N], p1&#x5B;N]; bool vis&#x5B;N];\r\nint n;\r\n\r\n#define v to&#x5B;i]\r\nvoid dfs(int p = 0, int u = 1){\r\n\r\n    REP_G(i, u) if (v != p){\r\n        dfs(u, v), f0&#x5B;u] += f0&#x5B;v];\r\n    }\r\n\r\n    if (!f0&#x5B;u]){ \/\/ is leaf .. .\r\n        f0&#x5B;u] = f1&#x5B;u] = 1;\r\n    }\r\n    else {\r\n        f1&#x5B;u] = f0&#x5B;u];\r\n\r\n        int t1 = INF; REP_G(i, u) if (v != p){\r\n            if (f1&#x5B;v] - f1&#x5B;v] &lt; t1){\r\n                t1 = f1&#x5B;v] - f0&#x5B;v];\r\n                p1&#x5B;u] = v;\r\n            }\r\n        }\r\n\r\n        int t0 = INF; REP_G(i, u) if (v != p &amp;&amp; v != p1&#x5B;u]){\r\n            if (f1&#x5B;v] - f1&#x5B;v] &lt; t0){\r\n                t0 = f1&#x5B;v] - f0&#x5B;v];\r\n                p0&#x5B;u] = v;\r\n            }\r\n        }\r\n        f1&#x5B;u] += t1, f0&#x5B;u] += t0 + t1 - 1;\r\n        checkMin(f0&#x5B;u], f1&#x5B;u]);\r\n    }\r\n}\r\n\r\nvoid gao(int u = 1, bool flag = false){ \/\/ \u662f\u5426\u5141\u8bb8\u5206\u5c94\u3002\r\n\r\n    vis&#x5B;u] = true;\r\n\r\n    if (p0&#x5B;u] &amp;&amp; !flag){\r\n        adj&#x5B;u].PB(p0&#x5B;u]);\r\n        adj&#x5B;p0&#x5B;u]].PB(u);\r\n        gao(p0&#x5B;u], 1);\r\n    }\r\n\r\n    if (p1&#x5B;u]){\r\n        adj&#x5B;u].PB(p1&#x5B;u]);\r\n        adj&#x5B;p1&#x5B;u]].PB(u);\r\n        gao(p1&#x5B;u], 1);\r\n    }\r\n\r\n    REP_G(i, u) if (!vis&#x5B;v]) gao(v) ;\r\n}\r\n\r\n\r\n#undef v\r\n#define v (*it)\r\nvoid out(int u){\r\n    vis&#x5B;u] = true; ECH(it, adj&#x5B;u]) if (!vis&#x5B;v]){\r\n        printf(&quot; %d&quot; , v);\r\n        out(v) ;\r\n    }\r\n}\r\n\r\nint main(){\r\n\r\n#ifndef ONLINE_JUDGE\r\n    freopen(&quot;in.txt&quot;, &quot;r&quot;, stdin);\r\n    \/\/freopen(&quot;out.txt&quot;, &quot;w&quot;, stdout);\r\n#endif\r\n\r\n#define a to&#x5B;i^1]\r\n#define b to&#x5B;i]\r\n\r\n    Rush{\r\n\r\n        RST(hd, f0, f1, p0, p1);\r\n\r\n        FOR_C(i, 2, RD(n)&lt;&lt;1){\r\n            RD(a, b);\r\n            suc&#x5B;i] = hd&#x5B;a], hd&#x5B;a] = i++;\r\n            suc&#x5B;i] = hd&#x5B;a], hd&#x5B;a] = i;\r\n        }\r\n\r\n        dfs(); OT(f0&#x5B;1]);\r\n\r\n        REP_1(i, n) CLR(adj&#x5B;i]);\r\n\r\n        RST(vis); gao(); RST(vis); REP_1(u, n) if (!vis&#x5B;u] &amp;&amp; SZ(adj&#x5B;u]) &lt;= 1){\r\n            printf(&quot;%d&quot;, u); out(u);\r\n            puts(&quot;&quot;);\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<pre class=\"brush: cpp; light: false; title: ; toolbar: true; notranslate\" title=\"\">\r\nconst int N = 10009;\r\n\r\nint hd&#x5B;N], suc&#x5B;N*2], to&#x5B;N*2], deg&#x5B;N];\r\nbool vis&#x5B;N];\r\nint n;\r\n\r\n#define v to&#x5B;i]\r\n\r\nVVI path; VI cur;\r\n\r\nvoid gao(int u){\r\n    vis&#x5B;u] = true; cur.PB(u);\r\n    REP_G(i, u) if (!vis&#x5B;v]) gao(v);\r\n}\r\n\r\nvoid dfs(int p = 0, int u = 1){\r\n\r\n    \/\/cout &lt;&lt; &quot; &quot; &lt;&lt; u &lt;&lt; endl;\r\n\r\n    REP_G(i, u) if (v != p){\r\n        dfs(u, v);\r\n    }\r\n\r\n    \/\/cout &lt;&lt; u &lt;&lt; &quot; &quot; &lt;&lt; deg&#x5B;u] &lt;&lt; endl;\r\n\r\n    if (deg&#x5B;u] &lt;= 1 &amp;&amp; p) ++deg&#x5B;p];\r\n    else {\r\n        int c = 0; CLR(cur); vis&#x5B;u] = true;\r\n        REP_G(i, u) if (v != p &amp;&amp; deg&#x5B;v] &lt;= 1){\r\n\r\n            \/*cout &lt;&lt; &quot;--&quot; &lt;&lt; u &lt;&lt; &quot; &quot; &lt;&lt; v &lt;&lt; endl;\r\n            cout &lt;&lt; &quot;--&quot;; REP(i, SZ(cur)) cout &lt;&lt; cur&#x5B;i] &lt;&lt; &quot; &quot;;\r\n            cout &lt;&lt; endl;*\/\r\n\r\n            if (!c) gao(v), reverse(ALL(cur)), cur.PB(u);\r\n            else if (c == 1) gao(v);\r\n            else{\r\n                if (SZ(cur)) path.PB(cur), CLR(cur);\r\n                gao(v);\r\n            }\r\n            ++c;\r\n        }\r\n        \/\/cout &lt;&lt; c &lt;&lt; endl;\r\n\r\n        if (SZ(cur)) path.PB(cur);\r\n        else if (!c) cur.PB(u), path.PB(cur);\r\n    }\r\n}\r\n\r\n\r\nint main(){\r\n\r\n#ifndef ONLINE_JUDGE\r\n    freopen(&quot;in.txt&quot;, &quot;r&quot;, stdin);\r\n    \/\/freopen(&quot;out.txt&quot;, &quot;w&quot;, stdout);\r\n#endif\r\n\r\n#define a to&#x5B;i^1]\r\n#define b to&#x5B;i]\r\n\r\n    Rush{\r\n\r\n        RST(hd, deg, vis); CLR(path);\r\n\r\n        FOR_C(i, 2, RD(n)&lt;&lt;1){\r\n            RD(a, b);\r\n            suc&#x5B;i] = hd&#x5B;a], hd&#x5B;a] = i++;\r\n            suc&#x5B;i] = hd&#x5B;a], hd&#x5B;a] = i;\r\n        }\r\n\r\n        dfs();\r\n\r\n        OT(SZ(path)); REP(i, SZ(path)){\r\n            printf(&quot;%d&quot;, path&#x5B;i]&#x5B;0]); FOR(j, 1, SZ(path&#x5B;i])) printf(&quot; %d&quot;, path&#x5B;i]&#x5B;j]);\r\n            puts(&quot;&quot;);\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>\uff08\u65e0\u8bba\u4ece\u54ea\u65b9\u9762\u770b\u3002\u3002\u8fd9\u9898\u4f3c\u4e4e\u3002\u3002\u8d2a\u5fc3\u90fd\u8981\u66f4\u4f18\u8d8a\u4e00\u4e9b\u3002\u3002\/w\\\u3002\u3002<\/p>\n<h3>External link: <\/h3>\n<p><a href=\"http:\/\/www.spoj.com\/problems\/PT07F\/\">http:\/\/www.spoj.com\/problems\/PT07F\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Brief description: \u3002\u3002\u6811\u7684\u8def\u5f84\u8986\u76d6\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[106,21],"tags":[],"class_list":["post-660","post","type-post","status-publish","format-standard","hentry","category-play-with-trees","category-spoj"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2tdP7-aE","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/660","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/comments?post=660"}],"version-history":[{"count":3,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/660\/revisions"}],"predecessor-version":[{"id":2986,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/660\/revisions\/2986"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/media?parent=660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/categories?post=660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/tags?post=660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}