warriorscats

War

Jun 1st, 2020
821
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.65 KB | None | 0 0
  1. #include <fstream>
  2. #include <vector>
  3. #include <set>
  4. #include <queue>
  5. #include <utility>
  6.  
  7.  
  8. std::ifstream fin ("warcraft.in");
  9. std::ofstream fout ("warcraft.out");
  10.  
  11. int n, m, x, cnt_conex, cnt_Frost_Wolf, cnt_total, Frost_Wolf;
  12. std::vector <std::vector <int>> Graph;
  13. std::vector <std::set <int>> Skills;
  14. std::vector <bool> Warchief, Seen;
  15. std::vector <int> Tribes;
  16. std::queue <int> Q;
  17. std::set <std::pair <int, std::pair <int, int>>> Store;
  18.  
  19.  
  20. inline void BFS (int src)
  21. {
  22.     std::vector <int> Count_Sk (n + 1, 0);
  23.     int cnt_trb = 0;
  24.     bool OK = false;
  25.  
  26.     Q.push (src); Seen[src] = true;
  27.  
  28.     while (!Q.empty ())
  29.     {
  30.         int v = Q.front (); Q.pop (); ++ cnt_trb;
  31.  
  32.         if (v != src)
  33.             for (auto sk : Skills[v])
  34.                 ++ Count_Sk[sk];
  35.  
  36.         if (v == x)
  37.             OK = true;
  38.  
  39.         for (auto u : Graph[v])
  40.             if (!Seen[u])
  41.                 Seen[u] = true, Q.push (u);
  42.     }
  43.  
  44.     Tribes[src] = cnt_trb;
  45.     if (OK)
  46.         cnt_Frost_Wolf = cnt_trb, Frost_Wolf = src;
  47.  
  48.     ///for (auto sk : Skills[src])
  49.         ///-- Count_Sk [sk];
  50.  
  51.     for (int sk = 1; sk <= m; ++ sk)
  52.         if (Count_Sk[sk] == 1)
  53.             Skills[src].insert (sk);
  54. }
  55.  
  56.  
  57. int main ()
  58. {
  59.     fin >> n >> m >> x;
  60.  
  61.     Graph = std::vector <std::vector <int>> (n + 1);
  62.     Skills = std::vector <std::set <int>> (n + 1);
  63.     Warchief = Seen = std::vector <bool> (n + 1, false);
  64.     Tribes = std::vector <int> (n + 1, 0);
  65.  
  66.     for (int i = 1, u, v, k; i <= n; ++ i)
  67.     {
  68.         fin >> u >> v >> k;
  69.  
  70.         if (v)
  71.             Graph[v].push_back (u);
  72.         else
  73.             Warchief[u] = true;
  74.  
  75.         for (int j = 1, sk; j <= k; ++ j)
  76.         {
  77.             fin >> sk;
  78.             Skills[u].insert (sk);
  79.         }
  80.     }
  81.  
  82.     for (int v = 1; v <= n; ++ v)
  83.         if (Warchief[v])
  84.             ++ cnt_conex, BFS (v);
  85.  
  86.     for (int v = 1; v <= n; ++ v)
  87.         if (v != Frost_Wolf && Warchief[v])
  88.             Store.insert (std::make_pair (Tribes[v], std::make_pair (static_cast <int> (Skills[v].size ()), v)));
  89.  
  90.     for (auto it : Store)
  91.     {
  92.         if ((Tribes[Frost_Wolf] > it.first) ||
  93.             (Tribes[Frost_Wolf] == it.first && static_cast <int> (Skills[Frost_Wolf].size ()) > it.second.first))
  94.         {
  95.             for (auto sk : Skills[it.second.second])
  96.                 Skills[Frost_Wolf].insert (sk);
  97.  
  98.             Tribes[Frost_Wolf] += it.first;
  99.             Graph[Frost_Wolf].push_back (it.second.second);
  100.         }
  101.     }
  102.     cnt_total = Tribes[Frost_Wolf];
  103.  
  104.     fout << cnt_conex << "\n" << cnt_Frost_Wolf << "\n" << cnt_total;
  105.  
  106.     fin.close (), fout.close ();
  107.     return 0;
  108. }
Add Comment
Please, Sign In to add comment