Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <set>
- #include <map>
- #include <queue>
- using namespace std;
- #define pll pair<long,pair<long,long>>
- long res = -1;
- long rooms, arrows;
- long temp, temp2;
- vector<set<long>> a;
- set<long> used;
- bool dfs(long start, set<long> visited = {}) {
- visited.insert(start);
- set<long> connections = a[start];
- for (auto connection : connections) {
- if (visited.size() >= rooms-1) {
- return true;
- }
- if (visited.find(connection) == visited.end()) {
- if (dfs(connection, visited)) return true;
- }
- }
- return false;
- }
- void main()
- {
- cin >> rooms >> arrows;
- a.resize(rooms);
- for (int i = 0; i < arrows; i++) {
- cin >> temp >> temp2;
- a[temp - 1].insert(temp2 - 1);
- }
- for (int i = 0; i < rooms; i++) {
- if (dfs(i)) res = i + 1;
- }
- cout << res;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement