Advertisement
Guest User

Untitled

a guest
May 25th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. const int maxn = (int)1e5 + 10;
  2. vector <int> ed[maxn];
  3.  
  4. int hospital(int city_nodes, vector<int> city_from, vector<int> city_to) {
  5.     int n = city_nodes;
  6.     for (int i = 0; i < (int)city_from.size(); i++) {
  7.         int x = city_from[i];
  8.         int y = city_to[i];
  9.         x--;
  10.         y--;
  11.        
  12.         ed[x].push_back(y);
  13.         ed[y].push_back(x);
  14.     }
  15.    
  16.     int ans = n;
  17.    
  18.     for (int ms = 0; ms < (1 << n); ms++) {
  19.         int cur = 0;
  20.         for (int i = 0; i < n; i++) {
  21.             if ((ms >> i) & 1) {
  22.                 cur++;
  23.                 continue;
  24.             }
  25.            
  26.             bool ok = false;
  27.            
  28.             for (int j = 0; j < (int)ed[i].size(); j++) {
  29.                 int to = ed[i][j];
  30.                
  31.                 if ((ms >> to) & 1) {
  32.                     ok = true;
  33.                     break;
  34.                 }
  35.             }
  36.            
  37.             if (!ok) {
  38.                 cur = n;
  39.                 break;
  40.             }
  41.         }
  42.        
  43.         ans = min(ans, cur);
  44.     }
  45.    
  46.     return ans;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement