Skip to main content

All Solution Code of CodeChef May Long Challenge 2021

competitve programming solution here...

solution code of Tree House problem number-6 of may long challenge codechef

 Tree House Problem Solution Code: CodeChef

Solution Code

#pragma GCC optimize("Ofast", "unroll-loops")
#include <bits/stdc++.h>
using namespace std;

#define int long long int
#define double long double
using pii = pair<intint>;
template <typename T>
using Prior = std::priority_queue<Tvector<T>, greater<T>>;

#define X first
#define Y second
#define eb emplace_back
#define ALL(xbegin(x), end(x)
#define RALL(xrbegin(x), rend(x)
#define fastIO() ios_base::sync_with_stdio, cin.tie(0)

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

inline int getRand(int Lint R)
{
    if (L > R)
        swap(LR);
    return (int)(rng() % (uint64_t)(R - L + 1+ L);
}
template <typename T1typename T2>
ostream &operator<<(ostream &ospair<T1T2p)
{
    os << "(" << p.first << "," << p.second << ")";
    return os;
}

template <typename T>
ostream &operator<<(ostream &osvector<Tvec)
{
    for (int i = 0; i < vec.size(); ++i)
    {
        if (i)
            os << " ";
    }
    return os;
}

const int maxn = 3E5 + 5;
const int mod = 1E9 + 7;

vector<int> adj[maxn], subval, val;
vector<pii> ch;

void dfs(int nowint lst = -1)
{
    for (auto x : adj[now])
    {
        if (x == lst)
            continue;
        dfs(x, now);
    }
    ch.clear();
    for (auto x : adj[now])
    {
        if (x != lst)
            ch.eb(subval[x], x);
    }
    sort(RALL(ch));

    int tok = 1;
    for (auto [_val, id] : ch)
        val[id] = tok++;
    for (auto x : adj[now])
    {
        if (x != lst)
            subval[now+= val[x] * subval[x];
    }
}

void solve()
{
    int N, X;
    cin >> N >> X;
    subval.assign(N, 1), val.assign(N, 0);
    for (int i = 0; i < N; ++i)
        vector<int>().swap(adj[i]);

    for (int i = 0; i < N - 1++i)
    {
        int u, v;
        cin >> u >> v, --u, --v;
        adj[u].eb(v), adj[v].eb(u);
    }

    dfs(0);

    cout << subval[0% mod * X % mod << endl;
}

int32_t main()
{
    fastIO();

    int t;
    cin >> t;
    for (int _ = 1; _ <= t; ++_)
    {
        solve();
    }
    return 0;
}

Comments

Post a Comment

Popular posts from this blog

solution code of Modular Equation problem number-5 of may long challenge codechef

 Modular Equation Problem Code: CodeChef Solution Code #include <bits/stdc++.h> using   namespace   std ; #define   int   long   long   int #define   endl   " \n " int32_t   main () {      ios_base :: sync_with_stdio ( false );     cin. tie ( 0 );      int  t;cin >> t;      while (t -- )     {          int  n, m;cin >> n >> m;          int  count_of_pair  =   0 ;          vector < int >  modular_equation(n + 1 ,  1 );          for ( int  a  =   2 ;a <= n;a ++ )         {              int  x  =  m % a;             count_of_pair  +=  modular_equation [ x ] ;              for ( int  b  =  x;b <= n;b += a)             {                 modular_equation [ b ] ++ ;             }         }         cout << count_of_pair << endl ;     }      return   0 ; }

solution code of Tic Tac Toe problem number-4 of may long challenge codechef

 Tic Tac Toe Problem Solution Code:  CodeChef Solution Code #include <bits/stdc++.h> using   namespace   std ; #define   ll   long   long   int int   main () {      ll  t;     cin >> t;      while (t -- )     {          ll  cx  =   0 , co  = 0 , c_ =   0 ;          char  a[ 3 ][ 3 ];          for ( ll  i = 0 ;i < 3 ;i ++ )         {              for ( ll  j = 0 ;j < 3 ;j ++ )             {                 cin >> a[i][j];                  if (a[i][j] == 'X' )cx ++ ;                  if (a[i][j] == 'O' )co ++ ;                  if (a[i][j] == '_' )c_ ++ ;             }         }          ll  wx  =   0 , wo  =   0 ;          if (a[ 0 ][ 0 ]  ==   'X'   &&  a[ 1 ][ 0 ]  ==   'X'   &&  a[ 2 ][ 0 ]  ==   'X' )wx = 1 ;          if (a[ 0 ][ 1 ]  ==   'X'   &&  a[ 1 ][ 1 ]  ==   'X'   &&  a[ 2 ][ 1 ]  ==   'X' )wx = 1 ;          if (a[ 0 ][ 2 ]  ==   'X'   &