Spread the word.

Share the link on social media.

Share
  • Facebook
Have an account? Sign In Now

Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here
Sign InSign Up

SIKSHAPATH

SIKSHAPATH Navigation

  • Home
  • Questions
  • Blog
    • Computer Science(CSE)
    • NPTEL
    • Startup
  • Shop
    • Internshala Answers
Search
Ask A Question

Mobile menu

Close
Ask A Question
  • Home
  • Questions
  • Blog
    • Computer Science(CSE)
    • NPTEL
    • Startup
  • Shop
    • Internshala Answers
Home/ Questions/Q 10285
Next
In Process

SIKSHAPATH Latest Questions

Advance Learner
  • 2
  • 2
Advance Learner
Asked: January 5, 20222022-01-05T19:07:35+05:30 2022-01-05T19:07:35+05:30In: Programming Language

You are given a rooted tree with N nodes, labeled 1 through n

  • 2
  • 2

You are given a rooted tree with N nodes, labeled 1 through n, rooted at node 1. Each node in the tree can be in one of two colors, black or white, at any point in time. Initially, all nodes are colored black. Over this tree, you need to perform Q queries of the following types:

  • 1 u v— report the number of paths in the tree that passes through the edge connecting the nodes u and v, and has both its endpoints colored in black.
  • 2 u — flip the color of all the nodes in the subtree of node u, i.e., of all nodes that are in the subtree of node u, color those nodes white which are currently black, and color those nodes black which are currently white.
you are given a rooted tree with n nodes
  • 1 1 Answer
  • 2k Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    1 Answer

    • Voted
    • Oldest
    • Recent
    1. I'M ADMIN
      I'M ADMIN
      2022-01-05T20:34:48+05:30Added an answer on January 5, 2022 at 8:34 pm

      Run with input

      #include<bits/stdc++.h>
      using namespace std;
      #define int long long
      #define fo(i,a,b) for(int i = a; i<b ; i++)
      #define FIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
      #define pb push_back
      #define M 1000000007
      int mod(int x){
      return ((x%M + M)%M);
      }
      int add(int a,int b){
      return mod(mod(a)+mod(b));
      }
      int mul(int a,int b){
      return mod(mod(a)*mod(b));
      }
      // ****************************************************************************
      const int maxn = 2e5 + 5;
      int ft[2*maxn],tree[8*maxn],diary[8*maxn],in[maxn],out[maxn],timer;
      vector<int>v[maxn];
      
      void dfs(int src,int par){
      ft[timer] = src;
      in[src] = timer++;
      for(auto &it : v[src]){
      if(it==par)
      continue;
      dfs(it,src);
      }
      ft[timer] = src;
      out[src] = timer++;
      }
      void build(int treeind,int tl,int tr){
      if(tl>tr)
      return;
      if(tl==tr){
      tree[treeind] =1ll;
      return;
      }
      int mid = (tl + tr)/2;
      build(2*treeind+1,tl,mid);
      build(2*treeind+2,mid+1,tr);
      tree[treeind] = tree[2*treeind+1] + tree[2*treeind+2];
      }
      void prop(int treeind,int tl,int tr){
      if(diary[treeind]){
      int val = tree[treeind];
      tree[treeind] = (tr-tl+1) - val;
      if(tl!=tr){
      diary[2*treeind+1]^=1ll;
      diary[2*treeind+2]^=1ll;
      }
      diary[treeind] = 0;
      }
      }
      int l,r;
      void upd(int treeind,int tl,int tr){
      if(tl>tr)
      return;
      prop(treeind,tl,tr);
      if(tl>r || tr<l)
      return;
      if(tl>=l && tr<=r){
      int val = tree[treeind];
      tree[treeind] = (tr-tl+1) - val;
      if(tl!=tr){
      diary[2*treeind+1]^=1ll;
      diary[2*treeind+2]^=1ll;
      }
      return;
      }
      int mid = (tl + tr)/2;
      upd(2*treeind+1,tl,mid);
      upd(2*treeind+2,mid+1,tr);
      tree[treeind] = tree[2*treeind+1] + tree[2*treeind+2];
      }
      int qry(int treeind,int tl,int tr){
      if(tl>tr)
      return 0ll;
      prop(treeind,tl,tr);
      if(tl>r || tr<l)
      return 0ll;
      if(tl>=l && tr<=r)
      return tree[treeind];
      int mid = (tl + tr)/2;
      return qry(2*treeind+1,tl,mid) + qry(2*treeind+2,mid+1,tr);
      }
      
      void solve(){
      int n;
      cin>>n;
      fo(i,0,n-1){
      int a,b;
      cin>>a>>b;
      v[a].pb(b);
      v[b].pb(a);
      }
      dfs(1,0);
      build(0,0,2*n-1);
      int q;
      cin>>q;
      while(q--){
      int ty;
      cin>>ty;
      if(ty==1){
      int u,v;
      cin>>u>>v;
      int par,ch;
      if(in[u]>=in[v] && out[u]<=out[v]){
      par=v,ch=u;
      }
      else{
      par=u,ch=v;
      }
      l = in[ch];
      r = out[ch];
      int tot = tree[0]/2;
      int ans = qry(0,0,2*n-1);
      ans/=2;
      cout<<(tot-ans)*ans<<"\n";
      }
      else{
      int node;
      cin>>node;
      l = in[node];
      r = out[node];
      upd(0,0,2*n-1);
      }
      }
      }
      signed main()
      {
      FIO
      int t;
      t=1;
      // cin>>t;
      while(t--)
      {
      solve();
      }
      }
        • 0
      • Reply
      • Share
        Share
        • Share on WhatsApp
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn

    Leave an answer
    Cancel reply

    You must login to add an answer.

    Forgot Password?

    Need An Account, Sign Up Here

    Sidebar

    store ads

    Stats

    • Questions 1k
    • Answers 1k
    • Posts 149
    • Best Answers 89
    • This Free AI Tool Translates Entire Books in Minute !
    • AI News: 🎬 Hollywood’s AI Studios, 🎓 OpenAI’s Latest Gift to Educators, 🚚 Class8 Bags $22M, 🧠 Google Gemini’s Memory Upgrade
    • AI NEWS: Legal Action Against OpenAI, $16M Paid, & Elon Musk’s Praise from Investor 🤖💰📑 | AI Boosts Cloud Seeding for Water Security 🌱💧
    • AI News: 🎬AI Video Tool Scam Exposed🤯, 🛰️ AI-Powered Drones to Ukraine 😱, Google’s $20M AI Push, Sam Altman Joins SF’s Leadership Team
    • AI News: 🤝 Biden Meets Xi on AI Talks, 💡 Xavier Niel’s Advice for Europe, ♻️ Hong Kong’s Smart Bin Revolution, 🚀 AI x Huawei

    Explore

    • Recent Questions
    • Questions For You
    • Answers With Time
    • Most Visited
    • New Questions
    • Recent Questions With Time

    Footer

    SIKSHAPATH

    Helpful Links

    • Contact
    • Disclaimer
    • Privacy Policy Notice
    • TERMS OF USE
    • FAQs
    • Refund/Cancellation Policy
    • Delivery Policy for Sikshapath

    Follow Us

    © 2021-24 Sikshapath. All Rights Reserved

    Insert/edit link

    Enter the destination URL

    Or link to existing content

      No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.