Naruto and One Shot (Graph ques.)

u r welcome bro :smile:

@sandeep1103 please share the editorial , i m having difficulty in the implementation. would be a great help :slight_smile:

1 Like

I have but the code which i have is not mine i picked someone’s code and modify that code according to me and then try to understand the test cases. I had picked this code before your wonderful help :sweat_smile:

#include <bits/stdc++.h>
using namespace std;
int n;
int dfs(vector<vector>&a,vector &vis,int x,int &X,int &W){
int ans=0,num=1;
vis[x]=true;
for(int i:a[x]){
if(!vis[i]){
int tmp=dfs(a,vis,i,X,W);
num+=tmp;
ans=max(ans,tmp);
}
}
ans=max(ans,n-num);
if(ans<W)
X=x,W=ans;
else if(ans==W)
X=min(X,x);
return num;
}

int main(){
int t; cin>>t;
while(t–){
cin>>n;
vector<vector> a(n+1);
for(int i=1;i<n;i++){
int X,v;
cin>>X>>v;
a[X].push_back(v);
a[v].push_back(X);
}
vectorvis(n+1,false);
int X=INT_MAX,W=INT_MAX;
dfs(a,vis,1,X,W);
cout<<X<<" "<<W<<endl;
}
return 0;
}

Blockquote

Hey!! bro can you share your approach of Notebook Distribution ques. CodeChef: Practical coding for everyone i am having trouble to implement this.

Naruto and one shot

The editorial has been posted.

2 Likes

The question basically asks you to find the centroid of the tree. Centroid of the tree is a node whose subtree size does not exceed N/2 (N being the number of nodes). You can check out my submission here:
https://www.codechef.com/viewsolution/37349513

1 Like

You can simply binary Search over the answer. Here, have a look at my submission and let me know if you are not able to understand anything.
https://www.codechef.com/viewsolution/37345796

CodeChef: Practical coding for everyone
dp denotes subtree size . let me know if u have any issue.

2 Likes

well as far subtree is involved can u please suggest me some starter problems so it would be easy for me to solve this .Like using dfs i can calculate connected components ,length of each connected component and print the node which is a part of the component and where can i learn this. Please reply @ssrivastava990 ,@harsh0911

Bro there are many blogs over codeforces , just google it -
for questions -
Problem Topics - Codeforces

2 Likes

wow its really nice thanks for ur help :smile:

1 Like

Great place to learn certain basic algorithms and also practise questions.

To practise, I recommend to apply filters on codeforces and then practise to increase your level, gradually moving on to more complex problems.

1 Like

googling skills matters :stuck_out_tongue_winking_eye:

2 Likes

I used binary search for this problem
Here is my solution : CodeChef: Practical coding for everyone

And if u want to learn how to apply binary search for problems like this do watch Coden code utube channel’s playlist on binary search.It really helped me a lot.

can you explain your logic ? i know how to use binary search but still i am able to think why we use this .

Encoding August 20 Problem Name-Notebook Distribution - #6 by kanisht09 visit here i had the same problem like u :frowning:

So, according to the question, we are asked to find the maximum number of pages each book can have. We know that any book cannot contain pages from different types of books and we also the number of pages of each type of book we have initially.
Let us take one book, say X. Let book X initially have pages p. Now, to find out the number of books we can make of type X having say Y number of pages, we can simply do p/Y.

Let a book have 200 pages initially, then the number of different books of pages 25 that can be made out of this book is 8 (200/25).

So, now we just find the sum of books of each kind we can get when we consider each book having mid number of pages. Then, just check if this sum is greater than or equal to the number of children.

1 Like

thanks you bro for giving your precious time and explaining the logic in a beautiful way :slight_smile:
now i am able to do this because of you and guys like yours :slight_smile:
THANK YOU SO MUCH :slight_smile:

1 Like

THANK YOU @kanisht09 for helping me :slight_smile: Encoding August 20 Problem Name-Notebook Distribution this was really helpful :slight_smile:

1 Like