CodeForces 209C 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
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.

 

并查集判联通块。

如果只有一个联通块,答案为奇度数节点数/2,

如果有多个联通块,答案为奇度数节点数/2 + 没有奇度数点的联通块个数

由于1点必须在欧拉回路中,1点默认要设为存在。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #define LL long long
 6 using namespace std;
 7 const int mx[5]={0,1,0,-1,0};
 8 const int my[5]={0,0,1,0,-1};
 9 const int INF=1e9;
10 const int mxn=1000010;
11 int read(){
12     int x=0,f=1;char ch=getchar();
13     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
14     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
15     return x*f;
16 }
17 int n,m;
18 int fa[mxn];
19 int find(int x){
20     if(fa[x]==x)return x;
21     return fa[x]=find(fa[x]);    
22 }
23 void init(int x){
24     for(int i=1;i<=x;i++)fa[i]=i;
25 }
26 int deg[mxn];
27 int cnt[mxn],ans=0;
28 bool flag[mxn];
29 bool vis[mxn];
30 int cct=0;
31 int main(){
32     n=read();m=read();
33     int i,j,u,v;
34     init(n);
35     vis[1]=1;
36     for(i=1;i<=m;i++){
37         u=read();v=read();
38         if(u!=v){
39             deg[u]++;
40             deg[v]++;
41             vis[u]=1;vis[v]=1;
42             u=find(u);v=find(v);
43             fa[u]=v;
44         }
45         else vis[u]=1;
46     }
47     for(i=1;i<=n;i++){
48         if(!vis[i])continue;
49         if(find(i) ==i)cct++;
50     }
51     for(i=1;i<=n;i++){
52         if(!vis[i] || deg[i]%2==0)continue;
53         int x=find(i);
54         cnt[x]++;
55     }
56     int res=0;
57     for(i=1;i<=n;i++){
58         if(!vis[i])continue;
59         if(find(i)==i){        
60             if(!cnt[i])ans++;
61             else res+=cnt[i];
62         }
63     }
64     if(cct==1)printf("%d\n",res/2);
65     else printf("%d\n",ans+res/2);
66     return 0;
67 }

 

转载于:https://www.cnblogs.com/SilverNebula/p/6129381.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值