Guest User

Untitled

a guest
Jan 18th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.19 KB | None | 0 0
  1. int protect(int i) {
  2.         Queue<Integer> q = new LinkedList<Integer>();
  3.         int res = 0;
  4.         boolean vis[] = new boolean[love.length];
  5.         for (int j = 0; j < love[i].length(); j++)
  6.             if (love[i].charAt(j) == 'Y') {
  7.                 q.add(j);
  8.                 res |= (1 << j);
  9.                 vis[j] = true;
  10.             }
  11.         while (!q.isEmpty()) {
  12.             int cur = q.poll();
  13.             for (int j = 0; j < love[cur].length(); j++)
  14.                 if (!vis[j] && love[cur].charAt(j) == 'Y') {
  15.                     q.add(j);
  16.                     res |= (1 << j);
  17.                     vis[j] = true;
  18.                 }
  19.         }
  20.         return res;
  21.     }
  22.  
  23.     String love[];
  24.  
  25.     public int maxMagicalGirls(String[] love) {
  26.         this.love = love;
  27.  
  28.         int[] protectMask = new int[love.length];
  29.         for (int i = 0; i < protectMask.length; i++)
  30.             protectMask[i] = protect(i);
  31.         int max = 1 << love.length;
  32.         int res = 0;
  33.         for (int i = 0; i < max; i++) {
  34.             int protectedG = 0, tmpi = i;
  35.             int j = 0;
  36.             while (tmpi > 0) {
  37.                 if ((tmpi & 1) == 1)
  38.                     protectedG |= protectMask[j];
  39.                 tmpi >>= 1;
  40.                 j++;
  41.             }
  42.             tmpi = i;
  43.             int curRes = 0;
  44.             while (tmpi > 0) {
  45.                 if ((tmpi & 1) == 1 && (protectedG & 1) == 0)
  46.                     curRes++;
  47.                 tmpi >>= 1;
  48.                 protectedG >>= 1;
  49.                 j++;
  50.             }
  51.             res  = Math.max(res, curRes);
  52.         }
  53.  
  54.         return res;
  55.     }
Add Comment
Please, Sign In to add comment