无向图一笔画需要多少次(欧拉路径的条数)
答案:奇数点的个数 / 2
这题需要维护联通块,用并查集即可
#include <iostream> #include <algorithm> #include <cstring> #include <stack> using namespace std ; const int N=2e5+3,M=N; int all,nxt[M],go[N],hd[N]; int n,m; void add_(int x,int y){ nxt[++all]=hd[x]; hd[x]=all; go[all]=y; } int fa[N],deg[N],odd[N]; int find(int x){ return x==fa[x]?x:fa[x]=find(fa[x]); } signed main(){ int i,x,y; while(cin>>n>>m){ for(i=1;i<=n;i++) hd[i]=deg[i]=odd[i]=0,fa[i]=i; all=0; for(i=1;i<=m;i++){ cin>>x>>y,add_(x,y),add_(y,x); deg[x]++,deg[y]++; int fx=find(x),fy=find(y); if(fx!=fy) fa[fy]=fx; } for(i=1;i<=n;i++){ fa[i]=find(i); if(deg[i]&1) odd[fa[i]]++; } int ans=0; for(i=1;i<=n;i++) if(fa[i]==i){ if(deg[i]==0) continue; // if(odd[i]==0) ans++; else ans+=odd[i]/2; } cout<<ans<<endl; } }
标签:fx,fa,int,Ant,3.7,add,10108,include,find From: https://www.cnblogs.com/towboa/p/17134753.html