kananasgarli90

Is it a Tree?

Sep 14th, 2020
876
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int n, adj[101][101], color[101], til, tepe;
  5.  
  6. void dfs(int s){
  7.     color[s] = 1;
  8.     tepe++;
  9.     for(int  i = 1; i <= n; i++){
  10.         if(adj[s][i] == 1 && color[i] == 0){
  11.             dfs(i);
  12.         }
  13.     }
  14. }
  15. int main()
  16. {
  17.     cin>>n;
  18.     for(int i = 1; i <= n; i++){
  19.         for(int j = 1; j <= n; j++){
  20.             cin>>adj[i][j];
  21.             if(adj[i][j] == 1){
  22.                 til++;
  23.             }
  24.         }
  25.     }
  26.     dfs(1);
  27.     if(tepe == n && til/2 == n-1){
  28.         cout<<"YES";
  29.     }
  30.     else{
  31.         cout<<"NO";
  32.     }
  33. }
  34.  
RAW Paste Data