Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • 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
Programming101
Programming101

Learn everything about programming

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
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes