Advertisement
Zeinab_Hamdy

Untitled

Sep 1st, 2023
677
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.26 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define nl "\n"
  4. #define fi first
  5. #define se second
  6. #define pb push_back
  7. #define ll long long
  8. #define ull unsigned ll
  9. #define RV  return void
  10. #define inf 2000000000
  11. #define sz(x) int(x.size())
  12. #define all(v) v.begin(), v.end()
  13. #define rall(v) v.rbegin(), v.rend()
  14. #define Mini(x) *min_element(all(x))
  15. #define Maxi(x) *max_element(all(x))
  16. #define fixed(n) fixed << setprecision(n)
  17. // #define ceil(w, m) (((w) / (m)) + ((w) % (m) ? 1 : 0))
  18. #define cin(v) for (auto&i:v) cin >> i;
  19. #define cout(v) for (auto&i:v) cout << i << " ";
  20. #define clr(memo, x) memset(memo, x, sizeof memo)
  21. #define updmin(a, b) a = min(a, b)
  22. #define updmax(a, b) a = max(a, b)
  23. #define vi vector < int >
  24. #define vl vector < ll >
  25. #define vc vector < char >
  26. #define vs vector < string >
  27. #define v2i vector < vector < int > >
  28. #define v2l vector < vector < int > >
  29. #define seti set < int >
  30. #define setl set < ll >
  31. #define mapii map < int , int >
  32. #define mapll map < ll , ll >
  33. #define mapli map < ll , int >
  34. #define mapci map < char , int >
  35. #define mapsi map < string , int >
  36. #define pll pair < ll , ll >
  37. #define pii pair < int , int >
  38. #define range(l,r,x) for(int i=l ; i < r ; i+=x)
  39. #define FastCode ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  40. vector < string > ternary= {"NO\n" , "YES\n"};
  41.  
  42. void  Zainab(){
  43.             #ifndef ONLINE_JUDGE
  44.               freopen("input.txt", "r", stdin);
  45.               freopen("output.txt", "w", stdout);
  46.             #endif
  47. }
  48.  
  49.  
  50. /*================================  Prblem solution  ================================ */
  51.  
  52. vector < vector < int > > adj  , adj2;
  53. vector < bool > vis;
  54.  
  55. bool dfs(int node , int par ){
  56.     vis[node]=1;
  57.    
  58.     bool ans =0;
  59.     //  connect with 2 nodes only or it is leaf
  60.    
  61.     if(sz(adj[node]) == 2 ) ans =1;
  62.     else if (node ==1 ) ans =1;
  63.     else if(sz(adj[node]) == 1 and node!=1 and adj[node][0]==par ) ans =1;
  64.     else {
  65.         // ans=0;
  66.         // cout << node << nl;
  67.         return 0;
  68.     }
  69.    
  70.     for(auto& child : adj[node]){
  71.          if(!vis[child]) {
  72.                ans &= dfs(child , node);
  73.          }
  74.     }
  75.  
  76.     return ans ;
  77. }
  78.  
  79.  
  80. bool dfs2(int node , int target ){
  81.     vis[node]=1;
  82.    
  83.     int cnt=0;
  84.     if(target == node){
  85.         cnt++;
  86.         return cnt == 1 ;
  87.     }
  88.     for(auto& child : adj2[node]){
  89.         cnt += dfs2(child , target);
  90.     }
  91.    
  92.     return cnt == 1 ;
  93. }
  94.  
  95. void myCode(){
  96.  
  97. int n , m ;
  98. cin >> n  >> m ;
  99. adj.assign (n+5 , vector < int > ());
  100. vis.assign(n +5, 0);
  101. adj2.assign(n+5 , vector < int > ());
  102. for(int i =1 ; i < n ; i++){
  103.   int u,v;
  104.   cin >> u  >> v;
  105.   adj[u].pb(v);
  106.   adj[v].pb(u);
  107.  
  108.     adj2[u].pb(v);
  109. }
  110.  
  111. if( m != n-1 ) RV(cout << "NO");
  112.  
  113.  
  114.  
  115.  
  116. bool ans = dfs(1,1);
  117. if(!ans) RV(cout << "NO");
  118.  
  119.  
  120.  
  121. //  what about cyclic ?
  122.  
  123. // check if there exist more than unique path from root to any node
  124.  
  125. for(int i =2 ; i <= n ; i++){
  126.     vis.assign(n +5, 0);
  127.     ans &= dfs2( 1 , i );
  128. }
  129.  
  130. cout << ( ans ? "YES" : "NO");
  131.  
  132.    
  133.  
  134.    
  135. }
  136.  
  137.  
  138. int main(){
  139.                                    FastCode ;
  140.                                     Zainab() ;
  141.  
  142.     int testCase=1;
  143.           // cin >> testCase ;
  144.       for(int i=1 ; i<= testCase ; i++)
  145.         myCode();
  146.  
  147.     return 0;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement