Codeforces Round #435 (Div. 2), problem: (B) Mahmoud and Ehab and the bipartiteness 【染色法dfs】

The meaning of problems

Given n-1 side view of an article n nodes, but also a maximum demand plus several edge, to ensure that this does not exist in FIG weight side, since the ring, and is a bipartite graph

Thinking

Tree must be a bipartite graph, there must be n-1 edges, we dfs its first-half staining, the points are divided into two sets, the set size of two product | s1 | * | s2 | is up to any number of sides, subtract the original number of sides that is asked for.

code

#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
int n;
const int maxn=1e5+5;
int vis[maxn];
vector<int> G[maxn];
ll n1,n2;
void dfs(int u,int t){
    vis[u]=1;
    if(t&1) n1++;
    else n2++;
    for(auto v:G[u]){
        if(!vis[v])
            dfs(v,t+1);
    }
}
int main(){
    cin>>n;
    n1=0,n2=0;
    for(int i=0;i<n-1;i++){
        int u,v;
        cin>>u>>v;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    dfs(1,0);
    cout<<n1*n2-(n-1)<<endl;
    return 0;
}
学如逆水行舟,不进则退
Published 415 original articles · won praise 974 · views 130 000 +

Guess you like

Origin blog.csdn.net/weixin_42429718/article/details/104105798