POJ-1144.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <sstream>
  4. #include <string>
  5. #include <vector>
  6. using namespace std;
  7. struct E { int to, id; };
  8. int main() {
  9. ios::sync_with_stdio(false);
  10. cin.tie(nullptr);
  11. int n;
  12. string line;
  13. while (cin >> n && n != 0) {
  14. getline(cin, line);
  15. vector<vector<E>> g(n);
  16. int edge_id = 0;
  17. while (getline(cin, line) && line != "0") {
  18. istringstream input(line);
  19. int u, v; input >> u; --u;
  20. while (input >> v) {
  21. --v;
  22. g[u].push_back({v, edge_id});
  23. g[v].push_back({u, edge_id});
  24. ++edge_id;
  25. }
  26. }
  27. vector<int> dfn(n), low(n);
  28. vector<bool> cut(n, false);
  29. int timer = 0;
  30. auto dfs = [&](auto &&self, int u, int parent_edge) -> void {
  31. dfn[u] = low[u] = ++timer;
  32. int children = 0;
  33. for (E e : g[u]) {
  34. if (e.id == parent_edge) continue;
  35. if (!dfn[e.to]) {
  36. ++children; self(self, e.to, e.id);
  37. low[u] = min(low[u], low[e.to]);
  38. if (parent_edge != -1 && low[e.to] >= dfn[u]) cut[u] = true;
  39. } else low[u] = min(low[u], dfn[e.to]);
  40. }
  41. if (parent_edge == -1 && children >= 2) cut[u] = true;
  42. };
  43. for (int v = 0; v < n; ++v) if (!dfn[v]) dfs(dfs, v, -1);
  44. cout << count(cut.begin(), cut.end(), true) << '\n';
  45. }
  46. }