Guest User

Beautiful Graph (Codeforces)- Sudhanshu Jaisani

a guest
May 29th, 2020
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.09 KB | None | 0 0
  1.  
  2. //https://codeforces.com/problemset/problem/1093/D#
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.Scanner;
  6.  
  7. class Graph {
  8.     int noOfNodes;
  9.     HashMap<Integer, ArrayList<Integer>> adjList = new HashMap<>();
  10.     boolean isVisited[];
  11.     int color[];
  12.     public static final int MOD = 998244353;
  13.  
  14.     public Graph(int n) {
  15.         this.noOfNodes = n;
  16.         isVisited = new boolean[n];
  17.         color = new int[n];
  18.         for (int i = 0; i < n; i++) {
  19.             adjList.put(i, new ArrayList<Integer>());
  20.             color[i] = -1;
  21.         }
  22.     }
  23.  
  24.     public void addEdge(int x, int y) {
  25.         adjList.get(x).add(y);
  26.         adjList.get(y).add(x);
  27.     }
  28.  
  29.     // tries to color the graph using dfs & returns if it could color it.
  30.     private boolean colorGraph(int src, int colorOfSrc, long colorCount[],int parentOf[]) {
  31.         // System.out.print(src+"("+colorOfSrc+"),");
  32.         isVisited[src] = true;
  33.         boolean oddCycleDetected=false;
  34.         colorCount[colorOfSrc]++;
  35.         color[src]=colorOfSrc;
  36.         ArrayList<Integer> neighbours = adjList.get(src);
  37.         for (Integer neighbour : neighbours) {
  38.             if (!isVisited[neighbour]) {
  39.                 parentOf[neighbour]=src;
  40.                 boolean couldColor = colorGraph(neighbour, 1 - colorOfSrc, colorCount,parentOf);
  41.                 if (!couldColor) {
  42.                     oddCycleDetected=true;
  43.                 }
  44.             } else {
  45.                 if (neighbour!=parentOf[src] && color[neighbour] == colorOfSrc) {
  46.                     oddCycleDetected=true;
  47.                 }
  48.             }
  49.         }
  50.        
  51.         return !oddCycleDetected;
  52.     }
  53.  
  54.     public static void main(String[] args) {
  55.         Scanner sc = new Scanner(System.in);
  56.         int t = sc.nextInt();
  57.         while (t-- > 0) {
  58.             int v = sc.nextInt();
  59.             int e = sc.nextInt();
  60.             Graph g = new Graph(v + 1);
  61.             for (int i = 0; i < e; i++) {
  62.                 int first = sc.nextInt();
  63.                 int sec = sc.nextInt();
  64.                 g.addEdge(first, sec);
  65.             }
  66.             g.solve();
  67.         }
  68.  
  69.     }
  70.  
  71.     private void solve() {
  72.         long finalAns = 1;
  73.         int parentOf[]=new int[noOfNodes];
  74.         for (int i = 1; i < noOfNodes; i++) {
  75.             if (!isVisited[i]) {
  76.                 long colorCount[] = new long[2];
  77.                 parentOf[i]=-1;
  78.                 // System.out.println("Starting dfs from:" + i);
  79.                 if (colorGraph(i, 0, colorCount,parentOf)) {
  80.                     // if colored succesfully
  81.                     long ansForThisComponent = ((1l << colorCount[0]) % MOD) + ((1l << colorCount[1]) % MOD);
  82.                     // System.out.println("ansForThisComponent:"+ansForThisComponent);
  83.                     finalAns = (finalAns*ansForThisComponent) % MOD;
  84.                 } else {
  85.                     // if odd cycle was detected somewhere
  86.                     System.out.println(0);
  87.                     return;
  88.                 }
  89.             }
  90.         }
  91.         System.out.println(finalAns);
  92.     }
  93.  
  94. }
Add Comment
Please, Sign In to add comment