Advertisement
ibragimova_mariam

Важная новость (битовые маски)

Aug 4th, 2020
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. public static void main(String[] args)
  2. throws FileNotFoundException {
  3. Scanner in = new Scanner(new File("new2.in"));//new File("f.in")
  4.  
  5. int n = in.nextInt();
  6. int k = in.nextInt();
  7.  
  8. int maxv = (1 << n) - 1;
  9.  
  10. int[] m = new int[n];
  11. for(int i = 0; i < n; i++)
  12. {
  13. m[i] = 1 << i;
  14. }
  15. int a, b;
  16. for (int i = 0; i < k; i++)
  17. {
  18. a = in.nextInt() - 1;
  19. b = in.nextInt() - 1;
  20. m[a] ^= 1 << b;
  21. m[b] ^= 1 << a;
  22. }
  23.  
  24. int ans = n;
  25. for (int mask = (1 << n) - 1; mask >= 0; mask--)
  26. {
  27. int maskBitsCount = bitCount(mask);
  28. if (maskBitsCount >= ans)
  29. continue;
  30.  
  31. int maskCopy = mask;
  32. int friendWhoKnow = 0;
  33. for (int i = 0; maskCopy != 0; maskCopy >>= 1, i++)
  34. {
  35. if ((maskCopy & 1) == 1)
  36. {
  37. friendWhoKnow |= m[i];
  38. }
  39. }
  40. if (friendWhoKnow == maxv)
  41. {
  42. ans = maskBitsCount;
  43. }
  44. }
  45. System.out.println(ans);
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement