1. 程式人生 > >CodeforcesRound435 div2B:Mahmoud and Ehab and the bipartiteness

CodeforcesRound435 div2B:Mahmoud and Ehab and the bipartiteness

fff bip vector mit type stack 結果 [0 ont

題意:給定一個二分圖的點數 每條邊的信息 求還能最多加幾條邊

很好的一個基礎題 讓我這個蒟蒻再次意識到了我知識學的流於形式‘

“顯然”有結論 結果是二分圖的兩端點數乘積-已有邊數+1

那麽該如何判斷兩側都有幾個元素呢

其實只需要遍歷一遍二分圖....

我的二分圖匹配學的真是差啊

#pragma GCC optimize("O2")
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<bitset>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<stack>
#include<set>
#include<map>
#include<limits.h>
#include<ctime>
#define N 100001
typedef long long ll;
const int inf=0x3fffffff;
const int maxn=2017;
const int mod=1000000007;
using namespace std;
inline int read()
{
    int f=1,x=0;char ch=getchar();
    while(ch>‘9‘||ch<‘0‘)
    {
        if(ch==‘-‘)
        f=-1;
        ch=getchar();
    }
    while(ch<=‘9‘&&ch>=‘0‘)
    {
        x=(x<<3)+(x<<1)+ch-‘0‘;
        ch=getchar();
    }
    return f*x;
}
struct tsdl{
 int w,to,next;
} edge[N*4];
int head[N],cnt[N],tot,vis[N];
void add(int ui,int vi)
{
 edge[tot].next=head[ui];
 //edge[tot].w=wi;
 edge[tot].to=vi;
 head[ui]=tot++;
}
void dfs(int x,int side)
{
  cnt[side]++;
  vis[x]=1;
  for(int i=head[x];i!=-1;i=edge[i].next)
  {
    int v=edge[i].to;
    if(!vis[v])
    {
      vis[v]=1;
      dfs(v,side^1);
    }
  }
}
int main()
{
  memset(head,-1,sizeof head);
  int n=read();
  for(int i=1;i<n;i++)
  {
    int a=read(),b=read();
    add(a,b);
    add(b,a);
  }
  dfs(1,0);
  cout<<(ll)cnt[0]*cnt[1]-(n-1)<<endl;
}

CodeforcesRound435 div2B:Mahmoud and Ehab and the bipartiteness