Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

HackerEarth The Battle of Panipat problem solution

YASH PAL, 31 July 2024
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

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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