Advertisement
shamiul93

1009 - Back to Underworld LightOJ

Feb 8th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.65 KB | None | 0 0
  1. #include<iostream>
  2. #include<algorithm>
  3. #include<queue>
  4. #include<vector>
  5. #include<cstdio>
  6. #include<cstring>
  7.  
  8. using namespace std;
  9.  
  10. #define ll long long int
  11.  
  12. ll BFS()
  13. {
  14.     ll u, v, n, t = 0;
  15.     cin >> n;
  16.     queue<ll> q;
  17.     vector<ll> vc[100007];
  18.     int race[100007] = {};
  19.     ll vam=0, lyk=0;
  20.  
  21.     while (t<n)
  22.     {
  23.         cin >> u >> v;
  24.         vc[u].push_back(v);
  25.         vc[v].push_back(u);
  26.         t++;
  27.     }
  28.     ll ans = 0 ;
  29. /*The next line is used for this reason - The graph can be disconnected. We have to check it and count it. We can use map for it. But as it's an easy one , it didn't create any problem with time complexity. */
  30.     for(ll idx = 1 ; idx< 100007 ; idx++)
  31.     {
  32.         if(vc[idx].size()!=0 && race[idx] == 0)
  33.         {
  34.  
  35.             vam = 0 ;
  36.             lyk = 0 ;
  37.  
  38. /**explaination: Look, here we don't care about if the fighter is a Vampire of Lykan, we only care the maximum
  39. number of fighters of a certain kind. We have to check their value in every step , in every graph traversing (as they can be disconnected). So, the values of vam , lyk has to be set to '0' in the beginning of every step. */
  40.  
  41.             race[idx] = 1;
  42.             vam++ ;
  43.             q.push(idx);
  44.             while (!q.empty())
  45.             {
  46.                 ll node;
  47.                 node = q.front();
  48.                 q.pop();
  49.  
  50.                 for (ll i = 0; i < vc[node].size(); i++)
  51.                 {
  52.                     if (race[vc[node][i]] == 0)
  53.                     {
  54.                         if (race[node] == 1)
  55.                         {
  56.                             race[vc[node][i]] = 2;
  57.                             lyk++;
  58.                         }
  59.                         else if (race[node] == 2)
  60.                         {
  61.                             race[vc[node][i]] = 1;
  62.                             vam++;
  63.                         }
  64.                         q.push(vc[node][i]);
  65.                     }
  66.                 }
  67.             }
  68.             ans += max(vam, lyk) ;
  69. /* See , here is the main trick. You have to maximize the number of one kind. Suppose you have taken 1 as vampire and 2 as lykan.
  70. But it doesn't really matter what have you taken as Lyk or Vam. You just have to get the maximized result at any graph.Because if we take 2 as vam and 1 as lyk then if the result maximizes , then it's our answer. so we have to check it in every step. */
  71.         }
  72.     }
  73.  
  74.     return ans;
  75. }
  76.  
  77. int main()
  78. {
  79. //    freopen("out.txt","w", stdout);
  80.     int T, t = 0;
  81.     cin >> T;
  82.     ll ans;
  83.  
  84.     while (T--)
  85.     {
  86.         ans = BFS();
  87.         printf("Case %d: %lld\n",t+1,ans );
  88.         t++;
  89.     }
  90.     return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement