Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*********************
- * My Solution Explain:
- *
- * 1) We can notice that the number of colors is two only.
- *
- * 2) We can simplify the problem to:
- * - Check if the Graph is Biparatite Graph.
- *
- * 3) Biparatite Graph is:
- * - The Graph that has n vertices and we can separate
- * the graph into two disjoint sets V1 ,V2.
- * - Each node exists in ONE of the sets.
- * - No two neighbour nodes are in the same set.
- *
- * 4) The problem is coloring the graph by using two colors
- * and no two neighbour nodes have the same color.
- * So the problem is check the graph is Biparatite or not.
- *
- * 5) We can check the Biparatite Property by using BFS:
- * - Make queue for travers the graph by levels.
- * - Make an array[N] that store the color of the node.
- * - The colors are:
- * # -1: (init) The node isn't colored yet.
- * # 0: First color.
- * # 1: Second color.
- * - Each node color all its neighbour nodes in differint color.
- * - If the neighbour node is colored before => we check if it has
- * the same color or not.
- * - If it has tha same color => we say the graph isn't Biparatite.
- *
- * 6) Time Complexity:
- *
- *
- * Thanks for the great competition AmesCom.
- *
- * Author: Ismaeil Al-refaey.
- ***************************/
- #include <bits/stdc++.h>
- using namespace std;
- typedef vector< int > vi;
- /// --- Graph Adjacency List --- ///
- vector< vi > al;
- int n ,m;
- /// --- Initialize new test case --- ///
- void init()
- {
- al.assign(n ,vi());
- }
- /// --- Function return true if the Graph is Biparatite else return false --- ///
- bool checkBiparatite()
- {
- /// --- Biparatite Checker --- ///
- bool isBiparatite = true;
- /// --- queue for travers the graph --- ///
- queue< int > q;
- q.push(0);
- /// --- vector have the colors of nodes --- ///
- vi color(n ,-1);
- color[0] = 0;
- /// --- BFS --- ///
- while( !q.empty() && isBiparatite )
- {
- int u = q.front(); q.pop();
- /// --- Check all neighbour nodes --- ///
- for(int v : al[u])
- {
- if( color[v] == -1 )
- {
- /// --- Color the neighbour nodes in different color --- ///
- color[v] = 1 - color[u];
- q.push(v);
- }
- /// --- Update Biparatite Checker --- ///
- isBiparatite &= (color[v] != color[u]);
- }
- }
- /// --- return the result --- ///
- return isBiparatite;
- }
- void Solve()
- {
- init();
- /// --- Input --- ///
- scanf("%d" ,&m);
- for(int i = 0 ; i < m ; i++)
- {
- int u; scanf("%d" ,&u);
- int v; scanf("%d" ,&v);
- /// --- Un_Directed Graph: we can go (u <-> v) --- ///
- /// --- We add new neighbour
- al[u].push_back(v);
- al[v].push_back(u);
- }
- /// --- Check if the Graph is Biparatite Graph --- ///
- bool isBi = checkBiparatite();
- /// --- Outpus --- ///
- puts(isBi ? "BICOLORABLE." : "NOT BICOLORABLE.");
- }
- int main()
- {
- /// --- Test Cases --- ///
- while( scanf("%d" ,&n) , n != 0 ) Solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement