862B Codeforces - Mahmoud and Ehab and the pipartiteness


862B Codeforces - Mahmoud and Ehab and the pipartiteness





Solution in C++


#include <bits/stdc++.h>

using namespace std;
typedef long long int lli;
lli n,t1,t2,bla,sol,acc;
vector<lli> adj[100005],vis(100005);
void dfs(int x,int col)
{
if(vis[x])return;vis[x]=1;
if(col)col=0,acc+=adj[x].size();else bla++,col=1;
for(auto u : adj[x])dfs(u,col);
}
int main() {
    cin>>n;
    for(int i=0;i<n-1;i++)
    {
        cin>>t1>>t2;
        adj[t1].push_back(t2);
        adj[t2].push_back(t1);
    }
    dfs(1,1);
    lli out=lli((n-bla)*bla)-acc;
    cout<<out;
    return 0;
}
Share on Google Plus

About Mohamed Sobhi

My name is Mo.Sobhy ,I study at School excelling high school in Ain Shams ,Cairo
My hobbies is programming ,and web development ,playing chess and writing horror stories.
    Blogger Comment
    Facebook Comment

0 التعليقات:

إرسال تعليق