题目链接:
Ant TripTime Limit: 2000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
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.
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N=1e6+5; const ll mod=1e9+7; int n,m,vis[N],ind[N],u,v; vector<int>ve[N]; queue<int>qu; int bfs(int num) { int cnt=0; qu.push(num); vis[num]=1; while(!qu.empty()) { int fr=qu.front(); qu.pop(); if(ind[fr]%2==1)cnt++; int len=ve[fr].size(); for(int i=0;i<len;i++) { int y=ve[fr][i]; if(!vis[y]) { vis[y]=1; qu.push(y); } } } return cnt; } int main() { while(scanf("%d%d",&n,&m)!=EOF) { for(int i=0;i<=n;i++) { ind[i]=vis[i]=0; ve[i].clear(); } for(int i=0;i<m;i++) { scanf("%d%d",&u,&v); ve[u].push_back(v); ve[v].push_back(u); ind[u]++; ind[v]++; } for(int i=1;i<=n;i++) { if(ind[i]==0)vis[i]=1; } int ans=0; for(int i=1;i<=n;i++) { if(!vis[i]) { int s=bfs(i); if(s==0)ans++; else ans+=s/2; } } printf("%d\n",ans); } return 0; }