题意:让你安排队伍,给你相互对立的关系,这些关系的人不能在一个队,然后尽量分成两个人数相同的队伍
解法:并查集,判断环,如果是奇数环,减1就好。。然后就没了
#include<iostream>
#include<cstdio>
#include<string.h>
#include<string>
#include<stack>
#include<set>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#define LL __int64
#define lll unsigned long long
#define MAX 1000009
#define eps 1e-8
#define INF 0xfffffff
#define pi 2*acos(0.0)
#define mod 10007
using namespace std;
int N,M;
int num;
int father[MAX];
void init()
{
for(int i = 1; i<=N; i++)
father[i] = -1;
}
int Find(int x)
{
if(father[x]<0)
return x;
else
return father[x]=Find(father[x]);
}
void Union(int x,int y)
{
int xx=Find(x);
int yy=Find(y);
if( xx == yy )
{
if(-father[xx]%2==1)
{
num--;
}
}
if( father[xx] < father[yy] )
{
father[xx] += father[yy];
father[yy] = xx;
}
else
{
father[yy] += father[xx];
father[xx] = yy;
}
}
int main()
{
int x,y;
int flag;
while(~scanf("%d%d",&N,&M))
{
init();
num = N;
while(M--)
{
scanf("%d%d",&x,&y);
Union(x,y);
}
if(num%2==1)
{
num--;
}
printf("%d\n",N - num);
}
return 0;
}