Sort by

recency

|

17 Discussions

|

  • + 0 comments

    Here is my solution in java, javascript, python, C, C++, Csharp HackerRank ByteLandian Tours Problem Solution

  • + 0 comments

    Here is the solution of ByteLandian Tours Click Here

  • + 1 comment

    Can it will be helpful in my Island hopping business? It tour base system.

  • + 1 comment

    https://zeroplusfour.com/bytelandian-tours-hackerrank-solution/

    Here's how I did in all languages Java 8 , C++ , C , Python 3, Python 2.

  • + 0 comments

    C++ Solution

    #include <algorithm>
    #include <cstdio>
    #include <vector>
    using namespace std;
    #define FOR(i, a, b) for (int i = (a); i < (b); i++)
    #define REP(i, n) FOR(i, 0, n)
    #define pb push_back
    typedef long long ll;
    
    int ri()
    {
      int x;
      scanf("%d", &x);
      return x;
    }
    
    const int N = 10000, MOD = 1000000007;
    int fac[N];
    vector<int> e[N];
    
    int f(int n)
    {
      int path = n, ans = 1;
      REP(i, n)
        if (e[i].size() == 1)
          path--;
      REP(i, n)
        if (e[i].size() > 1) {
          int leaf = 0, inner = 0;
          for (auto v: e[i])
            if (e[v].size() == 1)
              leaf++;
            else
              inner++;
          if (inner > 2)
            return 0;
          ans = ll(ans)*fac[leaf]%MOD;
        }
      return path == 1 ? ans : ans*2%MOD;
    }
    
    int main()
    {
      for (int cc = ri(); cc--; ) {
        int n = ri();
        REP(i, n)
          e[i].clear();
        fac[0] = 1;
        FOR(i, 1, n)
          fac[i] = ll(fac[i-1])*i%MOD;
        REP(i, n-1) {
          int x = ri(), y = ri();
          e[x].pb(y);
          e[y].pb(x);
        }
        printf("%d\n", f(n));
      }
    }