#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; int nMarked = 0; unsigned N, M; bool count_pipes(const int st, const unsigned nodes) { if (stations[st].marked) return false; if (st == collector) return nodes == N; for (auto child : stations[st].children) if (count_pipes(child, nodes + 1)) return true; return false; } int main() { std::cin >> N >> M; for (unsigned i = 0; i < N; i++) stations.push_back(Station()); for (unsigned i = 0; i < N; i++) { isSource.push_back(true); isCollector.push_back(true); } for (unsigned 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 << (count_pipes(source, 1) ? M - (N - 1) : 0) << std::endl; return 0; } /* 5 6 2 4 3 5 2 5 1 4 2 3 5 1 */ /* 4 4 1 2 1 3 2 4 3 4 */ /* 5 5 1 2 1 3 2 4 3 4 4 5 */