Advertisement
_rashed

UVA 615

Jun 29th, 2022
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 KB | None | 0 0
  1. #define ll long long
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. const int OO = 1e9;
  6. const double EPS = 1e-9;
  7.  
  8. map<int,bool> vis;
  9. set<int> nodes;
  10. map<int,vector<int>> graph;
  11. map<int,int> indeg;
  12.  
  13. bool dfs(int x) {
  14.     //cout << "here at x = " << x << "\n";
  15.     if(vis[x]) {
  16.         return false;
  17.     }
  18.     vis[x] = 1;
  19.     bool ret = true;
  20.     for(int e : graph[x]) {
  21.         ret = dfs(e) && ret;
  22.     }
  23.     return ret;
  24. }
  25.  
  26. int main()
  27. {
  28.     ios_base::sync_with_stdio(false);
  29.     cin.tie(NULL);
  30.     cout.tie(NULL);
  31.     int a,b;
  32.     int ti = 0;
  33.     while(true) {
  34.         cin >> a >> b;
  35.         if(a < 0 && b < 0) {
  36.             break;
  37.         }
  38.         ti++;
  39.         vis.clear();
  40.         nodes.clear();
  41.         graph.clear();
  42.         indeg.clear();
  43.         while(true) {
  44.             if(a == 0 && b == 0) {
  45.                 break;
  46.             }
  47.             graph[a].push_back(b);
  48.             indeg[b]++;
  49.             nodes.insert(a);
  50.             nodes.insert(b);
  51.             cin >> a >> b;
  52.         }
  53.         int root = -1;
  54.         bool tree = true;
  55.         if(nodes.empty()) {
  56.             goto done;
  57.         }
  58.  
  59.         for(int x : nodes) {
  60.             if(indeg[x] == 0) {
  61.                 if(root != -1) {
  62.                     tree = false;
  63.                     goto done;
  64.                 }
  65.                 root = x;
  66.             }
  67.         }
  68.         if(root == -1) {
  69.             tree = false;
  70.             goto done;
  71.         }
  72.  
  73.         tree = tree && dfs(root);
  74.         for(int x : nodes) {
  75.             if(!vis[x]) {
  76.                 tree = false;
  77.                 break;
  78.             }
  79.         }
  80.         done:
  81.             cout << "Case " << ti << " is " << (tree ? "a tree.\n":"not a tree.\n");
  82.     }
  83.     return 0;
  84. }
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement