VK Cup 2012 Finals, Practice Session--C. Trails and Glades--欧拉回路

C. Trails and Glades

time limit per test

4 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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, xiyi (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, 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.

Examples

input

Copy

3 3
1 2
2 3
3 1

output

Copy

0

input

Copy

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

output

Copy

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.

 

题意:找一个从1点出发的欧拉回路,问最少加多少条边。

==联通块数量为1时为 奇度数节点个数/2

否则为 奇度数节点个数/2+不包含奇度数节点的联通块

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,b) for(register int i=(a);i<=(b);i++)
#define dep(i,a,b) for(register int i=(a);i>=(b);i--)
//priority_queue<int,vector<int>,less<int> >q;
int dx[]= {-1,1,0,0,-1,-1,1,1};
int dy[]= {0,0,-1,1,-1,1,1,-1};
const int maxn = 1000000+66;
const int maxm=100000+66;
const ll mod=1e9+7;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
const int INF=99999999;
int du[maxn];
int f[maxn];
int vis[maxn];
int sum[maxn];
int finds(int x)
{
    return x==f[x]?x:f[x]=finds(f[x]);
}
void unions(int x,int y)
{
    int fx=finds(x);
    int fy=finds(y);
    if(fx!=fy)
    {
        f[fx]=fy;
    }
    return ;
}
int main()
{
    int n,m;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        int u,v;
        rep(i,1,n)
        {
            f[i]=i;
            sum[i]=du[i]=0;
        }
        vis[1]=1;
        rep(i,1,m)
        {
            //f[i]=i;
            int u,v;
            scanf("%d %d",&u,&v);
            if(u!=v)
            {
                du[u]++;
                du[v]++;
                vis[u]++;
                vis[v]++;//num代表度数
                unions(u,v);
            }
            else
            {
                vis[v]++;
            }
        }
        int s1=0;
        rep(i,1,n)
        {
            if(du[i]&1)
            {
                sum[finds(i)]++;//---//
            }
        }
        int s2=0;
        int s3=0;
        rep(i,1,n)//奇度数节点个数/2+不含奇数度数的联通快
        {
            if(finds(i)==i&&vis[i])
            {
                if(sum[i])
                    s2+=sum[i];
                else
                    s3++;
                s1++;
            }
        }
        if(s1==1)
        {
            printf("%d\n",s2/2+s3);
        }
        else
        {
            printf("%d\n",s2/2+s3);
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值