package Graph;
import java.util.*;
public class Largest_Sum_Cycle
{
public static int solution(int arr[])
{
ArrayList<Integer>sum=new ArrayList<>();
for(int i=0;i<arr.length;i++)
{
ArrayList<Integer>path=new ArrayList<>();
int j=i;
int t=0;
while(arr[j]<arr.length&&arr[j]!=i&&arr[j]!=-1&&!path.contains(j))
{
path.add(j);
t+=j;
j=arr[j];
if(arr[j]==i)
{
t+=j;
break;
}
}
if(j<arr.length&&i==arr[j])
sum.add(t);
}
if(sum.isEmpty())
return -1;
return Collections.max(sum);
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int testcases=sc.nextInt();
for(int loop=0;loop<testcases;loop++)
{
int numofBlocks=sc.nextInt();
int arr[]=new int[numofBlocks];
int src,dest;
for(int i=0;i<numofBlocks;i++)
{
arr[i]=sc.nextInt();
}
System.out.println(solution(arr));
}
}
}
Juspay โ
import java.util.*;
public class Largest_Sum_Cycle
{
public static int solution(int arr[])
{
ArrayList<Integer>sum=new ArrayList<>();
for(int i=0;i<arr.length;i++)
{
ArrayList<Integer>path=new ArrayList<>();
int j=i;
int t=0;
while(arr[j]<arr.length&&arr[j]!=i&&arr[j]!=-1&&!path.contains(j))
{
path.add(j);
t+=j;
j=arr[j];
if(arr[j]==i)
{
t+=j;
break;
}
}
if(j<arr.length&&i==arr[j])
sum.add(t);
}
if(sum.isEmpty())
return -1;
return Collections.max(sum);
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int testcases=sc.nextInt();
for(int loop=0;loop<testcases;loop++)
{
int numofBlocks=sc.nextInt();
int arr[]=new int[numofBlocks];
int src,dest;
for(int i=0;i<numofBlocks;i++)
{
arr[i]=sc.nextInt();
}
System.out.println(solution(arr));
}
}
}
Juspay โ
import java.util.Scanner;
import java.util.*;
public class metting
{
public static void helperFunction()
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] edges = new int[n];
for (int i = 0; i < n; i++)
{
edges[i] = sc.nextInt();
}
int C1 = sc.nextInt();
int C2 = sc.nextInt();
// int ans=minimumWeight(n,edges,C1,C2);
// System.out.println(ans);
// public static int minimumWeight(int n, int[] edges, int C1, int C2) {
List<List<Integer>> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
list.add(new ArrayList<Integer>());
}
for (int i = 0; i < n; i++) {
if (edges[i] != -1) {
list.get(i).add(edges[i]);
}
}
long[] array1 = new long[n];
long[] array2 = new long[n];
Arrays.fill(array1, Long.MAX_VALUE);
Arrays.fill(array2, Long.MAX_VALUE);
juspay(C1, list, array1);
juspay(C2, list, array2);
int node = 0;
long dist = Long.MAX_VALUE;
for (int i = 0; i < n; i++) {
if (array1[i] == Long.MAX_VALUE || array2[i] == Long.MAX_VALUE)
continue;
if (dist > array1[i] + array2[i]) {
dist = array1[i] + array2[i];
node = i;
}
}
if (dist == Long.MAX_VALUE)
System.out.print(-1);
//return -1;
// return node;
System.out.print(node);
}
private static void juspay(int start, List<List<Integer>> graph, long[] distances)
{
PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.offer(start);
distances[start] = 0;
while (!pq.isEmpty())
{
int curr = pq.poll();
for (int neighbor : graph.get(curr))
{
long distance = distances[curr] + 1;
if (distance < distances[neighbor])
{
distances[neighbor] = distance;
pq.offer(neighbor);
}
}
}
}
public static void main(String[] args) {
metting m = new metting();
metting.helperFunction();
}
}
Nearest meeting Cell
Juspay โ
import java.util.*;
public class metting
{
public static void helperFunction()
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] edges = new int[n];
for (int i = 0; i < n; i++)
{
edges[i] = sc.nextInt();
}
int C1 = sc.nextInt();
int C2 = sc.nextInt();
// int ans=minimumWeight(n,edges,C1,C2);
// System.out.println(ans);
// public static int minimumWeight(int n, int[] edges, int C1, int C2) {
List<List<Integer>> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
list.add(new ArrayList<Integer>());
}
for (int i = 0; i < n; i++) {
if (edges[i] != -1) {
list.get(i).add(edges[i]);
}
}
long[] array1 = new long[n];
long[] array2 = new long[n];
Arrays.fill(array1, Long.MAX_VALUE);
Arrays.fill(array2, Long.MAX_VALUE);
juspay(C1, list, array1);
juspay(C2, list, array2);
int node = 0;
long dist = Long.MAX_VALUE;
for (int i = 0; i < n; i++) {
if (array1[i] == Long.MAX_VALUE || array2[i] == Long.MAX_VALUE)
continue;
if (dist > array1[i] + array2[i]) {
dist = array1[i] + array2[i];
node = i;
}
}
if (dist == Long.MAX_VALUE)
System.out.print(-1);
//return -1;
// return node;
System.out.print(node);
}
private static void juspay(int start, List<List<Integer>> graph, long[] distances)
{
PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.offer(start);
distances[start] = 0;
while (!pq.isEmpty())
{
int curr = pq.poll();
for (int neighbor : graph.get(curr))
{
long distance = distances[curr] + 1;
if (distance < distances[neighbor])
{
distances[neighbor] = distance;
pq.offer(neighbor);
}
}
}
}
public static void main(String[] args) {
metting m = new metting();
metting.helperFunction();
}
}
Nearest meeting Cell
Juspay โ
Forwarded from OffCampus Jobs | OnCampus Jobs | Daily Jobs Updates | Lastest Jobs | All Jobs | CSE Jobs | Fresher Jobs โฅ (Dushyant)
QA Intern at HappyFox
Internship Details:
Full-time Paid Internship
Internship duration: 6 months
Mode of work: Work from office (Guindy, Chennai)
Full time opportunity will be available to interns with good performance
APPLY - https://happyfox.hire.trakstar.com/jobs/fk0xy99
Internship Details:
Full-time Paid Internship
Internship duration: 6 months
Mode of work: Work from office (Guindy, Chennai)
Full time opportunity will be available to interns with good performance
APPLY - https://happyfox.hire.trakstar.com/jobs/fk0xy99
Forwarded from OffCampus Jobs | OnCampus Jobs | Daily Jobs Updates | Lastest Jobs | All Jobs | CSE Jobs | Fresher Jobs โฅ (Dushyant)
๐Baker Hughes is Hiring !!
Role: Summer Intern
Batch: 2025, 2026, 2027
Expected Stipend: 25k-40k per month
Apply here- https://bit.ly/3S4F7ou
Role: Summer Intern
Batch: 2025, 2026, 2027
Expected Stipend: 25k-40k per month
Apply here- https://bit.ly/3S4F7ou
Unstop
Summer Internship (Engineering/Technology) by Baker Hughes! | 2023 // Unstop (formerly Dare2Compete)
Summer Internship (Engineering/Technology) a jobs by Baker Hughes open to Engineering Students Apply online before 2024-01-22 00:00:00! | 2023
๐๐ฆ ๐๐น๐ด๐ผ ๐ป ๐ ใ๐๐ผ๐บ๐ฝ๐ฒ๐๐ถ๐๐ถ๐๐ฒ ๐ฃ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ๐บ๐ถ๐ป๐ดใ
Photo
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
class TreeNode {
public:
int data;
TreeNode* left;
TreeNode* right;
TreeNode(int val) {
data = val;
left = nullptr;
right = nullptr;
}
};
class Solution {
public:
bool checkPath(TreeNode* root, int dest) {
if (!root) {
return false;
}
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
TreeNode* current = q.front();
q.pop();
if (!current->left && !current->right && current->data == dest) {
return true;
}
if (current->left) {
current->left->data += current->data;
q.push(current->left);
}
if (current->right) {
current->right->data += current->data;
q.push(current->right);
}
}
return false;
}
TreeNode* takeLevelOrder() {
int rootData;
cin >> rootData;
if (rootData == -1) {
return nullptr;
}
TreeNode* root = new TreeNode(rootData);
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
TreeNode* current = q.front();
q.pop();
int leftChild, rightChild;
cin >> leftChild >> rightChild;
if (leftChild != -1) {
current->left = new TreeNode(leftChild);
q.push(current->left);
}
if (rightChild != -1) {
current->right = new TreeNode(rightChild);
q.push(current->right);
}
}
return root;
}
};
int main() {
Solution solution;
int dest;
cin >> dest;
TreeNode* root = solution.takeLevelOrder();
bool result = solution.checkPath(root, dest);
cout << (result ? "True" : "False") << endl;
return 0;
}
Zeta โ
#include <queue>
#include <vector>
using namespace std;
class TreeNode {
public:
int data;
TreeNode* left;
TreeNode* right;
TreeNode(int val) {
data = val;
left = nullptr;
right = nullptr;
}
};
class Solution {
public:
bool checkPath(TreeNode* root, int dest) {
if (!root) {
return false;
}
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
TreeNode* current = q.front();
q.pop();
if (!current->left && !current->right && current->data == dest) {
return true;
}
if (current->left) {
current->left->data += current->data;
q.push(current->left);
}
if (current->right) {
current->right->data += current->data;
q.push(current->right);
}
}
return false;
}
TreeNode* takeLevelOrder() {
int rootData;
cin >> rootData;
if (rootData == -1) {
return nullptr;
}
TreeNode* root = new TreeNode(rootData);
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
TreeNode* current = q.front();
q.pop();
int leftChild, rightChild;
cin >> leftChild >> rightChild;
if (leftChild != -1) {
current->left = new TreeNode(leftChild);
q.push(current->left);
}
if (rightChild != -1) {
current->right = new TreeNode(rightChild);
q.push(current->right);
}
}
return root;
}
};
int main() {
Solution solution;
int dest;
cin >> dest;
TreeNode* root = solution.takeLevelOrder();
bool result = solution.checkPath(root, dest);
cout << (result ? "True" : "False") << endl;
return 0;
}
Zeta โ
Hotstar is hiring Product Design Intern
๐ Experience: Fresher
๐ Location: Bangalore
๐ค Share with your friends & colleagues
๐ Apply: https://jobs.lever.co/hotstar/1d648f52-4e43-4102-8856-87ca5562a6f2
๐ Experience: Fresher
๐ Location: Bangalore
๐ค Share with your friends & colleagues
๐ Apply: https://jobs.lever.co/hotstar/1d648f52-4e43-4102-8856-87ca5562a6f2
Goldman Sachs is hiring Summer Analyst
๐ Experience: Fresher
๐ค Share with your friends & colleagues
๐ Apply: https://goldmansachs.tal.net/vx/lang-en-GB/mobile-0/brand-2/candidate/so/pm/1/pl/1/opp/2-Summer-Analyst-Summer-Associate-Internship-programs/en-GB
๐ Experience: Fresher
๐ค Share with your friends & colleagues
๐ Apply: https://goldmansachs.tal.net/vx/lang-en-GB/mobile-0/brand-2/candidate/so/pm/1/pl/1/opp/2-Summer-Analyst-Summer-Associate-Internship-programs/en-GB
long long solve(int n, int** A) {
vector<pair<int, int>> vp;
vp.push_back({-1, 0});
for (int i = 0; i < n; i += 1) {
vp.push_back({A[i][1], A[i][2]});
}
sort(vp.begin(), vp.end());
auto check = [&] (int mid) {
long long dishes = 0;
for (int i = 1; i <= n; i += 1) {
dishes += static_cast<long long>(vp[i].first - vp[i - 1].first) * mid;
if (vp[i].second > dishes)
return false;
else
dishes -= vp[i].second;
}
return true;
};
int l = 0, r = 1e9;
while (r - l > 1) {
int mid = (l + r) / 2;
if (!check(mid))
l = mid;
else
r = mid;
}
return r;
}
Chef and ordering โ
vector<pair<int, int>> vp;
vp.push_back({-1, 0});
for (int i = 0; i < n; i += 1) {
vp.push_back({A[i][1], A[i][2]});
}
sort(vp.begin(), vp.end());
auto check = [&] (int mid) {
long long dishes = 0;
for (int i = 1; i <= n; i += 1) {
dishes += static_cast<long long>(vp[i].first - vp[i - 1].first) * mid;
if (vp[i].second > dishes)
return false;
else
dishes -= vp[i].second;
}
return true;
};
int l = 0, r = 1e9;
while (r - l > 1) {
int mid = (l + r) / 2;
if (!check(mid))
l = mid;
else
r = mid;
}
return r;
}
Chef and ordering โ
def Solve(N, K):
slots = [0] * K
current_bag = 1
current_slot = 0
while N > 0:
bags_to_distribute = min(N, current_bag)
slots[current_slot] += bags_to_distribute
N -= bags_to_distribute
current_bag += 1
current_slot = (current_slot + 1) % K
return slots
N = int(input())
K = int(input())
result = Solve(N, K)
print(" ".join(map(str, result)))
Airport Baggage โ
slots = [0] * K
current_bag = 1
current_slot = 0
while N > 0:
bags_to_distribute = min(N, current_bag)
slots[current_slot] += bags_to_distribute
N -= bags_to_distribute
current_bag += 1
current_slot = (current_slot + 1) % K
return slots
N = int(input())
K = int(input())
result = Solve(N, K)
print(" ".join(map(str, result)))
Airport Baggage โ
Forwarded from OffCampus Jobs | OnCampus Jobs | Daily Jobs Updates | Lastest Jobs | All Jobs | CSE Jobs | Fresher Jobs โฅ (Dushyant)
Intern - Data Research (Looking for 2023 graduates/ BCA/MCA/Bsc-Csc/ BE/ Btech) at Forbes Advisor
https://www.linkedin.com/jobs/view/3779121039
https://www.linkedin.com/jobs/view/3779121039
Linkedin
Forbes Advisor hiring Intern - Data Research (Looking for 2023 graduates/ BCA/MCA/Bsc-Csc/ BE/ Btech) in Mumbai, Maharashtra, Indiaโฆ
Posted 9:22:09 AM. Company DescriptionForbes Advisor is a new initiative for consumers under the Forbes MarketplaceโฆSee this and similar jobs on LinkedIn.
Forwarded from OffCampus Jobs | OnCampus Jobs | Daily Jobs Updates | Lastest Jobs | All Jobs | CSE Jobs | Fresher Jobs โฅ (Dushyant)
Company Name: Groww
Role: SDE 1 - Frontend (Kotlin)
Eligibility: if you have some experience or some projects you can apply.
Apply: https://docs.google.com/forms/d/e/1FAIpQLScpmEJxBOw38o9QEnqKKbrjXggXnzsnQic6GeuxkuuIKmB3OQ/viewform
Role: SDE 1 - Frontend (Kotlin)
Eligibility: if you have some experience or some projects you can apply.
Apply: https://docs.google.com/forms/d/e/1FAIpQLScpmEJxBOw38o9QEnqKKbrjXggXnzsnQic6GeuxkuuIKmB3OQ/viewform
Forwarded from OffCampus Jobs | OnCampus Jobs | Daily Jobs Updates | Lastest Jobs | All Jobs | CSE Jobs | Fresher Jobs โฅ (Dushyant)
Wipro Direct Interview for Analyst Role.
Must Attend this Drive Everyone either you are technical or Non-technical background.
Must Attend this Drive Everyone either you are technical or Non-technical background.
bool check(string s, string t, vector<int>& order, int mid) {
int n = s.size(), m = t.size();
vector<int> pos(n);
for (int i = 0; i < mid; ++i) {
pos[order[i] - 1] = -1;
}
for (int i = mid; i < n; ++i) {
pos[order[i] - 1] = i;
}
int j = 0;
for (int i = 0; i < n && j < m; ++i) {
if (pos[i] != -1 && s[i] == t[j]) {
++j;
}
}
return j == m;
}
int solve(string s, string t, vector<int>& order) {
int low = 0, high = s.size();
while (low < high) {
int mid = low + (high - low + 1) / 2;
if (check(s, t, order, mid)) {
low = mid;
} else {
high = mid - 1;
}
}
return low;
}
String Search โ
int n = s.size(), m = t.size();
vector<int> pos(n);
for (int i = 0; i < mid; ++i) {
pos[order[i] - 1] = -1;
}
for (int i = mid; i < n; ++i) {
pos[order[i] - 1] = i;
}
int j = 0;
for (int i = 0; i < n && j < m; ++i) {
if (pos[i] != -1 && s[i] == t[j]) {
++j;
}
}
return j == m;
}
int solve(string s, string t, vector<int>& order) {
int low = 0, high = s.size();
while (low < high) {
int mid = low + (high - low + 1) / 2;
if (check(s, t, order, mid)) {
low = mid;
} else {
high = mid - 1;
}
}
return low;
}
String Search โ
Job description nowadays,
we're looking for a junior developer with the experience of a senior developer, offering an internโs salary:(
we're looking for a junior developer with the experience of a senior developer, offering an internโs salary:(
Forwarded from OffCampus Jobs | OnCampus Jobs | Daily Jobs Updates | Lastest Jobs | All Jobs | CSE Jobs | Fresher Jobs โฅ (Dushyant)
โ๏ธ Innofied Off Campus Hiring | Software Engineer โ Fresher | Upto 4.2 LPA โ๏ธ
๐จโ๐ป Job Role : Software Engineer
๐Qualification : B.Tech/BCA/ MCA
๐Batch : 2022/2023
๐ฐSalary : 4.2 LPA
https://docs.google.com/forms/d/e/1FAIpQLScX5UNh1EZuB-CPcHQzOKUo7K0o191cY5Q-irNnaARnX1VHaA/viewform
๐จโ๐ป Job Role : Software Engineer
๐Qualification : B.Tech/BCA/ MCA
๐Batch : 2022/2023
๐ฐSalary : 4.2 LPA
https://docs.google.com/forms/d/e/1FAIpQLScX5UNh1EZuB-CPcHQzOKUo7K0o191cY5Q-irNnaARnX1VHaA/viewform
Forwarded from OffCampus Jobs | OnCampus Jobs | Daily Jobs Updates | Lastest Jobs | All Jobs | CSE Jobs | Fresher Jobs โฅ (Dushyant)
๐จJOB OPENING UPDATE๐จ
Company โ S&P Global
Role โ Data Quality Analyst
Exp. โ Fresher
Apply Here โ https://careers.spglobal.com/jobs/295211?lang=en-us&utm_source=indeed&utm_source=indeed
Company โ Shell
Role โ Lead BI Analyst โ Data Engineering
Exp. โ Fresher
Apply Here โ https://jobs.shell.com/job/bengaluru/lead-bi-analyst-data-engineering/25244/59453151280?codes=1-INDEED
Company โ Ericsson
Role โ Data Engineer
Exp. โ Fresher
Apply Here โ https://jobs.ericsson.com/careers/job/563121757378324?domain=ericsson.com&jobPipeline=Indeed
Company โ FXCM
Role โ Business Intelligence Analyst
Exp. โ Fresher
Apply Here โ https://fxcm-hr.my.salesforce-sites.com/recruit/fRecruit__ApplyJob?vacancyNo=VN982&sid=37
Company โ Cheeron Life
Role โ Data Analyst Trainee
Exp. โ 0-1 yrs
Apply Here โ https://www.naukri.com/job-listings-data-analyst-trainee-cheeron-life-surat-gujarat-0-to-1-years-121023010939?src=jobsearchDesk&sid=17043459554449609&xp=1&px=1&nignbevent_src=jobsearchDeskGNB
Company โ Zcts
Role โ Data Analyst Trainee
Exp. โ 0-1 yrs
Apply Here โ https://www.naukri.com/job-listings-data-analyst-trainee-da-trainee-zcts-coimbatore-0-to-1-years-301223903794?src=jobsearchDesk&sid=17043459554449609&xp=2&px=1&nignbevent_src=jobsearchDeskGNB
Company โ Comcast Corporation
Role โ Machine Learning Intern
Exp. โ 0-2 yrs
Apply Here โ https://jobs.comcast.com/jobs/description/regular?external_or_internal=External&job_id=R364614&source=ind_orga_at&jobPipeline=Indeed
Company โ SatSure Analytics India
Role โ Data Scientist Intern
Exp. โ Fresher
Apply Here โ https://satsure.keka.com/careers/jobdetails/26859
Company โ Add Innovations Private Limited
Role โ Deep Learning Intern
Exp. โ Fresher
Apply Here โ https://smartapply.indeed.com/beta/indeedapply/form/contact-info
ALL THE BEST๐
Company โ S&P Global
Role โ Data Quality Analyst
Exp. โ Fresher
Apply Here โ https://careers.spglobal.com/jobs/295211?lang=en-us&utm_source=indeed&utm_source=indeed
Company โ Shell
Role โ Lead BI Analyst โ Data Engineering
Exp. โ Fresher
Apply Here โ https://jobs.shell.com/job/bengaluru/lead-bi-analyst-data-engineering/25244/59453151280?codes=1-INDEED
Company โ Ericsson
Role โ Data Engineer
Exp. โ Fresher
Apply Here โ https://jobs.ericsson.com/careers/job/563121757378324?domain=ericsson.com&jobPipeline=Indeed
Company โ FXCM
Role โ Business Intelligence Analyst
Exp. โ Fresher
Apply Here โ https://fxcm-hr.my.salesforce-sites.com/recruit/fRecruit__ApplyJob?vacancyNo=VN982&sid=37
Company โ Cheeron Life
Role โ Data Analyst Trainee
Exp. โ 0-1 yrs
Apply Here โ https://www.naukri.com/job-listings-data-analyst-trainee-cheeron-life-surat-gujarat-0-to-1-years-121023010939?src=jobsearchDesk&sid=17043459554449609&xp=1&px=1&nignbevent_src=jobsearchDeskGNB
Company โ Zcts
Role โ Data Analyst Trainee
Exp. โ 0-1 yrs
Apply Here โ https://www.naukri.com/job-listings-data-analyst-trainee-da-trainee-zcts-coimbatore-0-to-1-years-301223903794?src=jobsearchDesk&sid=17043459554449609&xp=2&px=1&nignbevent_src=jobsearchDeskGNB
Company โ Comcast Corporation
Role โ Machine Learning Intern
Exp. โ 0-2 yrs
Apply Here โ https://jobs.comcast.com/jobs/description/regular?external_or_internal=External&job_id=R364614&source=ind_orga_at&jobPipeline=Indeed
Company โ SatSure Analytics India
Role โ Data Scientist Intern
Exp. โ Fresher
Apply Here โ https://satsure.keka.com/careers/jobdetails/26859
Company โ Add Innovations Private Limited
Role โ Deep Learning Intern
Exp. โ Fresher
Apply Here โ https://smartapply.indeed.com/beta/indeedapply/form/contact-info
ALL THE BEST๐
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
unordered_map<int, vector<int>> g;
void create(vector<int>& p) {
for (int i = 1; i < p.size(); i++) {
g[p[i]].push_back(i);
}
}
int dfs(int s, vector<int>& w, int oH, int uH, int a, vector<int>& v) {
if (g.find(s) == g.end()) {
if (w[s] == a) {
v[s] = oH;
return oH;
}
return 0;
}
int p = 0;
if (w[s] == a) {
p = oH;
}
for (int t : g[s]) {
p += dfs(t, w, oH, uH, a, v);
}
v[s] = p;
return p;
}
void solve(vector<int>& p, vector<int>& w, int oH, int uH) {
g.clear();
vector<int> h(w.size(), 0);
vector<int> u(w.size(), 0);
create(p);
dfs(0, w, oH, uH, 1, h);
dfs(0, w, uH, oH, -1, u);
int ans = INT_MAX;
int pen = u[0];
for (int i = 0; i < w.size(); i++) {
ans = min(ans, h[i] + (pen - u[i]));
}
cout << ans << endl;
}
Hydrate the nodes โ
#include <bits/stdc++.h>
using namespace std;
unordered_map<int, vector<int>> g;
void create(vector<int>& p) {
for (int i = 1; i < p.size(); i++) {
g[p[i]].push_back(i);
}
}
int dfs(int s, vector<int>& w, int oH, int uH, int a, vector<int>& v) {
if (g.find(s) == g.end()) {
if (w[s] == a) {
v[s] = oH;
return oH;
}
return 0;
}
int p = 0;
if (w[s] == a) {
p = oH;
}
for (int t : g[s]) {
p += dfs(t, w, oH, uH, a, v);
}
v[s] = p;
return p;
}
void solve(vector<int>& p, vector<int>& w, int oH, int uH) {
g.clear();
vector<int> h(w.size(), 0);
vector<int> u(w.size(), 0);
create(p);
dfs(0, w, oH, uH, 1, h);
dfs(0, w, uH, oH, -1, u);
int ans = INT_MAX;
int pen = u[0];
for (int i = 0; i < w.size(); i++) {
ans = min(ans, h[i] + (pen - u[i]));
}
cout << ans << endl;
}
Hydrate the nodes โ