Vasya went for a walk in the park. The park has n glades, numbered from 1 to n. There are m trails between the glades. The trails are numbered from 1 to m, where the i-th trail connects glades xi and yi. The numbers of the connected glades may be the same (xi = yi), which means that a trail connects a glade to itself. Also, two glades may have several non-intersecting trails between them.
Vasya is on glade 1, he wants to walk on all trails of the park exactly once, so that he can eventually return to glade 1. Unfortunately, Vasya does not know whether this walk is possible or not. Help Vasya, determine whether the walk is possible or not. If such walk is impossible, find the minimum number of trails the authorities need to add to the park in order to make the described walk possible.
Vasya can shift from one trail to another one only on glades. He can move on the trails in both directions. If Vasya started going on the trail that connects glades a and b, from glade a, then he must finish this trail on glade b.
The first line contains two integers n and m (1 ≤ n ≤ 106; 0 ≤ m ≤ 106) — the number of glades in the park and the number of trails in the park, respectively. Next m lines specify the trails. The i-th line specifies the i-th trail as two space-separated numbers, xi, yi(1 ≤ xi, yi ≤ n) — the numbers of the glades connected by this trail.
Print the single integer — the answer to the problem. If Vasya's walk is possible without adding extra trails, print 0, otherwise print the minimum number of trails the authorities need to add to the park in order to make Vasya's walk possible.
3 3
1 2
2 3
3 1
0
2 5
1 1
1 2
1 2
2 2
1 2
1
In the first test case the described walk is possible without building extra trails. For example, let's first go on the first trail, then on the second one, and finally on the third one.
In the second test case the described walk is impossible without adding extra trails. To make the walk possible, it is enough to add one trail, for example, between glades number one and two.
并查集判联通块。
如果只有一个联通块,答案为奇度数节点数/2,
如果有多个联通块,答案为奇度数节点数/2 + 没有奇度数点的联通块个数
由于1点必须在欧拉回路中,1点默认要设为存在。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define LL long long
using namespace std;
const int mx[]={,,,-,};
const int my[]={,,,,-};
const int INF=1e9;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m;
int fa[mxn];
int find(int x){
if(fa[x]==x)return x;
return fa[x]=find(fa[x]);
}
void init(int x){
for(int i=;i<=x;i++)fa[i]=i;
}
int deg[mxn];
int cnt[mxn],ans=;
bool flag[mxn];
bool vis[mxn];
int cct=;
int main(){
n=read();m=read();
int i,j,u,v;
init(n);
vis[]=;
for(i=;i<=m;i++){
u=read();v=read();
if(u!=v){
deg[u]++;
deg[v]++;
vis[u]=;vis[v]=;
u=find(u);v=find(v);
fa[u]=v;
}
else vis[u]=;
}
for(i=;i<=n;i++){
if(!vis[i])continue;
if(find(i) ==i)cct++;
}
for(i=;i<=n;i++){
if(!vis[i] || deg[i]%==)continue;
int x=find(i);
cnt[x]++;
}
int res=;
for(i=;i<=n;i++){
if(!vis[i])continue;
if(find(i)==i){
if(!cnt[i])ans++;
else res+=cnt[i];
}
}
if(cct==)printf("%d\n",res/);
else printf("%d\n",ans+res/);
return ;
}