Codeforces 862B - Mahmoud and Ehab and the bipartiteness

时间:2023-03-09 13:32:48
Codeforces 862B - Mahmoud and Ehab and the bipartiteness

862B - Mahmoud and Ehab and the bipartiteness

思路:先染色,然后找一种颜色dfs遍历每一个点求答案。

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a)) const int N=1e6+;
bool color[N];
vector<int>g[N];
int c=;
int n;
ll ans=; void dfs(int u,int v)
{
color[v]=!color[u];
if(color[v]==true)c++;
for(int i=;i<g[v].size();i++)
if(g[v][i]!=u)dfs(v,g[v][i]);
} void DFS(int u,int v)
{
if(color[v]==true)ans+=(n-c-g[v].size());
for(int i=;i<g[v].size();i++)
{
if(g[v][i]!=u)DFS(v,g[v][i]);
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
int u,v;
cin>>n;
for(int i=;i<n-;i++)
{
cin>>u>>v;
g[u].pb(v);
g[v].pb(u);
} color[]=false;
dfs(,);
DFS(,);
cout<<ans<<endl; return ;
}