CSES - Practice Contest 2024 - Results
Submission details
Task:Graph painting
Sender:\(._.)/
Submission time:2024-09-28 16:25:07 +0300
Language:C++ (C++11)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.20 sdetails

Code

#include <algorithm>
#include <climits>
#include <cstdio>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

// Depth first search
void dfs(int s, bool visited[], vector<int> adj[], vector<int> &colors, int color) {
    if (visited[s]) {
        return;
    };
    visited[s] = true;
    colors[s] = color;
    for (auto u: adj[s]) {
        dfs(u, visited, adj, colors, 1 - color);
    }
}

void iterate_connected_components(int nof_vertices, int nof_edges,
				  int start_vertice,
				  vector<pair<int, int>> edges,
                  vector<int> &colors
                  )
{
	// Create needed structures
	vector<int> adj[nof_vertices + 1];
	bool visited[nof_vertices + 1];
	for (int i = 0; i < nof_vertices + 1; i++)
		visited[i] = false;

	// Add edges to adjacent structure
	for (int j = 0; j < nof_edges; j++) {
		adj[edges[j].first].push_back(edges[j].second);
		adj[edges[j].second].push_back(edges[j].first);
	}

	int k = start_vertice;
	while (!visited[k]) {
		// Call dfs for first connected component
		dfs(k, visited, adj, colors, 0);

        // for (int j = 1; j < nof_vertices + 1; j++) {
        //     cout << colors[j] << "\n";
        // }

		// Find first unconnected vertice and update start vertice
		for (int i = k; i < nof_vertices + 1; i++) {
			if (!visited[i]) {
				k = i;
				// Do some stuff for each connected component
				break;
			}
		}
	}
}

int main(int argc, char *argv[])
{
	// Read the input parameters
	int t;
	cin >> t;

    for (int i = 0; i < t; i++) {
        int n, m;
        cin >> n >> m;
        vector<int> colors(n + 1, -1);
        vector<pair<int, int>> edges;
        for (int j = 0; j < m; j++) {
            int v1, v2;
            cin >> v1 >> v2;
            edges.push_back({v1, v2});
        }
        iterate_connected_components(n, m, 1, edges, colors);
        for (int j = 1; j < n + 1; j++) {
            // cout << colors[j] << "\n";
            char color = colors[j] == 0 ? 'B' : 'R';
            if (colors[j] == -1) color = 'R';
            cout << color << " ";
        }
        // cout << "\n\n";
        cout << "\n";
    }
	return 0;
}

Test details

Test 1

Verdict: ACCEPTED

input
100
7 1
2 5
8 28
2 7
...

correct output
B R B B B B R 
R B B R B R B B 
R R B B B B R R R B 
B B R B R B 
B B B R B R R B R 
...

user output
B B B B R B B 
B B R R B B R R 
B R R B R R B B B R 
B R B R R B 
B R R B B R R R B 
...
Truncated

Test 2

Verdict: ACCEPTED

input
10
38 36
18 28
20 37
22 38
...

correct output
R R B R B R R R R R B B R B R ...

user output
B B R B B B B B B R B R B B B ...
Truncated

Test 3

Verdict: ACCEPTED

input
1
100000 200000
89300 98492
33853 56822
92967 99427
...

correct output
R R R R B R R R B B B R B B B ...

user output
B B R B B B B B R R R R R B B ...
Truncated