POJ-3177.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <utility>
  4. #include <vector>
  5. using namespace std;
  6. struct E { int to, id; };
  7. int main() {
  8. ios::sync_with_stdio(false);
  9. cin.tie(nullptr);
  10. int n, m; cin >> n >> m;
  11. vector<vector<E>> g(n);
  12. vector<pair<int, int>> edges(m);
  13. for (int id = 0; id < m; ++id) {
  14. int u, v; cin >> u >> v; --u; --v; edges[id] = {u, v};
  15. g[u].push_back({v, id}); g[v].push_back({u, id});
  16. }
  17. vector<int> dfn(n), low(n), par(n, -1), pe(n, -1);
  18. vector<size_t> it(n); vector<bool> bridge(m); int timer = 0;
  19. for (int root = 0; root < n; ++root) if (!dfn[root]) {
  20. vector<int> st{root}; dfn[root] = low[root] = ++timer;
  21. while (!st.empty()) {
  22. int u = st.back();
  23. if (it[u] < g[u].size()) {
  24. E e = g[u][it[u]++];
  25. if (e.id == pe[u]) continue;
  26. if (!dfn[e.to]) {
  27. par[e.to] = u; pe[e.to] = e.id;
  28. dfn[e.to] = low[e.to] = ++timer; st.push_back(e.to);
  29. } else low[u] = min(low[u], dfn[e.to]);
  30. } else {
  31. st.pop_back();
  32. if (par[u] != -1) {
  33. if (low[u] > dfn[par[u]]) bridge[pe[u]] = true;
  34. low[par[u]] = min(low[par[u]], low[u]);
  35. }
  36. }
  37. }
  38. }
  39. vector<int> comp(n, -1); int count = 0;
  40. for (int s = 0; s < n; ++s) if (comp[s] == -1) {
  41. vector<int> st{s}; comp[s] = count;
  42. while (!st.empty()) {
  43. int u = st.back(); st.pop_back();
  44. for (E e : g[u]) if (!bridge[e.id] && comp[e.to] == -1) {
  45. comp[e.to] = count; st.push_back(e.to);
  46. }
  47. }
  48. ++count;
  49. }
  50. vector<int> degree(count);
  51. for (int id = 0; id < m; ++id) if (bridge[id]) {
  52. auto [u, v] = edges[id]; ++degree[comp[u]]; ++degree[comp[v]];
  53. }
  54. int leaves = 0;
  55. for (int d : degree) leaves += d == 1;
  56. cout << (leaves + 1) / 2 << '\n';
  57. }