Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class B {
- List<Integer>[] graph;
- int[] color;
- void dfs(int u, int c) {
- color[u] = c;
- for (int v : graph[u]) {
- if (color[v] == -1) {
- dfs(v, 1 - c);
- }
- }
- }
- public void solve() {
- int n = in.nextInt(), m = in.nextInt();
- graph = new List[n];
- color = new int[n];
- Arrays.fill(color, -1);
- for (int i = 0; i < n; i++) {
- graph[i] = new ArrayList<>();
- }
- for (int i = 0; i < m; i++) {
- int a = in.nextInt() - 1, b = in.nextInt() - 1;
- graph[a].add(b);
- graph[b].add(a);
- }
- for (int i = 0; i < n; i++) {
- if (color[i] == -1) {
- dfs(i, 0);
- }
- }
- int countLeft = 0, countRight = 0;
- for (int i = 0; i < n; i++) {
- if (color[i] == 0) {
- countLeft++;
- } else {
- countRight++;
- }
- }
- if (countLeft > countRight) {
- int tmp = countLeft;
- countLeft = countRight;
- countRight = tmp;
- for (int i = 0; i < n; i++) {
- color[i] = 1 - color[i];
- }
- }
- List<Integer> left = new ArrayList<>(), right = new ArrayList<>();
- int[] id = new int[n];
- for (int i = 0; i < n; i++) {
- if (color[i] == 0) {
- id[i] = left.size();
- left.add(i);
- } else {
- id[i] = right.size();
- right.add(i);
- }
- }
- int result = 0;
- int[] dp = new int[1 << countLeft];
- int[] newDp = new int[1 << countLeft];
- for (int mask = 0; mask < 1 << countLeft; mask++) {
- boolean[] coveredRight = new boolean[countRight];
- List<Integer> notIn = new ArrayList<>();
- int[] masks = new int[countRight];
- for (int i = 0; i < countLeft; i++) {
- if ((mask & (1 << i)) != 0) {
- for (int j : graph[left.get(i)]) {
- coveredRight[id[j]] = true;
- }
- } else {
- for (int j : graph[left.get(i)]) {
- masks[id[j]] |= 1 << notIn.size();
- }
- notIn.add(i);
- }
- }
- int size = notIn.size();
- for (int submask = 0; submask < 1 << size; submask++) {
- dp[submask] = 0;
- newDp[submask] = 0;
- }
- dp[0] = 1;
- for (int i = 0; i < countRight; i++) {
- for (int submask = 0; submask < 1 << size; submask++) {
- if (coveredRight[i]) {
- newDp[submask] += dp[submask];
- }
- newDp[submask | masks[i]] += dp[submask];
- }
- for (int submask = 0; submask < 1 << size; submask++) {
- dp[submask] = newDp[submask];
- newDp[submask] = 0;
- }
- }
- result += dp[(1 << size) - 1];
- }
- out.println(result);
- }
- public void run() {
- in = new FastScanner();
- out = new PrintWriter(System.out);
- solve();
- out.close();
- }
- FastScanner in;
- PrintWriter out;
- class FastScanner {
- BufferedReader br;
- StringTokenizer st;
- public FastScanner(String fileName) {
- try {
- br = new BufferedReader(new FileReader(fileName));
- } catch (FileNotFoundException e) {
- }
- }
- public FastScanner() {
- br = new BufferedReader(new InputStreamReader(System.in));
- }
- String nextToken() {
- while (st == null || !st.hasMoreElements()) {
- try {
- st = new StringTokenizer(br.readLine());
- } catch (IOException e) {
- }
- }
- return st.nextToken();
- }
- int nextInt() {
- return Integer.parseInt(nextToken());
- }
- long nextLong() {
- return Long.parseLong(nextToken());
- }
- double nextDouble() {
- return Double.parseDouble(nextToken());
- }
- }
- public static void main(String[] args) {
- new B().run();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment