Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- using Vertex = size_t;
- class Graph {
- public:
- vector<vector<Vertex>> graph;
- vector<int> tin;
- vector<int> tout;
- int time;
- explicit Graph(size_t n) {
- graph.resize(n);
- tin.resize(n);
- tout.resize(n);
- time = 0;
- }
- size_t size() const {
- return graph.size();
- }
- void AddEdge(Vertex vert1, Vertex vert2) {
- graph[vert1].push_back(vert2);
- }
- vector<Vertex> GetNeighbours(Vertex vert) {
- return graph[vert];
- }
- void ConnDFS(Vertex vertex, vector<bool> &visited, vector<Vertex> &topologysort) {
- visited[vertex] = true;
- for (Vertex neig: graph[vertex]) {
- if (!visited[neig]) {
- ConnDFS(neig, visited, topologysort);
- }
- }
- topologysort.push_back(vertex);
- }
- void ColourDFS(Vertex vertex, vector<int> &visited, int colour) {
- visited[vertex] = colour;
- for (Vertex neig: graph[vertex]) {
- if (visited[neig] == 0) {
- ColourDFS(neig, visited, colour);
- }
- }
- }
- };
- int main() {
- int n, m;
- cin >> n >> m;
- string s;
- cin >> s;
- Graph graph(2 * n);
- Graph transgraph(2 * n);
- vector<pair<char, char>> colours(n);
- for (int i = 0; i < n; ++i) {
- if (s[i] == 'R') {
- colours[i].first = 'G';
- colours[i].second = 'B';
- } else if (s[i] == 'G') {
- colours[i].first = 'R';
- colours[i].second = 'B';
- } else {
- colours[i].first = 'R';
- colours[i].second = 'G';
- }
- }
- for (int i = 0; i < m; ++i) {
- int v1, v2;
- cin >> v1 >> v2, --v1, --v2;
- if (colours[v1].first == colours[v2].first) {
- graph.AddEdge(v1 + n, v2);
- graph.AddEdge(v2 + n, v1);
- transgraph.AddEdge(v2, v1 + n);
- transgraph.AddEdge(v1, v2 + n);
- } if (colours[v1].first == colours[v2].second) {
- graph.AddEdge(v2, v1);
- graph.AddEdge(v1 + n, v2 + n);
- transgraph.AddEdge(v1, v2);
- transgraph.AddEdge(v2 + n, v1 + n);
- } if (colours[v1].second == colours[v2].first) {
- graph.AddEdge(v1, v2);
- graph.AddEdge(v2 + n, v1 + n);
- transgraph.AddEdge(v2, v1);
- transgraph.AddEdge(v1 + n, v2 + n);
- } if (colours[v1].second == colours[v2].second){
- graph.AddEdge(v2, v1 + n);
- graph.AddEdge(v1, v2 + n);
- transgraph.AddEdge(v1 + n, v2);
- transgraph.AddEdge(v2 + n, v1);
- }
- }
- vector<Vertex> topologysort(2 * n);
- vector<bool> visited(graph.size(), false);
- for (size_t i = 0; i < graph.size(); ++i) {
- if (!visited[i]) {
- graph.ConnDFS(i, visited, topologysort);
- }
- }
- std::reverse(topologysort.begin(), topologysort.end());
- int colour = 0;
- vector<int> visited2(graph.size(), false);
- for (size_t i = 0; i < topologysort.size(); ++i) {
- if (!visited2[topologysort[i]]) {
- ++colour;
- transgraph.ColourDFS(topologysort[i], visited2, colour);
- }
- }
- for (int i = 0; i < n; ++i) {
- if (visited2[i] == visited2[i + n]) {
- cout << "Impossible";
- return 0;
- }
- }
- for (int i = 0; i < n; ++i) {
- if (visited2[i] < visited2[i+n]){
- cout << colours[i].first;
- } else {
- cout << colours[i].second;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement