In a tree, if you check, all leaf nodes have a degree = 1 So you can remove all leaf nodes first, and once you remove them, their parents will become the leaf node of the new tree formed due to their removal. And so on, you can just go on removing the leaf nodes until only root node is left So you can always remove n-1 nodes... basically just go on removing leaf nodes, then next set of leaf nodes...so on uptil only root node is left He is doing a dfs, where he is passing the vertex and its parent. When the dfs function is returning from its function call, it means the node now has no more children, and he is making a check that vertex != Parent ,which means its a leaf node, and hence you can just print it In this way you will print all leaf nodes in order uptil only the root node is left
Can anyone help me with TREE REMOVAL problem ? I did the same approach and implemented but code gives WA on hidden testcase, I'm unable to debug.. If anyone can help me find the testcase on which it is failing.. MY CODE : #include using namespace std; #define int long long int #define endl " " #define all(v) (v).begin(), (v).end() #define vi vector void dfs(map &mp, int node, int parent, vector &vec) { for (auto ele : mp[node]) { if (ele != parent) { dfs(mp, ele, node, vec); } } vec.push_back(node); } void solve() { int n; cin >> n; vector v; for (int i = 0; i < n; i++) { int x; cin >> x; v.emplace_back(x); } map mp; for (int i = 0; i < n - 1; ++i) { int u, v; cin >> u >> v; mp[u].push_back(v); mp[v].push_back(u); } int node = *(min_element(all(v))); cout
I think you are considering the min value as root but not the node with min value. For example, values are [6,7,8,5] then in your case root is 5 but it should be 4 (1-based indexing)
jiski value minimum ho usko root assume kro aur kisi bhi tree me root hi hogi jo kabhi bhi ans me add nhi hogi(kyuki end me uski degree 0)hogi aur leaf ki degree 1 h to bottom to up move kro aur value ko score me add kro aur leaf ko store kr dena
can someone please help me with this for fifth question giving run time error #include using namespace std; int main() { int t; cin>>t; while(t--){ string s; cin>>s; int ans=0; int n=s.length(); if(n%2!=0) ans=0; else{ int x=n/2; vectorprefix(n),suffix(n),mera(n); for(int i=0;i=0) prefix[i]=prefix[i-1]+s[i]; else prefix[i]=s[0]; } for(int i=n-1;i>=0;i--){ if(i
Your explanation is poor . Atleast dry run the code.
for problem c, if you just try to come up with answer of n = 4,5,6,7 then you eventually you will get the pattern.
Just great explanations. Big fan 🙏. Also , can you please tell me how to be so good at proving your solutions I mean you're just great !
Please fill the feedback form: forms.gle/3zBtR5zkbaVrxKzh7
came for question B. Sorry sir but not able to understand !
what about problem f?
where to learn z algo or other string alfgoS>?
cp algorithm
no need i think sliding window lagao ho jaega
@@kumkumslab5811 but for similar questions it is must
\
In problem D how does it make sure that it will print odd degree nodes only? If anyone knows please explain..
In a tree, if you check, all leaf nodes have a degree = 1
So you can remove all leaf nodes first, and once you remove them, their parents will become the leaf node of the new tree formed due to their removal.
And so on, you can just go on removing the leaf nodes until only root node is left
So you can always remove n-1 nodes... basically just go on removing leaf nodes, then next set of leaf nodes...so on uptil only root node is left
He is doing a dfs, where he is passing the vertex and its parent.
When the dfs function is returning from its function call, it means the node now has no more children, and he is making a check that vertex != Parent ,which means its a leaf node, and hence you can just print it
In this way you will print all leaf nodes in order uptil only the root node is left
@@hashirshaikh510 Thank you so much for your explanation
Thumbnail??
bakwas explaination, na sound hai, na dry run. should give more time on explanation and intution than just showing the code
Extremely poor explanation
Can anyone help me with TREE REMOVAL problem ? I did the same approach and implemented but code gives WA on hidden testcase, I'm unable to debug.. If anyone can help me find the testcase on which it is failing..
MY CODE :
#include
using namespace std;
#define int long long int
#define endl "
"
#define all(v) (v).begin(), (v).end()
#define vi vector
void dfs(map &mp, int node, int parent, vector &vec)
{
for (auto ele : mp[node])
{
if (ele != parent)
{
dfs(mp, ele, node, vec);
}
}
vec.push_back(node);
}
void solve()
{
int n;
cin >> n;
vector v;
for (int i = 0; i < n; i++)
{
int x;
cin >> x;
v.emplace_back(x);
}
map mp;
for (int i = 0; i < n - 1; ++i)
{
int u, v;
cin >> u >> v;
mp[u].push_back(v);
mp[v].push_back(u);
}
int node = *(min_element(all(v)));
cout
I think you are considering the min value as root but not the node with min value. For example, values are [6,7,8,5] then in your case root is 5 but it should be 4 (1-based indexing)
@@_dragneel_ Oh yes that was the problem. I confused indices and values. Thank you so much for pointing it out. 🙏🏼🫂
d wala aache se clear nhi hua
jiski value minimum ho usko root assume kro aur kisi bhi tree me root hi hogi jo kabhi bhi ans me add nhi hogi(kyuki end me uski degree 0)hogi aur leaf ki degree 1 h to bottom to up move kro aur value ko score me add kro aur leaf ko store kr dena
@@alankritsingh5222 ohk got it thanks
can someone please help me with this for fifth question giving run time error
#include
using namespace std;
int main()
{
int t;
cin>>t;
while(t--){
string s;
cin>>s;
int ans=0;
int n=s.length();
if(n%2!=0) ans=0;
else{
int x=n/2;
vectorprefix(n),suffix(n),mera(n);
for(int i=0;i=0)
prefix[i]=prefix[i-1]+s[i];
else prefix[i]=s[0];
}
for(int i=n-1;i>=0;i--){
if(i
yaar es bhaiya ka itna ganda explanation h , raghav bhiaya is fine
idk man, i find his explanations good enough
explanation can be better
poor explanation