| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- #include <algorithm>
- #include <iostream>
- #include <utility>
- #include <vector>
- using namespace std;
- struct E { int to, id; };
- int main() {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- int n, m; cin >> n >> m;
- vector<vector<E>> g(n);
- vector<pair<int, int>> edges(m);
- for (int id = 0; id < m; ++id) {
- int u, v; cin >> u >> v; --u; --v; edges[id] = {u, v};
- g[u].push_back({v, id}); g[v].push_back({u, id});
- }
- vector<int> dfn(n), low(n), par(n, -1), pe(n, -1);
- vector<size_t> it(n); vector<bool> bridge(m); int timer = 0;
- for (int root = 0; root < n; ++root) if (!dfn[root]) {
- vector<int> st{root}; dfn[root] = low[root] = ++timer;
- while (!st.empty()) {
- int u = st.back();
- if (it[u] < g[u].size()) {
- E e = g[u][it[u]++];
- if (e.id == pe[u]) continue;
- if (!dfn[e.to]) {
- par[e.to] = u; pe[e.to] = e.id;
- dfn[e.to] = low[e.to] = ++timer; st.push_back(e.to);
- } else low[u] = min(low[u], dfn[e.to]);
- } else {
- st.pop_back();
- if (par[u] != -1) {
- if (low[u] > dfn[par[u]]) bridge[pe[u]] = true;
- low[par[u]] = min(low[par[u]], low[u]);
- }
- }
- }
- }
- vector<int> comp(n, -1); int count = 0;
- for (int s = 0; s < n; ++s) if (comp[s] == -1) {
- vector<int> st{s}; comp[s] = count;
- while (!st.empty()) {
- int u = st.back(); st.pop_back();
- for (E e : g[u]) if (!bridge[e.id] && comp[e.to] == -1) {
- comp[e.to] = count; st.push_back(e.to);
- }
- }
- ++count;
- }
- vector<int> degree(count);
- for (int id = 0; id < m; ++id) if (bridge[id]) {
- auto [u, v] = edges[id]; ++degree[comp[u]]; ++degree[comp[v]];
- }
- int leaves = 0;
- for (int d : degree) leaves += d == 1;
- cout << (leaves + 1) / 2 << '\n';
- }
|