题意:判断能否构成欧拉回路,不能的话输出最小需要添加多少条边
题解:并查集处理出联通分量,输入时处理出奇度数点个数,结果即为 奇度数点个数/2+不含奇度数点的联通分量个数,1节点自动算作一个联通分量 不管是否有边连接
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <list>
#include <queue>
#include <map>
#include <stack>
using namespace std;
#define L(i) i<<1
#define R(i) i<<1|1
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-3
#define maxn 100010
#define MOD 1000000007
int n,m,ans;
int num[1000010],flag[1000010];
int fa[1000010],vis[1000010];
int Find(int x)
{
return fa[x] = x == fa[x]?x:Find(fa[x]);
}
void Union(int x,int y)
{
int fx = Find(x);
int fy = Find(y);
if(fx != fy)
fa[fy] = fx;
}
int main()
{
int t,C = 1;
while(scanf("%d%d",&n,&m) != EOF)
{
memset(num,0,sizeof(num));
memset(flag,0,sizeof(flag));
memset(vis,0,sizeof(vis));
for(int i = 1; i <= n; i++)
fa[i] = i;
int u,v;
vis[1] = 1;
for(int i = 0; i < m; i++)
{
scanf("%d%d",&u,&v);
if(u != v)
{
num[u]++;
num[v]++;
vis[u]++;
vis[v]++;
Union(u,v);
}
else
vis[u]++;
}
int ans = 0,k = 0,tong = 0;
for(int i = 1; i <= n; i++)
if(num[i]&1)
flag[Find(i)]++;
for(int i = 1; i <= n; i++)
if(vis[i] && Find(i)== i)
{
if(flag[i])
ans += flag[i];
else
tong++;
k++;
}
if(k == 1)
printf("%d\n",ans/2);
else
printf("%d\n",ans/2+tong);
}
return 0;
}