niyaznigmatullin

Untitled

Feb 15th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.53 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class B {
  5.  
  6.     List<Integer>[] graph;
  7.     int[] color;
  8.  
  9.     void dfs(int u, int c) {
  10.         color[u] = c;
  11.         for (int v : graph[u]) {
  12.             if (color[v] == -1) {
  13.                 dfs(v, 1 - c);
  14.             }
  15.         }
  16.     }
  17.  
  18.     public void solve() {
  19.         int n = in.nextInt(), m = in.nextInt();
  20.         graph = new List[n];
  21.         color = new int[n];
  22.         Arrays.fill(color, -1);
  23.         for (int i = 0; i < n; i++) {
  24.             graph[i] = new ArrayList<>();
  25.         }
  26.         for (int i = 0; i < m; i++) {
  27.             int a = in.nextInt() - 1, b = in.nextInt() - 1;
  28.             graph[a].add(b);
  29.             graph[b].add(a);
  30.         }
  31.  
  32.         for (int i = 0; i < n; i++) {
  33.             if (color[i] == -1) {
  34.                 dfs(i, 0);
  35.             }
  36.         }
  37.  
  38.         int countLeft = 0, countRight = 0;
  39.         for (int i = 0; i < n; i++) {
  40.             if (color[i] == 0) {
  41.                 countLeft++;
  42.             } else {
  43.                 countRight++;
  44.             }
  45.         }
  46.         if (countLeft > countRight) {
  47.             int tmp = countLeft;
  48.             countLeft = countRight;
  49.             countRight = tmp;
  50.             for (int i = 0; i < n; i++) {
  51.                 color[i] = 1 - color[i];
  52.             }
  53.         }
  54.         List<Integer> left = new ArrayList<>(), right = new ArrayList<>();
  55.         int[] id = new int[n];
  56.         for (int i = 0; i < n; i++) {
  57.             if (color[i] == 0) {
  58.                 id[i] = left.size();
  59.                 left.add(i);
  60.             } else {
  61.                 id[i] = right.size();
  62.                 right.add(i);
  63.             }
  64.         }
  65.  
  66.         int result = 0;
  67.         int[] dp = new int[1 << countLeft];
  68.         int[] newDp = new int[1 << countLeft];
  69.         for (int mask = 0; mask < 1 << countLeft; mask++) {
  70.             boolean[] coveredRight = new boolean[countRight];
  71.             List<Integer> notIn = new ArrayList<>();
  72.             int[] masks = new int[countRight];
  73.  
  74.             for (int i = 0; i < countLeft; i++) {
  75.                 if ((mask & (1 << i)) != 0) {
  76.                     for (int j : graph[left.get(i)]) {
  77.                         coveredRight[id[j]] = true;
  78.                     }
  79.                 } else {
  80.                     for (int j : graph[left.get(i)]) {
  81.                         masks[id[j]] |= 1 << notIn.size();
  82.                     }
  83.                     notIn.add(i);
  84.                 }
  85.             }
  86.             int size = notIn.size();
  87.  
  88.             for (int submask = 0; submask < 1 << size; submask++) {
  89.                 dp[submask] = 0;
  90.                 newDp[submask] = 0;
  91.             }
  92.             dp[0] = 1;
  93.  
  94.             for (int i = 0; i < countRight; i++) {
  95.                 for (int submask = 0; submask < 1 << size; submask++) {
  96.                     if (coveredRight[i]) {
  97.                         newDp[submask] += dp[submask];
  98.                     }
  99.                     newDp[submask | masks[i]] += dp[submask];
  100.                 }
  101.  
  102.                 for (int submask = 0; submask < 1 << size; submask++) {
  103.                     dp[submask] = newDp[submask];
  104.                     newDp[submask] = 0;
  105.                 }
  106.             }
  107.             result += dp[(1 << size) - 1];
  108.         }
  109.         out.println(result);
  110.     }
  111.  
  112.     public void run() {
  113.         in = new FastScanner();
  114.         out = new PrintWriter(System.out);
  115.         solve();
  116.         out.close();
  117.     }
  118.  
  119.     FastScanner in;
  120.     PrintWriter out;
  121.  
  122.     class FastScanner {
  123.         BufferedReader br;
  124.         StringTokenizer st;
  125.  
  126.         public FastScanner(String fileName) {
  127.             try {
  128.                 br = new BufferedReader(new FileReader(fileName));
  129.             } catch (FileNotFoundException e) {
  130.             }
  131.         }
  132.  
  133.         public FastScanner() {
  134.             br = new BufferedReader(new InputStreamReader(System.in));
  135.         }
  136.  
  137.         String nextToken() {
  138.             while (st == null || !st.hasMoreElements()) {
  139.                 try {
  140.                     st = new StringTokenizer(br.readLine());
  141.                 } catch (IOException e) {
  142.                 }
  143.             }
  144.             return st.nextToken();
  145.         }
  146.  
  147.         int nextInt() {
  148.             return Integer.parseInt(nextToken());
  149.         }
  150.  
  151.         long nextLong() {
  152.             return Long.parseLong(nextToken());
  153.         }
  154.  
  155.         double nextDouble() {
  156.             return Double.parseDouble(nextToken());
  157.         }
  158.     }
  159.  
  160.     public static void main(String[] args) {
  161.         new B().run();
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment