CSES - Practice Contest 2024 - Results
Submission details
Task:Graph painting
Sender:hefapa_aachen
Submission time:2024-09-28 16:09:15 +0300
Language:C++ (C++17)
Status:READY
Result:
Test results
testverdicttime
#10.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.10 sdetails

Code

// ~/.vim/cpp_template.cpp
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

#define REP(i,a,b) for (int i = a; i < b; i++)

// Type Aliases for 1D and 2D vectors with initialization
#define vi(n, val) vector<int>(n, val) // 1D vector of ints with size n, initialized to val
#define vll(n, val) vector<long long>(n, val) // 1D vector of long longs with size n, initialized to val
#define ll long long
#define vvi(n, m, val) vector<vector<int>>(n, vector<int>(m, val)) // 2D vector of ints (n x m), initialized to val
#define vvll(n, m, val) vector<vector<long long>>(n, vector<long long>(m, val)) // 2D vector of long longs (n x m), initialized to val


using namespace std;

template <typename T>
void pV(const std::vector<T>& vec, const std::string& label = "Vector") {
    std::cout << label << ": [ ";
    for (const auto& elem : vec) {
        std::cout << elem << " ";
    }
    std::cout << "]" << std::endl;
}


using namespace std;


void dfs(int s, vector<bool> *visited, vector<int> (*adj)[]) {
    if ((*visited)[s]) return;
    (*visited)[s] = true;
    // process node s


    for (auto u: (*adj)[s]) {
        dfs(u, visited, adj);
    }
}
/*
vector<int> adj[N];                                                          
vector<bool> visited(N, false);
int u, v;
for(int i = 0; i < M;i++){
    cin >> u >> v;
    u--;
    v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
}
*/

void paintGraph(int n, const vector<pair<int, int>>& edges, vector<char>& colors) {
    vector<vector<int>> adj(n + 1);
    for (const auto& edge : edges) {
        adj[edge.first].push_back(edge.second);
        adj[edge.second].push_back(edge.first);
    }

    for (int i = 1; i <= n; ++i) {
        if (colors[i] == ' ') {
            queue<int> q;
            q.push(i);
            colors[i] = 'R';
            
            while (!q.empty()) {
                int node = q.front();
                q.pop();
                
                for (int neighbor : adj[node]) {
                    if (colors[neighbor] == ' ') {
                        colors[neighbor] = (colors[node] == 'R') ? 'B' : 'R';
                        q.push(neighbor);
                    }
                }
            }
        }
    }
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    // Your code starts here
    int t;
    cin >> t;
    while (t--) {
        int n, m;
        cin >> n >> m;
        vector<pair<int, int>> edges(m);
        for (int i = 0; i < m; ++i) {
            int a, b;
            cin >> a >> b;
            edges[i] = {a, b};
        }

        vector<char> colors(n + 1, ' '); 
        paintGraph(n, edges, colors);

        for (int i = 1; i <= n; ++i) {
            cout << colors[i] << " ";
        }
        cout << endl;
    }
    return 0;
}

    

Test details

Test 1

Verdict:

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
R R R R B R R 
R B B B B B B B 
R B B B B B R R R R 
R B B B B B 
R B B B R R B R R 
...
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
R R B B R R R R R B R B R R R ...
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
R R R B B B R B R R R B R B R ...
Truncated