http://acm.hdu.edu.cn/showproblem.php?pid=3018
3 3 1 2 2 3 1 3 4 2 1 2 3 4
1 2HintNew ~~~ Notice: if there are no road connecting one town ,tony may forget about the town. In sample 1,tony and his friends just form one group,they can start at either town 1,2,or 3. In sample 2,tony and his friends must form two group.
一种为含奇数点的 一种为只含偶数点的
对于含奇数点的 笔画数=奇数点个数/2
对于只含偶数点的 存在欧拉回路 笔画数=1
对于整张图 则 ans=总奇数点/2+只含偶数点的集合个数
#include <stdio.h> #include <string.h> #include <iostream> using namespace std; int fa[100005],way[100005],mark[100005]; int n,m,ans; void init() { for(int i=0;i<=n;i++) { fa[i]=i; way[i]=0; mark[i]=0; } } int find(int x) { if(fa[x]!=x) return fa[x]=find(fa[x]); return fa[x]; } int add(int x,int y) { x=find(x); y=find(y); if(x!=y) { fa[x]=y; } } void solve() { int i,r; ans=0; for(i=1;i<=n;i++) { if(way[i]%2==1) { r=find(i); mark[r]=1; ans++; } } ans/=2; for(i=1;i<=n;i++) { if(way[i]>0) { r=find(i); if(mark[r]==0&&i==r) { ans++; } } } } int main() { int i,j,t,l,r; while(~scanf("%d%d",&n,&m)) { init(); for(i=1;i<=m;i++) { scanf("%d%d",&l,&r); way[l]++; way[r]++; add(l,r); } solve(); printf("%d\n",ans); } return 0; }