Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <map>
- #include <set>
- #include <vector>
- #include <fstream>
- #include <iostream>
- #include <stack>
- using namespace std;
- map<int, vector<int> > graph;
- stack<int> st;
- set<int> used;
- bool dfs(int S, int T) {
- used.insert(S);
- st.push(S);
- while (!st.empty()) {
- int v = st.top(); st.pop();
- if (v == T) return true;
- vector<int> & to = graph[v];
- for (int i = 0; i < to.size(); ++i) if (!used.count(to[i])) {
- used.insert(to[i]);
- st.push(to[i]);
- }
- }
- return false;
- }
- int main() {
- setlocale(LC_ALL, ".1251");
- ifstream fin("input.txt");
- int s, t, m;
- fin >> m >> s >> t;
- while (m--) {
- int x, y;
- fin >> x >> y;
- graph[x].push_back(y);
- }
- cout << (dfs(s, t) ? "Да\n" : "Нет\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement