Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
      • Data Structures Solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth The Battle of Panipat problem solution

YASH PAL, 31 July 202417 February 2026
In this HackerEarth The Battle of Panipat problem solution This is 1526 A.D. Third Battle of Panipat between Babur and Ibrahim Lodi, the current ruler of India is going on.
 
Realising that Lodi has much larger army, Babur planned to attack the cities of India to bring the moral of the soldiers of Lodi down.
 
Now, Lodi had been ordered by his ancestors that, if he is to rule India, the cities in India should be connected and this system should always form a tree.
 
Now, in 1526, India had N cities(numbered 0 to N-1) all connected by roads and the whole system formed a tree. Lodi had to plan to save his cities. So he decided to use his special power S.H.I.E.L.D. And since all the other cities would be destroyed he wanted the protected cities system should form a tree.
 
S.H.I.E.L.D was an expensive power and Lodi decided that he would use it only on some specific cities. Now you being the prime minister of the ruler, need to tell him in how many ways he can do this. Lodi may even not use this power at all on any city or he may even select all the cities.
 
 
HackerEarth The Battle of Panipat problem solution

 

 

HackerEarth The Battle of Panipat problem solution.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<vector>
#include<cassert>
#include<sstream>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std;
#define llu long long unsigned int
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define rep(i,b) for(i=0;i<b;i++)
#define mod 1000000007
#define pin(n) printf("%llun",n)
typedef pair<llu,llu> PII;
typedef vector<llu> VI;
VI degree[1000000];
int visited[1000000]={};
PII f(llu v)
{
llu i;
visited[v]=1;
PII ret = mp(1,0);
rep(i,degree[v].size())
{
if(visited[degree[v][i]]==0)
{
PII pr = f(degree[v][i]);
ret.F = (ret.F*(pr.F + 1))%mod;
ret.S = (ret.S+(pr.F + pr.S))%mod;
}
}
return ret;
}
int main()
{
llu n,i,x,y;
scanf("%llu",&n);
rep(i,n-1)
{
scanf("%llu%llu",&x,&y);
degree[x].pb(y);
degree[y].pb(x);
}
PII pr = f(0);
pin((pr.F + pr.S + 1)%mod);
return 0;
}
 
 
 
coding problems solutions HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes