Advertisement
Saleh127

Spoj is it a tree

Oct 20th, 2020
65
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define test int t; cin>>t; for(int cs=1;cs<=t;cs++)
  5. vector<ll>graph[200005];
  6. bool visit[200005];
  7.  
  8. void dfs(ll x)
  9. {
  10. visit[x]=true;
  11. for(ll i=0;i<graph[x].size();i++)
  12. {
  13. if(visit[graph[x][i]]==0)
  14. {
  15. dfs(graph[x][i]);
  16. }
  17. }
  18. }
  19.  
  20. int main()
  21. {
  22. ios_base::sync_with_stdio(0);
  23. cin.tie(0);cout.tie(0);
  24.  
  25. ll n,m,i,j,k,l;
  26. cin>>n>>m;
  27.  
  28. for(i=0;i<m;i++)
  29. {
  30. ll a,c;
  31. cin>>a>>c;
  32. graph[a].push_back(c);
  33. graph[c].push_back(a);
  34. }
  35. dfs(1);
  36.  
  37. for(i=1;i<=n;i++)
  38. {
  39. if(visit[i]==0)
  40. {
  41. cout<<"NO"<<endl;
  42. return 0;
  43. }
  44. }
  45. if(n-1==m) cout<<"YES"<<endl;
  46. else cout<<"NO"<<endl;
  47.  
  48. return 0;
  49. }
  50.  
Advertisement
RAW Paste Data Copied
Advertisement