passo o Pai e Verice Atual //cout U ' ' : Using Namespace Const Int Int Int Bool Bool Void Int Int

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

1 #include<bits/stdc++.

h>
2
3 using namespace std;
4
5 const int MAX = 11234;
6
7 int n,m;
8 vector<int> adj[MAX];
9 vector<bool> visited;
10
11 bool cycle = 0;
12
13 void dfs(int u, int p) //passo o pai e verice atual
14 {
15 visited[u]=1;
16 //cout << u << ' ';
17 for(auto x:adj[u]){
18 if(visited[x]==0)
19 dfs(x,u);
20 else if (x!=p) cycle=1;
21 }
22 }
23
24 bool is_cyclical()
25 {
26 dfs(0,0);
27 if(cycle) return 1;
28 return 0;
29 }
30
31 int main()
32 {
33 visited.assign(MAX,false);
34 cin>>n>>m; //Vertice e arestas
35 for(int i=0;i<m;i++){
36 int a,b;
37 cin>>a>>b;
38 adj[a].push_back(b);
39 adj[b].push_back(a);
40 }
41 is_cyclical() ? cout << "SIM" : cout << "NAO" << endl;
42 return 0;
43 }
44
45
46
47

You might also like