codeforces 862B. Mahmoud and Ehab and the bipartiteness

来源:互联网 发布:flash cc mac中文版 编辑:程序博客网 时间:2024/06/03 03:37

题目链接:

http://codeforces.com/contest/862/problem/B

题解:

二分图,染色法。

代码:

#include <map>#include <cmath>#include <vector>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define met(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3ftypedef long long ll;const int maxn = 1e5+10;vector<int> p[maxn];int color[maxn],visited[maxn];void dfs(int x,int c){    visited[x]=1;    color[x]=c;    for(int i=0;i<p[x].size();i++)    {        int y=p[x][i];        if(color[y]==0&&!visited[y])        {            dfs(y,-c);        }    }}int main(){    int n;    scanf("%d",&n);    for(int i=1;i<=n;i++)        p[i].clear();    int m=n-1;    for(int i=0;i<m;i++)    {        int x,y;        scanf("%d%d",&x,&y);        p[x].push_back(y);        p[y].push_back(x);    }    met(visited,0);    met(color,0);    dfs(1,1);    ll ans1=0,ans2=0;    for(int i=1;i<=n;i++)    {        if(color[i]==-1)            ans1++;        if(color[i]==1)            ans2++;    }    ll ans=ans1*ans2-n+1;    printf("%lld\n",ans);}
阅读全文
0 0
原创粉丝点击