Codeforces 209 C. Trails and Glades

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.

Input

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.

Output

Print the single integer — the answer to the problem. If Vasya’s walk is possible without adding extra trails, print0, otherwise print the minimum number of trails the authorities need to add to the park in order to make Vasya’s walk possible.

Examples

input

3 3
1 2
2 3
3 1

output

0

input

2 5
1 1
1 2
1 2
2 2
1 2

output

1

Note

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.

Solution

先跑dfs求出每个联通块的奇度点个数 然后从1开始 如果一个块不是一个点 就和当前的合并 最后合并成大联通块,大联通块的答案为奇数度点个数/2.

Code

#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
typedef long long LL;
const int maxn = 1000005;
inline int getint() {
    int r = 0; bool z = true; char c = getchar();
    for (; '0' > c || c > '9'; c = getchar()) if (c == '-') z = false;
    for (; '0' <= c && c <= '9'; c = getchar()) r = r * 10 - '0' + c;
    return z ? r : (-r);
}
struct edge_type {int to, next; } edge[maxn<<1];
int cnte, h[maxn], cnt[maxn], du[maxn], x, y, tot, n, m, ans;
bool vis[maxn], ava[maxn];
void ins(int x, int y) {
    edge[++cnte].to = y;
    edge[cnte].next = h[x];
    h[x] = cnte;
}
void dfs(int now) {
    vis[now] = true;
    if (du[now] & 1) ++cnt[tot];
    for (int i = h[now]; i; i = edge[i].next)
        if (!vis[edge[i].to])
            dfs(edge[i].to);
}
int combine(int a, int b) {
    ++ans;
    if (a == 0 && b == 0) return 2;
    if (a == 0 || b == 0) return a + b;
    return a + b - 2;
}
int main() {
    n = getint(); m = getint();
    for (int i = 0; i < m; ++i) {
        x = getint();
        y = getint();
        ins(x, y);
        ins(y, x);
        ++du[x];
        ++du[y];
    }
    for (int i = 1; i <= n; ++i)
        if (!vis[i]) {
            ++tot;
            if (du[i] == 0) {vis[i]=true;ava[tot]=false;}
            else {dfs(i);ava[tot]=true;}
        }
    int nowdu = cnt[1];
    for (int i = 2; i <= tot; ++i)
        if (ava[i])
            nowdu = combine(nowdu, cnt[i]);
    ans += nowdu / 2;
    printf("%d\n", ans);
    return 0;
}

 

posted on 2016-11-08 10:14  MagHSK  阅读(207)  评论(0编辑  收藏  举报