标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2461 Accepted Submission(s): 965
#include <iostream> #include <string.h> #include <vector> using namespace std; const int MAXN=100005; int n,m; int deg[MAXN],vis[MAXN]; vector<int> arc[MAXN]; void dfs(int u,int& dep,int& odd) { dep++; if(deg[u]&1) odd++; vis[u]=1; for(int i=0;i<arc[u].size();i++) { int v=arc[u][i]; if(!vis[v]) { dfs(v,dep,odd); } } } int main() { while(cin>>n>>m) { memset(deg,0,sizeof(deg)); memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++) arc[i].clear(); for(int i=0;i<m;i++) { int u,v; cin>>u>>v; arc[u].push_back(v); arc[v].push_back(u); deg[u]++; deg[v]++; } int res=0; for(int i=1;i<=n;i++) { if(!vis[i]) { int dep=0,odd=0; dfs(i,dep,odd); if(dep>1)//连通分量存在边,即不为孤立点 { if(odd==0) res++;//若奇数度的结点个数为0,则只需派一组人 else res+=(odd/2);//派odd/2组人 } } } cout<<res<<endl; } return 0; }
标签:
原文地址:http://www.cnblogs.com/program-ccc/p/5654045.html