{"id":1417,"date":"2013-03-22T20:39:28","date_gmt":"2013-03-22T12:39:28","guid":{"rendered":"https:\/\/blog.rexdf.org\/?p=1417"},"modified":"2013-03-22T20:39:28","modified_gmt":"2013-03-22T12:39:28","slug":"codeforces-175-b","status":"publish","type":"post","link":"https:\/\/blog.rexdf.org\/ja\/2013\/03\/codeforces-175-b\/","title":{"rendered":"Codeforces 175 B"},"content":{"rendered":"<p>&nbsp;<\/p>\n<div>\n<div>B. Find Marble<\/div>\n<div>\n<div>time limit per test<\/div>\n<p>2 seconds<\/p><\/div>\n<div>\n<div>memory limit per test<\/div>\n<p>256 megabytes<\/p><\/div>\n<div>\n<div>input<\/div>\n<p>standard input<\/p><\/div>\n<div>\n<div>output<\/div>\n<p>standard output<\/p><\/div>\n<\/div>\n<div>\n<p>Petya and Vasya are playing a game. Petya&#8217;s got\u00a0<i>n<\/i>\u00a0non-transparent glasses, standing in a row. The glasses&#8217; positions are indexed with integers from\u00a01\u00a0to\u00a0<i>n<\/i>\u00a0from left to right. Note that the positions are indexed but the glasses are not.<\/p>\n<p>First Petya puts a marble under the glass in position\u00a0<i>s<\/i>. Then he performs some (possibly zero) shuffling operations. One shuffling operation means moving the glass from the first position to position\u00a0<i>p<\/i><sub>1<\/sub>, the glass from the second position to position\u00a0<i>p<\/i><sub>2<\/sub>\u00a0and so on. That is, a glass goes from position\u00a0<i>i<\/i>\u00a0to position\u00a0<i>p<\/i><sub><i>i<\/i><\/sub>. Consider all glasses are moving simultaneously during one shuffling operation. When the glasses are shuffled, the marble doesn&#8217;t travel from one glass to another: it moves together with the glass it was initially been put in.<\/p>\n<p>After all shuffling operations Petya shows Vasya that the ball has moved to position\u00a0<i>t<\/i>. Vasya&#8217;s task is to say what minimum number of shuffling operations Petya has performed or determine that Petya has made a mistake and the marble could not have got from position\u00a0<i>s<\/i>to position\u00a0<i>t<\/i>.<\/p>\n<\/div>\n<div>\n<div>Input<\/div>\n<p>The first line contains three integers:\u00a0<i>n<\/i>,\u2009<i>s<\/i>,\u2009<i>t<\/i>\u00a0(1\u2009\u2264\u2009<i>n<\/i>\u2009\u2264\u200910<sup>5<\/sup>;\u00a01\u2009\u2264\u2009<i>s<\/i>,\u2009<i>t<\/i>\u2009\u2264\u2009<i>n<\/i>)\u00a0\u2014 the number of glasses, the ball&#8217;s initial and final position. The second line contains\u00a0<i>n<\/i>\u00a0space-separated integers:\u00a0<i>p<\/i><sub>1<\/sub>,\u2009<i>p<\/i><sub>2<\/sub>,\u2009&#8230;,\u2009<i>p<\/i><sub><i>n<\/i><\/sub>\u00a0(1\u2009\u2264\u2009<i>p<\/i><sub><i>i<\/i><\/sub>\u2009\u2264\u2009<i>n<\/i>)\u00a0\u2014 the shuffling operation parameters. It is guaranteed that all\u00a0<i>p<\/i><sub><i>i<\/i><\/sub>&#8216;s are distinct.<\/p>\n<p>Note that\u00a0<i>s<\/i>\u00a0can equal\u00a0<i>t<\/i>.<\/p>\n<\/div>\n<div>\n<div>Output<\/div>\n<p>If the marble can move from position\u00a0<i>s<\/i>\u00a0to position\u00a0<i>t<\/i>, then print on a single line a non-negative integer \u2014 the minimum number of shuffling operations, needed to get the marble to position\u00a0<i>t<\/i>. If it is impossible, print number -1.<\/p>\n<\/div>\n<div>\n<div>Sample test(s)<\/div>\n<div>\n<div>\n<div>input<\/div>\n<pre>4 2 1 2 3 4 1<\/pre>\n<\/div>\n<div>\n<div>output<\/div>\n<pre>3<\/pre>\n<\/div>\n<div>\n<div>input<\/div>\n<pre>4 3 3 4 1 3 2<\/pre>\n<\/div>\n<div>\n<div>output<\/div>\n<pre>0<\/pre>\n<\/div>\n<div>\n<div>input<\/div>\n<pre>4 3 4 1 2 3 4<\/pre>\n<\/div>\n<div>\n<div>output<\/div>\n<pre>-1<\/pre>\n<\/div>\n<div>\n<div>input<\/div>\n<pre>3 1 3 2 1 3<\/pre>\n<\/div>\n<div>\n<div>output<\/div>\n<pre>-1<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<pre class=\"brush: cpp; gutter: true\">\r\n#include &lt;iostream&gt;\r\n#include &lt;cstdio&gt;\r\n#include &lt;cstring&gt;\r\n\r\nusing namespace std;\r\n\r\nint a[100005];\r\nint b[100005];\r\n\r\nint main()\r\n{\r\n    int n,s,t,tmp,ans=0,err=0;\r\n    cin&gt;&gt;n&gt;&gt;s&gt;&gt;t;\r\n    for(int i=1;i&lt;=n;i++)\r\n      cin&gt;&gt;a[i];\r\n    tmp=s;\r\n    while(t!=tmp)\r\n    {\r\n        if(b[tmp])\r\n        {\r\n            err=1;\r\n            t++;\r\n            cout\r\n            break;\r\n        }\r\n        b[tmp]=1;\r\n        tmp=a[tmp];\r\n        ans++;\r\n    }\r\n    if(err)cout&lt;&lt;-1&lt;&lt;endl;\r\n    else cout&lt;&lt;ans&lt;&lt;endl;\r\n    return 0;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; B. Find Marble time limit per tes &hellip; <a href=\"https:\/\/blog.rexdf.org\/ja\/2013\/03\/codeforces-175-b\/\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[125],"class_list":["post-1417","post","type-post","status-publish","format-standard","hentry","category-algorithm","tag-codeforces"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/blog.rexdf.org\/ja\/wp-json\/wp\/v2\/posts\/1417","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.rexdf.org\/ja\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.rexdf.org\/ja\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.rexdf.org\/ja\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.rexdf.org\/ja\/wp-json\/wp\/v2\/comments?post=1417"}],"version-history":[{"count":0,"href":"https:\/\/blog.rexdf.org\/ja\/wp-json\/wp\/v2\/posts\/1417\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.rexdf.org\/ja\/wp-json\/wp\/v2\/media?parent=1417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.rexdf.org\/ja\/wp-json\/wp\/v2\/categories?post=1417"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.rexdf.org\/ja\/wp-json\/wp\/v2\/tags?post=1417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}