google kickstart round H 2022| C problem | doubt

Revision en1, by loverBoUy, 2022-11-12 09:06:17

In today's kickstart C problem of Round H, my approach was:

a) IN one dfs func, find the number of nodes in the subtree which is strictly lesser than the node. b) in anohter dfs func, find the number of nodes which are strictly greater than the node parent and if a[node]>a[parent] add answer of parent into node. c)combine both and find ans;

code:

Your code here...

void dfs(int node)
{

     vis[node] = true;

     for (auto it : g[node])
     {
          if (vis[it] == false)
          {
               dfs(it);
               if (a[it] < a[node])
                    niche[node] += niche[it] + 1;
          }
     }
     return;
}
void dfs2(int node, int par)
{
     vis[node] = true;
     if (par != -1)
     {
          if (a[node] > a[par])
          {
               upar[node] = upar[par] + 1;

               upar[node] += niche[par];
          }
     }

     for (auto it : g[node])
     {
          if (vis[it] == false)
          {
               dfs2(it, node);
          }
     }
     return;
}
// in main function 
 for (int i = 0; i < n; i++)
     {
          if (ans < upar[i] + niche[i] + 1)
          {
               ans = upar[i] + niche[i] + 1;
               
          }
     }

why is this wrong?

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English loverBoUy 2022-11-12 09:06:17 1356 Initial revision (published)