285B - Find Marble - CodeForces Solution


implementation *1200

Please click on ads to support us..

Python Code:

import math
import sys

n,s,t=map(int,input().split())

    
        arr=list(map(int,input().split()))
if(s==t):
    print(0)
    sys.exit()
i=0
while(arr[s-1]!=t and i<=n-1):
    s=arr[s-1]
    
    i+=1
if(arr[s-1]==t):

    print(i+1)
else:
    print(-1)

C++ Code:

#include <bits/stdc++.h>
using ll = long long;
#define Shalaby ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
using namespace std;
#define endl '\n'
#define Endl '\n'
#define YES cout << "YES\n";
#define NO cout << "NO\n";
#define nl cout << '\n';
#define all(v) v.begin(), v.end()
#define loop(a)       \
    for (auto &i : a) \
        cin >> i;
ll mod = 1e9 + 7;
bool isprime(ll n)
{
    for (int i = 2; i * i < n; i++)
    {
        if (n % i == 0)
            return false;
    }
    return true;
}
bool com(pair<int, int> s1, pair<int, int> s2)
{
    return s1.second < s2.second;
}
bool palindrom(string s)
{
    for (int i = 0; i < s.size() / 2; i++)
    {
        if (s[i] != s[s.size() - i - 1])
        {
            return false;
        }
    }
    return true;
}
vector<int> prime_factorization(ll n)
{
    vector<int> pf;
    for (int i = 2; i <= sqrt(n); i++)
    {
        while (n % i == 0)
        {
            pf.push_back(i);
            n /= i;
        }
    }
    if (n > 1)
        pf.push_back(n);
    return pf;
}
int searchInsert(vector<int> &nums, int target)
{
    int n = 0, m = nums.size() - 1, mid;
    while (n <= m)
    {
        mid = (n + m) / 2;
        cout << mid << " " << nums[mid] << endl;
        ;
        if (nums[mid] == target)
        {
            break;
        }
        else if (nums[mid] < target)
        {
            n = mid + 1;
        }
        else
            m = mid - 1;
    }
    return mid;
}
ll factorial(ll n)
{
    ll mul = 1;
    for (ll i = n; i <= n; i++)
    {
        mul *= i;
    }
    return mul;
}
// moduler binary exponention
ll Binary_Exponention(ll a, ll b, ll k) // fast power
{
    ll res = 1;
    while (b)
    {
        if (b % 2 == 1)
            res = ((res % mod) * (a % mod)) % mod;
        // a = Binary_Exponention(b, k);
        b /= 2;
    }
    return res;
}
ll no_zeros(ll n)
{
    ll ans = 0, ten = 1;
    while (n)
    {
        if (n % 10 != 0)
        {
            ans += ten * (n % 10);
            ten *= 10;
        }
        n /= 10;
    }
    return ans;
}
ll lcm(ll a, ll b)
{
    return (a / __gcd(a, b)) * b;
}
int divisorSum(int n)
{
    int sum = 0;

    for (int i = 1; i <= n; ++i)
    {

        // Find all divisors of i and add them
        for (int j = 1; j * j <= i; ++j)
        {
            if (i % j == 0)
            {
                if (i / j == j)
                    sum += j;
                else
                    sum += j + i / j;
            }
        }
    }
    return sum;
}
bool beaty(string s)
{
    for (int i = 1; i < s.size(); i++)
    {
        if (s[i] == s[i - 1])
            return false;
    }
    return true;
}
void solve()
{
    int n, s, t;
    cin >> n >> s >> t;
    int a[n], b[n + 1];
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    int ans = a[s-1];
    for (int i = 0; i < n; i++)
    {
        if (s == t)
        {
            cout << i << endl;
            return;
        }
        s = a[s-1];
    }
    cout << -1;
}
int main()
{
    Shalaby;
    int n = 1;
    // cin >> n;
    while (n--)
    {
        solve();
    }
}
/*

*/


Comments

Submit
0 Comments
More Questions

60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person