Comments

Bruh in D i used unordered map during the contest and it gave TLE after the contest on test 28 but when I changed unordered map to map it gave passed all test cases!!

On RohancoderQuestion, 3 years ago
0

yeah saw that leetcode question but was wondering how will we be able to do by using stacks when all different parenthesis are there..

On RohancoderQuestion, 3 years ago
0

Auto comment: topic has been updated by Rohancoder (previous revision, new revision, compare).

0

i was talking about div 2 mate

0

By doing A B C fast you can reach 1500+ easily maybe expert aswell.

0

Best of Luck everyone hope many pupils become specialist today and newbies can also change their color.

I always lose rating in div 1+ div2 contest any suggestions…?

India vs Pakistan (cry emoji)

Same!best of luck

On AmirrzwMCodeforces Global Round 23, 4 years ago
-44

Remove testcase 47

0

unrated participant be like my first official round

Can you elaborate the dfs method..?

+1

Best of Luck everyone!Hoping for a positive delta

please explain solution of B

On FagunHow do become specialist ??, 4 years ago
0

You have solved alot of problems buddy!But you should try to pratice questions of higher level from now on you should only pratice problems which are of rating 1500 or higher and solve till 1800 rating problems and try to solve on your own with out looking at the editorial try solving graphs problem,number theory when yo u are comfortable with rating of problem around 1700 or so go much higher you can easily become specialist then or even expert ig

0

Best of luck to everyone

Finally specialist!!

0

Best of luck to everyone!

whats wrong in this..? giving WA on test 2

ll dfs(ll node,vb & visited,ll last){ if(visited[node]){ return length[node]; } visited[node]=true; for(ll i=0;i<graph[node].size();i++){ // cout<<graph[node][i]<<" "<<node<<endl; if(graph[node][i]==last){ continue; } length[node]+=dfs(graph[node][i],visited,node);

}
return length[node];

} ll sol(ll node,ll last){ if(node==1){ if(graph[node].size()==2){ if(length[graph[node][0]]>length[graph[node][1]]){ return length[graph[node][0]]-1+sol(graph[node][1],1); } else{ return length[graph[node][1]]-1+sol(graph[node][0],1);

}
    }
    else if(graph[node].size()==1){
        return length[graph[node][0]]-1;
    }
    else{
        return 0;
    }
}




else if(graph[node].size()==2){
    if(graph[node][0]!=last){
        return length[graph[node][0]]-1;

    }
    else{
        return length[graph[node][1]]-1;

    }

}
else if(graph[node].size()==1){
    return 0;
}
else if(graph[node].size()==3){
    if(graph[node][2]==last){
        if(length[graph[node][0]]>length[graph[node][1]]){
            return length[graph[node][0]]-1+sol(graph[node][1],node);
        }
        else{
            return length[graph[node][1]]-1+sol(graph[node][0],node);

        }
    }
    else if(graph[node][1]==last){
        if(length[graph[node][0]]>length[graph[node][2]]){
            return length[graph[node][0]]-1+sol(graph[node][2],node);
        }
        else{
            return length[graph[node][2]]-1+sol(graph[node][0],node);

        }

    }
    else{
        if(length[graph[node][1]]>length[graph[node][2]]){
            return length[graph[node][1]]-1+sol(graph[node][2],node);
        }
        else{
            return length[graph[node][2]]-1+sol(graph[node][1],node);

        }


    }
}

}

int main() { fast_io int tt; cin>>tt; ll c=0;

while(tt--)
{
    c++;

   ll n;
   cin>>n;
   graph.clear();
   graph.resize(n+1);
   length.clear();
   length.resize(n+1);
   vector<ll>gg(n+1,1);
   length=gg;
   ll root=-1;


   fl(i,n-1){
    ll a;
    ll b;
    cin>>a>>b;

    graph[a].pb(b);
    graph[b].pb(a);
   }

   vb visited(n+1,false);
   ll ans=dfs(1,visited,-1);



  // cout<<ans<<endl;
   ll ans1=sol(1,-1);
   cout<<ans1<<nl;





}

}

can someone tell why this code is not giving correct answer on test case 88 expected 3 but found 2

include

include <bits/stdc++.h>

using namespace std;

//Speed

define Code ios_base::sync_with_stdio(false);

define By cin.tie(NULL);

define Asquare cout.tie(NULL);

//Aliases using ll= long long; using lld= long double; using ull= unsigned long long;

//Constants const lld pi= 3.141592653589793238; const ll INF= LONG_LONG_MAX; const ll mod=1e9+7;

//TypeDEf typedef pair<ll, ll> pll; typedef vector vll; typedef vector vpll; //typedef vector vs; typedef unordered_map<ll,ll> umll; typedef map<ll,ll> mll;

// Macros

define ff first

define ss second

define pb push_back

define mp make_pair

define fl(i,n) for(ll i=0;i<n;i++)

define rl(i,m,n) for(ll i=n;i>=m;i--)

define py cout<<"YES\n";

define pm cout<<"-1\n";

define pn cout<<"NO\n";

define vr(v) v.begin(),v.end()

define rv(v) v.end(),v.begin()

// Utility functions template void prll(T &&t) { cout << t << "\n"; } void prllarr(ll arr[], ll n){fl(i,n) cout << arr[i] << " ";cout << "\n";} template void prllvec(vectorv){ll n=v.size();fl(i,n)cout<<v[i]<<" ";cout<<"\n";} template ll sumvec(vectorv){ll n=v.size();ll s=0;fl(i,n)s+=v[i];return s;}

// Mathematical functions ll gcd(ll a, ll b){if (b == 0)return a;return gcd(b, a % b);} //__gcd ll lcm(ll a, ll b){return (a/gcd(a,b)*b);} ll moduloMultiplication(ll a,ll b,ll mod){ll res = 0;a %= mod;while (b){if (b & 1)res = (res + a) % mod;b >>= 1;}return res;} ll powermod(ll x, ll y, ll p){ll res = 1;x = x % p;if (x == 0) return 0;while (y > 0){if (y & 1)res = (res*x) % p;y = y>>1;x = (x*x) % p;}return res;}

//Graph-dfs // bool gone[MN]; // vector adj[MN]; // void dfs(ll loc){ // gone[loc]=true; // for(auto x:adj[loc])if(!gone[x])dfs(x); // }

define loop(i,a,b) for(int i=a;i<=b;i++)

define revloop(i,a,b) for(int i=a;i>=b;i--)

define readv(vec) for(auto &x:vec){cin>>x;}

define printv(vec) for(auto x: vec){cout<<x<<" ";}cout<<"\n";

//Sorting bool sorta(const pair<ll,ll> &a,const pair<ll,ll> &b){return (a.second < b.second);} bool sortd(const pair<ll,ll> &a,const pair<ll,ll> &b){return (a.second > b.second);}

//Bits string decToBinary(ll n){string s="";ll i = 0;while (n > 0) {s =to_string(n % 2)+s;n = n / 2;i++;}return s;} ll binaryToDecimal(string n){string num = n;ll dec_value = 0;ll base = 1;ll len = num.length();for(ll i = len — 1; i >= 0; i--){if (num[i] == '1')dec_value += base;base = base * 2;}return dec_value;}

//Check bool isPrime(ll n){if(n<=1)return false;if(n<=3)return true;if(n%2==0||n%3==0)return false;for(ll i=5;i*i<=n;i=i+6)if(n%i==0||n%(i+2)==0)return false;return true;} bool isPowerOfTwo(ll n){if(n==0)return false;return (ceil(log2(n)) == floor(log2(n)));} bool isPerfectSquare(ll x){if (x >= 0) {ll sr = sqrt(x);return (sr * sr == x);}return false;} //Code by Abhinav Awasthi

define vl vector

define vii vector

define vll vector

//#define vs vector

define vb vector

define no cout << "NO\n"

define yes cout << "YES\n"

using namespace std; void inpv(vl& a,ll n){ fl(i,n){ ll te; cin>>te; a.pb(te); } }

include

include

include<math.h>

using namespace std;

define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);

define nl "\n"

define ll long long

vector size1; vector parent; vector<vector>graph(10);

vectorlength;

ll dfs(ll node,vb & visited,ll last){ if(visited[node]){ return length[node]; } visited[node]=true; for(ll i=0;i<graph[node].size();i++){ // cout<<graph[node][i]<<" "<<node<<endl; if(graph[node][i]==last){ continue; } length[node]+=dfs(graph[node][i],visited,node);

}
return length[node];

} ll sol(ll node,ll last){ if(node==1){ if(graph[node].size()==2){ if(length[graph[node][0]]>length[graph[node][1]]){ return length[graph[node][0]]-1+sol(graph[node][1],1); } else{ return length[graph[node][1]]-1+sol(graph[node][0],1);

}
    }
    else if(graph[node].size()==1){
        return length[graph[node][0]]-1;
    }
    else{
        return 0;
    }
}




else if(graph[node].size()==2){
    if(graph[node][0]!=last){
        return length[graph[node][0]]-1;

    }
    else{
        return length[graph[node][1]]-1;

    }

}
else if(graph[node].size()==1){
    return 0;
}
else if(graph[node].size()==3){
    if(graph[node][2]==last){
        if(length[graph[node][0]]>length[graph[node][1]]){
            return length[graph[node][0]]-1+sol(graph[node][1],node);
        }
        else{
            return length[graph[node][1]]-1+sol(graph[node][0],node);

        }
    }
    else if(graph[node][1]==last){
        if(length[graph[node][0]]>length[graph[node][2]]){
            return length[graph[node][0]]-1+sol(graph[node][2],node);
        }
        else{
            return length[graph[node][2]]-1+sol(graph[node][0],node);

        }

    }
    else{
        if(length[graph[node][1]]>length[graph[node][2]]){
            return length[graph[node][1]]-1+sol(graph[node][2],node);
        }
        else{
            return length[graph[node][2]]-1+sol(graph[node][1],node);

        }


    }
}

}

int main() { fast_io int tt; cin>>tt; ll c=0;

while(tt--)
{
    c++;

   ll n;
   cin>>n;
   graph.clear();
   graph.resize(n+1);
   length.clear();
   length.resize(n+1);
   vector<ll>gg(n+1,1);
   length=gg;
   ll root=-1;


   fl(i,n-1){
    ll a;
    ll b;
    cin>>a>>b;

    graph[a].pb(b);
    graph[b].pb(a);
   }

   vb visited(n+1,false);
   ll ans=dfs(1,visited,-1);



  // cout<<ans<<endl;
   ll ans1=sol(1,-1);
   cout<<ans1<<nl;





}

}