【 CodeForces 209C】 【欧拉回路推结论+并查集计算联通分量】 【给定n点m边无向图,可能有自环和重边。 问最少添加多少条边后,使得图存在从点1出发发又回到点1的欧拉回路】

来源:互联网 发布:淘宝网的运营模式是() 编辑:程序博客网 时间:2024/05/21 07:50

传送门: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
3 31 22 33 1
output
0
input
2 51 11 21 22 21 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.


题意:
给定n点m边无向图,可能有自环和重边。 问最少添加多少条边后,使得图存在从点1出发又回到点1的欧拉回路(所给的边要保证经过一次)。 n,m ≤ 106
思路:
利用欧拉回路存在的性质,先求出每个连通块内度数是奇数的点的个数。 我们需要加边 以消除所有奇度数点。 然后我们还得把所有连通块连通起来。 可以发现,如果一个连通块包含奇数度数点,那 么就不需要额外加边就能与其他连通块连通 (想象把这些连通块的奇数点收尾相连)。 但如果一个连通块里没有奇度数点,那么必须额外花费1条边才能把它与其他部分连接起 来。 于是答案是: 

• 如果全图连通,则答案是奇数度数点的个数/2.

 • 否则答案是奇数度数点的个数/2+不含奇数度数点的连通块的个数。
注意:
有一些细节要考虑。 注意孤立点的特殊处理(有没有自环,有的话需要当做联通分量连入图中,没有的话就还是孤立点)。

代码:

#include <bits/stdc++.h>#define pr(x) cout << #x << "= " << x << "  " ;#define pl(x) cout << #x << "= " << x << endl;#define ll __int64using  namespace  std;template<class T> void read(T&num) {    char CH; bool F=false;    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());    F && (num=-num);}int stk[70], tp;template<class T> inline void print(T p) {    if(!p) { puts("0"); return; }    while(p) stk[++ tp] = p%10, p/=10;    while(tp) putchar(stk[tp--] + '0');    putchar('\n');}const int N=1e6+10;int p[N],deg[N];int flag[N];//是否含有奇数点的联通分量int vis[N];int n,m;int Find(int x){  return p[x]==x?x:p[x]=Find(p[x]);}void unite(int x,int y){  x=Find(x);  y=Find(y);  if(x!=y)p[y]=x;}int  main(){ /* #ifndef ONLINE_JUDGE  freopen("in.txt","r",stdin);  #endif*/  read(n);read(m);  for(int i=1; i<=n; i++)p[i]=i;  vis[1]=1;  for(int i=1; i<=m; i++){  int u,v;  read(u);read(v);  if(u!=v){  deg[u]++; deg[v]++;  vis[u]++; vis[v]++;  unite(u, v);  }  else vis[u]++;  }  for(int i=1; i<=n; i++){  if(deg[i]&1){  flag[Find(i)]++;  }  }  int tong=0,ans=0,k=0;  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)print(ans/2);//全图连通  else print(ans/2+tong);  return 0;}




0 0
原创粉丝点击