1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| #include <bits/stdc++.h> #define ios ios::sync_with_stdio(0),cin.tie(0) #define lld long long #define N 100005 using namespace std; int n,m,ans = 0,cnt[2]; vector<int> edge[N];
int color[N];
bool dfs(int cur,int c){ color[cur] = c; cnt[c]+=1; bool flag = 1; for(auto i:edge[cur]){ if(color[i]==color[cur])return 0; if(color[i]==-1)flag = (flag && dfs(i,!c)); } return flag; }
void solve(){ memset(color,0,sizeof(color)); for(int i=0;i<n;i++)color[i] = -1; for(int i=0;i<m;i++){ int a,b;cin>>a>>b;a--;b--; edge[a].push_back(b); edge[b].push_back(a); } for(int i=0;i<n;i++){ if(color[i]==-1){ cnt[0] = 0;cnt[1] = 0; if(!dfs(i,0)){ cout<<0<<endl; return; } ans += min(cnt[0],cnt[1]); } } cout<<ans<<endl; }
signed main(){ cin>>n>>m; solve(); }
|