Avg CTC: 21lpa
Role: Backend Developer
Application Link: Backend Dev Job Link
N cities are there connected using (n-1) bidirectional roads. Each city has a score given by A[i]. Two Marathons were to be organized. But the path of the marathon to contain cities with only even score or an odd score. You have to tell max number of cities in the marathon path if the cities were with even score or with odd score.
Input
5 (n cities)
4 3 2 3 5 (n scores of cities)
1 2 (n-1 lines denoting a road b/w two cities)
1 3
2 4
2 5
Output
2 3
Doctor Strange is finding the number of outcomes where Avengers will win the war given that number of days the war will go on.
The possible number of wins given by d days = number of pure number sequences of length d.
A pure number sequence is a sequence of digits where any digit (0-9) can come at any index i iff either n is 0(zero) or any divisor of n(except for 1) has been already placed before such that (j
vector<int> par;
vector<int> size;
void find(int u){
if(par[u] == -1) return u;
else return par[u] = find(par[u]);
}
void merge(int u , int v){
u = find(u);
v = find(v);
if( u != v){
par[u] = v;
size[v] += size[u];
}
}
void solve(int n , vector<int> score , vector<vector<int>> edge){
par.assign(n , -1);
size.assign(n, 1);
// find largest odd score connected component.
for(auto i : edge){
int u = i[0];
int v = i[1];
if( score[u]%2 && score[v]%2 ){
merge(u,v);
}
}
int odd_size = *max_element(size.begin() , size.end());
par.assign(n , -1);
size.assign(n, 1);
// find largest even score connected component.
for(auto i : edge){
int u = i[0];
int v = i[1];
if( (score[u]+1)%2 && (score[v]+1)%2 ){
merge(u,v);
}
}
int even_size = *max_element(size.begin() , size.end());
}
Overview
Solution
Now for every city we will do the following.
Now if the current city score is odd then,
Now if the current city score is even then,
Now at last return a pair {max_even_length_path,max_odd_length+path} starting from current vertex and going down.
After the dfs is over, cout ans_even_path and ans_odd_path.
did u check for the and ?? , as I think question is stating about a marathon meaning each node is visited only once during a path from start to end
In that case the max size of connected component will be wrong (ex a plus shaped tree having nodes of same parity)