Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <vector>
- #include <string>
- std::vector<std::vector<int>> graph;
- std::vector<short> used;
- std::string res;
- void dfs( int v )
- {
- used[v] = 1;
- for (auto to : graph[v])
- {
- switch (used[to])
- {
- case 0:
- dfs(to);
- break;
- case 1:
- printf("-1");
- exit(0);
- break;
- }
- }
- used[v] = 2;
- res += std::to_string(v + 1) + " ";
- }
- int main()
- {
- // read graph
- int n = 0, m = 0;
- scanf("%i%i", &n, &m);
- if (n < 1 || m < 0 || n > 1e5 || m > 1e5) { printf("-1"); return 0; }
- graph.resize(n);
- for (int k = 0, i = 0, j = 0; k < m; k++)
- scanf("%i%i", &i, &j), graph[i - 1].push_back(j - 1);
- // check graph
- used.resize(n);
- for (int i = 0; i < n; i++)
- if (!used[i])
- dfs(i);
- std::reverse(res.begin(), res.end());
- printf("%s", res.c_str());
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement