999ms

C. Ктулху

Apr 8th, 2020
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using i32 = int;
  4. using ui32 = unsigned int;
  5. using i64 = long long;
  6. using ui64 = unsigned long long;
  7.  
  8.  
  9. const ui32 MSIZE = 100;
  10.  
  11. std::vector<ui32> g[MSIZE];
  12.  
  13. ui32 color[MSIZE];
  14. bool cycle[MSIZE];
  15. bool used[MSIZE];
  16.  
  17. void UsedDfs(ui32 from) {
  18.   used[from] = true;
  19.   for (ui32 v : g[from]) {
  20.     if (!used[v]) {
  21.       UsedDfs(v);
  22.     }
  23.   }
  24. }
  25.  
  26. i32 Dfs(ui32 from, ui32 parent) {
  27.   color[from] = 1;
  28.   for (ui32 v : g[from]) {
  29.     if (v == parent) continue;
  30.     if (color[v] == 1) {
  31.       cycle[from] = true;
  32.       return v;
  33.     }
  34.     if (color[v] == 0) {
  35.       i32 result = Dfs(v, from);
  36.       if (result >= 0) {
  37.         cycle[from] = true;
  38.         if (result == from) {
  39.           return -2;
  40.         } else {
  41.           return result;
  42.         }
  43.       } else if (result == -2) {
  44.         return -2;
  45.       }
  46.     }
  47.   }
  48.   color[from] = 2;
  49.   return -1;
  50. }
  51.  
  52. i32 main() {
  53.   ui32 n, m;
  54.   scanf("%d%d", &n, &m);
  55.   for (ui32 i = 0; i < m; i++) {
  56.     int f, t;
  57.     scanf("%d%d", &f, &t);
  58.     f--, t--;
  59.     g[f].push_back(t);
  60.     g[t].push_back(f);
  61.   }
  62.  
  63.   // check that graph is connected
  64.   UsedDfs(0);
  65.   bool isGod = true;
  66.   for (ui32 i = 0; i < n; i++) {
  67.     if (!used[i]) {
  68.       isGod = false;
  69.     }
  70.   }
  71.  
  72.   // check that cycle exists
  73.   if (Dfs(0, 0) != -2) {
  74.     isGod = false;
  75.   }
  76.  
  77.   // check that length of cycle >= 3
  78.   ui32 trees = 0;
  79.   for (ui32 i = 0; i < n; i++) {
  80.     if (cycle[i]) {
  81.       trees++;  
  82.     }
  83.   }
  84.   isGod &= trees >= 3;
  85.  
  86.   // check that it is only one cycle (cycle + trees = n edges => m should be equals to n)
  87.   isGod &= m == n;
  88.  
  89.   puts(isGod ? "FHTAGN!" : "NO");
  90. }
Add Comment
Please, Sign In to add comment