https://www.luogu.com.cn/problem/P1330
#include <bits/stdc++.h> using namespace std; const int M = 2e5 + 5; int a[M], vis[M], color[M], ans, anss; vector<int> f[M]; void dfss(int now) { vis[now] = color[now] = 0; for (auto x : f[now]) if (vis[x]) dfss(x); } bool dfs(int now) { vis[now] = 1; if (color[now]) ans++; for (auto x : f[now]) if (!vis[x]) { color[x] = 1 - color[now]; if (!dfs(x)) return 0; } else if (color[now] == color[x]) return 0; return 1; } void solve() { int n, m, x, y; cin >> n >> m; while (m--) { cin >> x >> y; f[x].push_back(y); f[y].push_back(x); } for (int i = 1; i <= n; ++i) if (!vis[i]) { int cnt = 1e9; ans = 0; if (dfs(i)) cnt = min(cnt, ans); ans = 0; dfss(i); color[i] = 1; if (dfs(i)) cnt = min(cnt, ans); if (cnt == 1e9) { cout << "Impossible"; return; } anss += cnt; } cout << anss; } signed main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int t = 1; // cin>>t; while (t--) solve(); return 0; }

