#include #include #include struct Station { Station():marked(false){} bool marked; std::vector children; }; std::vector stations; std::vector isSource; std::vector isCollector; int source = 0, collector = 0; unsigned count_pipes(const int st, const unsigned pipes) { if (stations[st].marked) return 0; if (st != collector) stations[st].marked = true; if (st == collector) return pipes; unsigned result = 0; for (auto station : stations[st].children) result += count_pipes(station, pipes + 1); return result; } int main() { int N, M; std::cin >> N >> M; for (int i = 0; i < N; i++) stations.push_back(Station()); for (int i = 0; i < N; i++) { isSource.push_back(true); isCollector.push_back(true); } for (int i = 0; i < M; i++) { int s, t; std::cin >> s >> t; s--; t--; stations[s].children.push_back(t); isSource[t] = false; while (!isSource[source]) source++; isCollector[s] = false; while(!isCollector[collector]) collector++; } std::cout << M - count_pipes(source, 0) << std::endl; } /* 5 6 2 4 3 5 2 5 1 4 2 3 5 1 */ /* 4 4 1 2 1 3 2 4 3 4 */