Hacker Rank Minimum Edges in a Layered Graph

/*
Lionel Messi is such a player that you may catch him, you may touch him, you may feel him
and definitely you may Love him.
Lionel Messi is Messi. A little Magician in this World.

*/

#include<bits/stdc++.h>

using namespace std;

#define fast ios_base::sync_with_stdio(0)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define nl puts("")
#define psb push_back

typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef unsigned long long ull;

const int mod = 1000007;
const int high = 100;

int main()
{
    fast;
    int n , k;
    while(cin >> n >> k)
    {
        if(n < k) outn(-1);
        else if(n == k) outn(n-1);
        else if(n > k)
        {
            if(k == 2) outn(-1);
            else
            {
                outn((k - 1) + (2 * (n - k))); // every vertex is connected to another for one time
            }
        }
    }

    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number