1620E - Replace the Numbers - CodeForces Solution


constructive algorithms data structures dsu implementation *1900

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define ll long long
#define endl "\n"
#define fori(n)         for (ll i=0; i<n; i++) 
#define forj(n)         for (ll j=0; j<n; j++) 
#define fork(n)         for (ll k=0; k<n; k++) 
#define Sort(a)         sort(a.begin(),a.end())
#define pd pair<ll, ll>
#define  Alice  cout<<"Alice"<<endl
#define Bob  cout<<"Bob"<<endl

using namespace std;

// ncr 

/*  void prllNcR(ll n, ll r)
{
 
    // p holds the value of n*(n-1)*(n-2)...,
    // k holds the value of r*(r-1)...
    long long p = 1, k = 1;
 
    // C(n, r) == C(n, n-r),
    // choosing the smaller value
    if (n - r < r)
        r = n - r;
 
    if (r != 0) {
        while (r) {
            p *= n;
            k *= r;
 
            // gcd of p, k
            long long m = __gcd(p, k);
 
            // dividing by gcd, to simplify
            // product division by their gcd
            // saves from the overflow
            p /= m;
            k /= m;
 
            n--;
            r--;
        }
 
        // k should be simplified to 1
        // as C(n, r) is a natural number
        // (denominator should be 1 ) .
    }
 
    else
        p = 1;
 
    // if our approach is correct p = ans and k =1
    cout << p << endl;
}  */







//nCr%p
/*     unsigned long long power(unsigned long long x, 
                                  ll y, ll p)
{
    unsigned long long res = 1; // Initialize result
  
    x = x % p; // Update x if it is more than or
    // equal to p
  
    while (y > 0) 
    {
      
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res * x) % p;
  
        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
  
// Returns n^(-1) mod p
unsigned long long modInverse(unsigned long long n,  
                                            ll p)
{
    return power(n, p - 2, p);
} 
  
// Returns nCr % p using Fermat's little
// theorem.
 unsigned long long nCrModPFermat(unsigned long long n,
                                 ll r, ll p)
{
    // If n<r, then nCr should return 0
    if (n < r)
        return 0;
    // Base case
    if (r == 0)
        return 1;
  
    // Fill factorial array so that we
    // can find all factorial of r, n
    // and n-r
    unsigned long long fac[n + 1];
    fac[0] = 1;
    for (ll i = 1; i <= n; i++)
        fac[i] = (fac[i - 1] * i) % p;
  
    return (fac[n] * modInverse(fac[r], p) % p
            * modInverse(fac[n - r], p) % p)
           % p;
}    





ll M=998244353; */
// GRAPH
/*  vector<vector<ll>>graph(100001);
vector<ll>visited(100001,0);
vector<ll>counti(100001,0);

ll dfs(ll n)
{

    if(!visited[n])
    {
        ll count=0;
        if(graph[n].size()==1 && n!=1)
        count++;
            visited[n]=1;

                for(auto child:graph[n])
                count=count+dfs(child);
                counti[n]=count;
                return count;
 
    }
    else
    return 0;
}     */
 






// SIEVE 
/*      const ll N = 10002;
vector<ll> lp(N+1);
vector<ll> pr;

void Sieve()
{
for (ll i=2; i <= N; ++i) {
    if (lp[i] == 0) {
        lp[i] = i;
        pr.push_back(i);
    }
    for (ll j = 0; i * pr[j] <= N; ++j) {
        lp[i * pr[j]] = pr[j];
        if (pr[j] == lp[i]) {
            break;
        }
    }
} 
}      */



// BINPOW
/* ll power(ll x, ll y, ll p)
{
    ll res = 1;     // Initialize result
 
    x = x % p; // Update x if it is more than or
                // equal to p
  
    if (x == 0) return 0; // In case x is divisible by p;
 
    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x) % p;
 
        // y must be even now
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
} */
/*  vector<long long> trial_division2(long long n) {
    vector<long long> factorization;
    while (n % 2 == 0) {
        factorization.push_back(2);
        n /= 2;
    }
    for (long long d = 3; d * d <= n; d += 2) {
        while (n % d == 0) {
            factorization.push_back(d);
            n /= d;
        }
    }
    if (n > 1)
        factorization.push_back(n);
    return factorization;
}  */



int main()
{
    ios::sync_with_stdio(false);
	cin.tie(nullptr); 
    ll t,i;
    t=1;
    //cin>>t; 
    fori(t)
    {
        
        vector<ll>v(500002);
        forj(500002)
        {
            v[j]=j;
        }
        ll n;
        cin>>n;
        vector<pair<ll,vector<ll>>>vx;
        forj(n)
        {
            ll x;
            cin>>x;
            vector<ll>vy;
            fork(x)
            {
                ll y;
                cin>>y;
                vy.push_back(y);
            }
            vx.push_back({x,vy});
        }
        vector<ll>ans;
        for(ll j=n-1;j>=0;j--)
        {
            if(vx[j].first==2)
            {
                v[vx[j].second[0]]=v[vx[j].second[1]];

            }
            else
            {
                ans.push_back(v[vx[j].second[0]]);

            }

        }
        forj(ans.size())
        cout<<ans[ans.size()-1-j]<<" ";
        cout<<endl;


    }
    
}


Comments

Submit
0 Comments
More Questions

63A - Sinking Ship
1704B - Luke is a Foodie
298B - Sail
239A - Two Bags of Potatoes
1704E - Count Seconds
682A - Alyona and Numbers
44A - Indian Summer
1133C - Balanced Team
1704A - Two 0-1 Sequences
1467A - Wizard of Orz
1714E - Add Modulo 10
1714A - Everyone Loves to Sleep
764A - Taymyr is calling you
1714B - Remove Prefix
1264F - Beautiful Fibonacci Problem
52A - 123-sequence
1543A - Exciting Bets
1714D - Color with Occurrences
215B - Olympic Medal
1445A - Array Rearrangment
1351A - A+B (Trial Problem)
935B - Fafa and the Gates
1291A - Even But Not Even
1269A - Equation
441A - Valera and Antique Items
1702C - Train and Queries
816B - Karen and Coffee
838D - Airplane Arrangements
148B - Escape
847G - University Classes