fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. vector<int> ke[1001];
  4. bool visited[1001];
  5. int parent[1001];
  6. bool DFS(int u){
  7. visited[u] = true;
  8. for(int x : ke[u]){
  9. if(!visited[x]){
  10. parent[x] = u;
  11. if(DFS(x)){
  12. return true;
  13. }
  14. }
  15. else if(u != parent[x]){
  16. return true;
  17. }
  18. }
  19. return false;
  20. }
  21.  
  22. int main(){
  23. int m , n;
  24. cin >> m >> n ;
  25. for(int i = 1 ; i <= n ; i++){
  26. int x,y;
  27. cin >> x >> y ;
  28. ke[x].push_back(y);
  29. ke[y].push_back(x);
  30. }
  31. if(DFS(1)){
  32. cout << "1";
  33. }
  34. else cout << "0";
  35. }
  36.  
Success #stdin #stdout 0.01s 5296KB
stdin
10 11
10 5
10 4
10 1
10 3
5 2
5 4
10 8
5 3
5 1

10 6
10 9
stdout
1