Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- using namespace std;
- int n, m;
- vector<int> graph[1005];
- bool visited[1005], dfs_visited[1005];
- bool dfs(int node) {
- visited[node] = true;
- dfs_visited[node] = true;
- for(int i = 0; i < graph[node].size(); i++) {
- int sosed = graph[node][i];
- if(!visited[sosed]) {
- if(dfs(sosed)) {
- return true;
- }
- }
- else if(dfs_visited[sosed]) {
- return true;
- }
- }
- dfs_visited[node] = false;
- return false;
- }
- int main() {
- cin >> n >> m;
- memset(visited, false, sizeof visited);
- memset(dfs_visited, false, sizeof dfs_visited);
- for(int i = 0; i < m; i++) {
- int a,b;
- cin >> a >> b;
- graph[a].push_back(b);
- }
- for(int i = 0; i < n; i++) {
- if(!visited[i] and dfs(i)) {
- cout << "DA" << endl;
- return 0;
- }
- }
- cout << "NE" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement