Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- enum vertex_colour // the colour
- {
- UNDEFINED, // non-visited
- RED,
- BLACK
- };
- struct vertex // a vertex point
- {
- vertex_colour colour;
- vector <int> edges_connected;
- vertex()
- {
- colour = UNDEFINED;
- return;
- }
- };
- vertex_colour opposite(vertex_colour c)
- {
- switch (c)
- {
- case RED:
- return BLACK;
- case BLACK:
- return RED;
- case UNDEFINED:
- return UNDEFINED;
- default:
- return UNDEFINED;
- }
- }
- int main()
- {
- int vertices_count, edges; // size of vertices array
- cin >> vertices_count >> edges;
- vector <vertex> vertices(vertices_count); // vertices
- for (int i = 0; i < edges; i++) // input edges
- {
- int a, b;
- cin >> a >> b;
- vertices[a].edges_connected.push_back(b);
- vertices[b].edges_connected.push_back(a);
- }
- queue <int> bfs_queue; // BFS queue
- bfs_queue.push(0); // add vertex 0 to BFS queue
- vertices[0].colour = RED;
- while (!bfs_queue.empty()) // BFS
- {
- int vertex_index = bfs_queue.front(); // get check index for vertex
- bfs_queue.pop();
- for (int i = 0; i < vertices[vertex_index].edges_connected.size(); i++) // check all edges connecting to vertex
- {
- if (vertices[vertices[vertex_index].edges_connected[i]].colour == vertices[vertex_index].colour)
- {
- cout << "NO"; // two neighbour vertices have same colour
- return 0;
- }
- if (vertices[vertices[vertex_index].edges_connected[i]].colour == UNDEFINED)
- {
- vertices[vertices[vertex_index].edges_connected[i]].colour = opposite(vertices[vertex_index].colour);
- bfs_queue.push(vertices[vertex_index].edges_connected[i]); // colour with opposite colour
- }
- }
- }
- cout << "YES"; // all are coloured
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement