Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int protect(int i) {
- Queue<Integer> q = new LinkedList<Integer>();
- int res = 0;
- boolean vis[] = new boolean[love.length];
- for (int j = 0; j < love[i].length(); j++)
- if (love[i].charAt(j) == 'Y') {
- q.add(j);
- res |= (1 << j);
- vis[j] = true;
- }
- while (!q.isEmpty()) {
- int cur = q.poll();
- for (int j = 0; j < love[cur].length(); j++)
- if (!vis[j] && love[cur].charAt(j) == 'Y') {
- q.add(j);
- res |= (1 << j);
- vis[j] = true;
- }
- }
- return res;
- }
- String love[];
- public int maxMagicalGirls(String[] love) {
- this.love = love;
- int[] protectMask = new int[love.length];
- for (int i = 0; i < protectMask.length; i++)
- protectMask[i] = protect(i);
- int max = 1 << love.length;
- int res = 0;
- for (int i = 0; i < max; i++) {
- int protectedG = 0, tmpi = i;
- int j = 0;
- while (tmpi > 0) {
- if ((tmpi & 1) == 1)
- protectedG |= protectMask[j];
- tmpi >>= 1;
- j++;
- }
- tmpi = i;
- int curRes = 0;
- while (tmpi > 0) {
- if ((tmpi & 1) == 1 && (protectedG & 1) == 0)
- curRes++;
- tmpi >>= 1;
- protectedG >>= 1;
- j++;
- }
- res = Math.max(res, curRes);
- }
- return res;
- }
Add Comment
Please, Sign In to add comment