{"id":1272,"date":"2016-08-26T15:56:07","date_gmt":"2016-08-26T12:56:07","guid":{"rendered":"http:\/\/java.mazurok.com\/?p=1272"},"modified":"2016-09-02T15:26:55","modified_gmt":"2016-09-02T12:26:55","slug":"e-olymp-982","status":"publish","type":"post","link":"https:\/\/java.mazurok.com\/?p=1272","title":{"rendered":"e-olymp 982. \u0421\u0432\u044f\u0437\u043d\u043e\u0441\u0442\u044c"},"content":{"rendered":"<p><strong>\u0417\u0430\u0434\u0430\u0447\u0430.<\/strong> \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u0439 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0433\u0440\u0430\u0444 \u0441\u0432\u044f\u0437\u043d\u044b\u043c, \u0442\u043e \u0435\u0441\u0442\u044c \u0447\u0442\u043e \u0438\u0437 \u043b\u044e\u0431\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043c\u043e\u0436\u043d\u043e \u043f\u043e \u0440\u0451\u0431\u0440\u0430\u043c \u044d\u0442\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u043f\u043e\u043f\u0430\u0441\u0442\u044c \u0432 \u043b\u044e\u0431\u0443\u044e \u0434\u0440\u0443\u0433\u0443\u044e.<\/p>\n<p><strong>\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435<\/strong><\/p>\n<p>\u0412 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0437\u0430\u0434\u0430\u043d\u044b \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0435\u0440\u0448\u0438\u043d <img decoding=\"async\" class=\"latex\" title=\"n\" src=\"http:\/\/s0.wp.com\/latex.php?latex=n&amp;bg=292625&amp;fg=b29d85&amp;s=0\" alt=\"n\" \/> \u0438 \u0440\u0435\u0431\u0435\u0440 <img decoding=\"async\" class=\"latex\" title=\"m\" src=\"http:\/\/s0.wp.com\/latex.php?latex=m&amp;bg=292625&amp;fg=b29d85&amp;s=0\" alt=\"m\" \/> \u0432 \u0433\u0440\u0430\u0444\u0435 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e <img decoding=\"async\" class=\"latex\" title=\"(1 \\leq n \\leq 100, 1 \\leq m \\leq 10000)\" src=\"http:\/\/s0.wp.com\/latex.php?latex=%281+%5Cleq+n+%5Cleq+100%2C+1+%5Cleq+m+%5Cleq+10000%29&amp;bg=292625&amp;fg=b29d85&amp;s=0\" alt=\"(1 \\leq n \\leq 100, 1 \\leq m \\leq 10000)\" \/>. \u041a\u0430\u0436\u0434\u0430\u044f \u0438\u0437 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 m \u0441\u0442\u0440\u043e\u043a \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043f\u043e \u0434\u0432\u0430 \u0447\u0438\u0441\u043b\u0430 <img decoding=\"async\" class=\"latex\" title=\"u_i\" src=\"http:\/\/s0.wp.com\/latex.php?latex=u_i&amp;bg=292625&amp;fg=b29d85&amp;s=0\" alt=\"u_i\" \/> \u0438 <img decoding=\"async\" class=\"latex\" title=\"v_i\" src=\"http:\/\/s0.wp.com\/latex.php?latex=v_i&amp;bg=292625&amp;fg=b29d85&amp;s=0\" alt=\"v_i\" \/> <img decoding=\"async\" class=\"latex\" title=\"(1 \\leq u_i, v_i \\leq n);\" src=\"http:\/\/s0.wp.com\/latex.php?latex=%281+%5Cleq+u_i%2C+v_i+%5Cleq+n%29%3B&amp;bg=292625&amp;fg=b29d85&amp;s=0\" alt=\"(1 \\leq u_i, v_i \\leq n);\" \/> \u00a0\u043a\u0430\u0436\u0434\u0430\u044f \u0442\u0430\u043a\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u0432 \u0433\u0440\u0430\u0444\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0440\u0435\u0431\u0440\u043e \u043c\u0435\u0436\u0434\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0430\u043c\u0438 <img decoding=\"async\" class=\"latex\" title=\"u_i\" src=\"http:\/\/s0.wp.com\/latex.php?latex=u_i&amp;bg=292625&amp;fg=b29d85&amp;s=0\" alt=\"u_i\" \/> \u0438 <img decoding=\"async\" class=\"latex\" title=\"v_i\" src=\"http:\/\/s0.wp.com\/latex.php?latex=v_i&amp;bg=292625&amp;fg=b29d85&amp;s=0\" alt=\"v_i\" \/>.<br \/>\n<strong><br \/>\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435<\/strong><\/p>\n<p>\u0412\u044b\u0432\u0435\u0434\u0438\u0442\u0435 <strong>\u00abYES\u00bb<\/strong>, \u0435\u0441\u043b\u0438 \u0433\u0440\u0430\u0444 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0432\u044f\u0437\u043d\u044b\u043c \u0438 <strong>\u00abNO\u00bb<\/strong> \u0432 \u043f\u0440\u043e\u0442\u0438\u0432\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435.<\/p>\n<p><strong>\u0422\u0435\u0441\u0442\u044b<\/strong><\/p>\n<table>\n<tbody>\n<tr>\n<td>Test<\/td>\n<td>Input<\/td>\n<td>Output<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>4 2<br \/>\n1 2<br \/>\n3 4<\/td>\n<td>NO<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>5 4<br \/>\n1 2<br \/>\n5 3<br \/>\n4 5<br \/>\n3 4<\/td>\n<td>NO<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>5 4<br \/>\n1 2<br \/>\n5 1<br \/>\n3 5<br \/>\n4 3<\/td>\n<td>YES<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u041a\u043e\u0434 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b<\/strong><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:java decode:true\">import java.util.*;\r\n \r\nclass Graph {\r\n    public static void main(String[] args) {\r\n        int n, m;\r\n        Scanner scanner = new Scanner(System.in);\r\n        n = scanner.nextInt();\r\n        m = scanner.nextInt();\r\n \r\n        ArrayList&lt;Integer&gt; gr[] = new ArrayList[n];\r\n        for (int i = 0; i &lt; n; i++) {\r\n            gr[i] = new ArrayList&lt;&gt;();\r\n        }\r\n \r\n        int s = 0;\r\n        for (int i = 0; i &lt; m; i++) {\r\n            int u, v;\r\n            u = scanner.nextInt();\r\n            v = scanner.nextInt();\r\n            gr[u - 1].add(v - 1);\r\n            gr[v - 1].add(u - 1);\r\n        }\r\n\r\n        LinkedList&lt;Integer&gt; queue = new LinkedList&lt;&gt;();\r\n        queue.push(s);\r\n        boolean used[] = new boolean[n];\r\n        used[s] = true;\r\n \r\n        while (!queue.isEmpty()) {\r\n            int v = queue.peek();\r\n            queue.pop();\r\n            for (int i = 0; i &lt; gr[v].size(); ++i) {\r\n                int to = gr[v].get(i);\r\n                if (!used[to]) {\r\n                    used[to] = true;\r\n                    queue.push(to);\r\n                }\r\n            }\r\n        }\r\n        String answer = \"YES\";\r\n        for (int i = 0; i &lt; used.length; i++) {\r\n            if (used[i] == false) {\r\n                answer = \"NO\";\r\n            }\r\n        }\r\n        System.out.println(answer);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c<\/strong><\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0433\u0440\u0430\u0444 \u0441\u0432\u044f\u0437\u043d\u044b\u043c, \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0430 \u0443\u0434\u043e\u0431\u043d\u044b\u0439 \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u043e\u0438\u0441\u043a\u0430 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443. \u041e\u043d \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c: \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u0432\u0435\u0440\u0448\u0438\u043d\u044b, \u043c\u044b \u043f\u043e\u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e \u043f\u0440\u043e\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u043c \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b, \u0441\u043e\u0441\u0435\u0434\u043d\u0438\u0435 \u0441 \u043d\u0435\u0439. \u041a\u0430\u0436\u0434\u0443\u044e \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043c\u044b \u043f\u043e\u043c\u0435\u0447\u0430\u0435\u043c \u043c\u0430\u0440\u043a\u0435\u0440\u043e\u043c. \u0417\u0430\u0442\u0435\u043c \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u043c \u044d\u0442\u043e\u0442 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0438\u0437\u00a0\u0441\u043e\u0441\u0435\u0434\u043d\u0438\u0445\u00a0\u0432\u0435\u0440\u0448\u0438\u043d, \u0438 \u0442\u0430\u043a \u0434\u0430\u043b\u0435\u0435. \u041f\u043e\u0438\u0441\u043a \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c\u0441\u044f, \u043f\u043e\u043a\u0430 \u043c\u044b \u043d\u0435 \u043e\u0431\u043e\u0439\u0434\u0435\u043c \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0441\u0442\u0438\u0433\u043d\u0443\u0442\u044c \u0438\u0437 \u0434\u0430\u043d\u043d\u043e\u0439. \u0415\u0441\u043b\u0438 \u043f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e\u00a0\u0432 \u0433\u0440\u0430\u0444\u0435\u00a0\u043e\u0441\u0442\u0430\u043b\u0430\u0441\u044c \u0445\u043e\u0442\u044f \u0431\u044b \u043e\u0434\u043d\u0430\u00a0\u043d\u0435 \u043f\u043e\u043c\u0435\u0447\u0435\u043d\u043d\u0430\u044f\u00a0\u0432\u0435\u0440\u0448\u0438\u043d\u0430, \u0437\u043d\u0430\u0447\u0438\u0442 \u0438\u0437 \u043d\u0435\u0435 \u043d\u0435\u043b\u044c\u0437\u044f \u043f\u043e\u043f\u0430\u0441\u0442\u044c \u0432 \u043f\u043e\u043c\u0435\u0447\u0435\u043d\u043d\u044b\u0435, \u0442\u043e \u0435\u0441\u0442\u044c \u0433\u0440\u0430\u0444 \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0432\u044f\u0437\u043d\u044b\u043c. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u043d\u0435\u0432\u0430\u0436\u043d\u043e, \u0441 \u043a\u0430\u043a\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043f\u043e\u0438\u0441\u043a, \u0432\u0435\u0434\u044c \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u0430\u043c \u0444\u0430\u043a\u0442, \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0433\u0440\u0430\u0444 \u0438\u043b\u0438 \u043d\u0435\u0442.<\/p>\n<p>&nbsp;<br \/>\n\u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 <a href=\"http:\/\/ideone.com\/dG0kxm\"> Ideone <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0417\u0430\u0434\u0430\u0447\u0430. \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u0439 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0433\u0440\u0430\u0444 \u0441\u0432\u044f\u0437\u043d\u044b\u043c, \u0442\u043e \u0435\u0441\u0442\u044c \u0447\u0442\u043e \u0438\u0437 \u043b\u044e\u0431\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043c\u043e\u0436\u043d\u043e \u043f\u043e \u0440\u0451\u0431\u0440\u0430\u043c \u044d\u0442\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u043f\u043e\u043f\u0430\u0441\u0442\u044c \u0432 \u043b\u044e\u0431\u0443\u044e \u0434\u0440\u0443\u0433\u0443\u044e. \u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0412 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0437\u0430\u0434\u0430\u043d\u044b \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0435\u0440\u0448\u0438\u043d \u0438 \u0440\u0435\u0431\u0435\u0440 \u0432 \u0433\u0440\u0430\u0444\u0435 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e . \u041a\u0430\u0436\u0434\u0430\u044f \u0438\u0437 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 m \u0441\u0442\u0440\u043e\u043a \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043f\u043e \u0434\u0432\u0430 \u0447\u0438\u0441\u043b\u0430 \u0438 \u00a0\u043a\u0430\u0436\u0434\u0430\u044f \u0442\u0430\u043a\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u0432 \u0433\u0440\u0430\u0444\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 &hellip; <a href=\"https:\/\/java.mazurok.com\/?p=1272\" class=\"more-link\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":84,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[109],"tags":[178,180,179],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/java.mazurok.com\/index.php?rest_route=\/wp\/v2\/posts\/1272"}],"collection":[{"href":"https:\/\/java.mazurok.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java.mazurok.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java.mazurok.com\/index.php?rest_route=\/wp\/v2\/users\/84"}],"replies":[{"embeddable":true,"href":"https:\/\/java.mazurok.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1272"}],"version-history":[{"count":1,"href":"https:\/\/java.mazurok.com\/index.php?rest_route=\/wp\/v2\/posts\/1272\/revisions"}],"predecessor-version":[{"id":1273,"href":"https:\/\/java.mazurok.com\/index.php?rest_route=\/wp\/v2\/posts\/1272\/revisions\/1273"}],"wp:attachment":[{"href":"https:\/\/java.mazurok.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java.mazurok.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java.mazurok.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}