Description
Ant Country consist of N towns.There are M roads connecting the towns.
Ant Tony,together with his friends,wants to go through every part of the country.
They intend to visit every road , and every road must be visited for exact one time.However,it may be a mission impossible for only one group of people.So they are trying to divide all the people into several groups,and each may start at different town.Now tony wants to know what is the least groups of ants that needs to form to achieve their goal.
Input
Input contains multiple cases.Test cases are separated by several blank lines. Each test case starts with two integer N(1<=N<=100000),M(0<=M<=200000),indicating that there are N towns and M roads in Ant Country.Followed by M lines,each line contains two integers a,b,(1<=a,b<=N) indicating that there is a road connecting town a and town b.No two roads will be the same,and there is no road connecting the same town.
Output
For each test case ,output the least groups that needs to form to achieve their goal.
Sample Input
3 3
1 2
2 3
1 3
4 2
1 2
3 4
Sample Output
1
2
题意:
给你无向图的N个点和M条边,保证这M条边都不同且不会存在同一点的自环边,现在问你至少要几笔才能所有边都画一遍.(一笔画的时候笔不离开纸)。
题解:
对于一个连通图,笔画次数=奇度顶点数/2。如果没有奇度顶点,就可以一笔画。
对于每个联通块,显然存在三种情况。
1.联通块中只存在一个点。需要0笔。
2.联通块是一个欧拉图/半欧拉图 需要1笔。
3.其他情况。此时显然存在偶数个奇顶点。(实际上,对于一个无向图,若存在奇顶点,则一定存在偶数个。)感性理解一下,我们每一笔至少可以消掉2个奇顶点,而对于最后的两个奇顶点和偶度顶点,则可以一笔画。【好水啊2333333
有个博客讲的很好:大佬的详解
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 100000 + 10;
const int M = 200000 + 10;
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n,m;
struct node{
int pre,v;
}e[M<<1];
int tot=0,head[N];
void addedge(int from,int to){
e[++tot].pre=head[from],e[tot].v=to;
head[from]=tot;
}
int in[N],num[N],odd[N],bl[N];
void dfs(int u,int c){
bl[u]=c,++num[c];if(in[u]&1) ++odd[c];
for(int i=head[u];i;i=e[i].pre){
int v=e[i].v;
if(bl[v]) continue;
dfs(v,c);
}
}
#define ms(x,y) memset(x,y,sizeof(x))
void update(){
ms(in,0);ms(num,0);ms(odd,0);ms(bl,0);
ms(head,0);tot=0;
}
int main(){
while(scanf("%d%d",&n,&m)==2){
update();
for(register int i=1;i<=m;i++){
int u=read(),v=read();
addedge(u,v);addedge(v,u);
++in[u],++in[v];
}
int cnt=0,ans=0;
for(int i=1;i<=n;i++){
if(!bl[i]){
dfs(i,++cnt);
if(num[cnt]>1&&odd[cnt]==0) ++ans;
else if(num[cnt]>1&&odd[cnt]) ans+=odd[cnt]>>1;
}
}
printf("%d\n",ans);
}
return 0;
}