Submission details
Task:3-Coloring
Sender:ariadna.roga
Submission time:2025-11-26 17:06:42 +0200
Language:C++ (C++17)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.00 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.00 sdetails
#7ACCEPTED0.00 sdetails
#8ACCEPTED0.00 sdetails
#9ACCEPTED0.00 sdetails
#100.00 sdetails
#110.00 sdetails
#120.01 sdetails
#130.06 sdetails

Code

#include <bits/stdc++.h>

using namespace std;

// 3-coloring problem

/*
You are given a directed graph with n nodes and n edges. Node i has exactly one outgoing edge that leads to node e_i. Your task is to find a valid 3-coloring of the graph: the endpoints of each edge should have different colors and you can only use at most 3 colors. It is guaranteed that the graph does not contain any self-loops and it can be proven that a valid coloring always exists when this constraint is met.
Input
The first line contains one integer n: the number of nodes.
The second line contains n integers e_1,\ e_2,\ \dots,\ e_n: the edges.
Output
Print n integers in the range [1, 3] on a single line. The output should be a valid coloring for the nodes.
*/

int main()
{
    // ios_base::sync_with_stdio(0);
    // cin.tie(0);

    int n;
    cin >> n;

    priority_queue<pair<int, int>> pq;
    for (int i = 0; i < n; ++i) {
        int node1 = i;
        int node2;
        cin >> node2;
        --node2;

        pq.push({max(node1, node2), min(node1, node2)});
    }

    vector<int> color(n, 1);
    while (!pq.empty()) {
        pair<int, int> edge = pq.top();
        int node1 = edge.first;
        int node2 = edge.second;
        if (color[node1] == color[node2]) {
            color[node2] = ((color[node1] % 3) + 1);
        }
        pq.pop();
    }

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

Test details

Test 1

Verdict: ACCEPTED

input
2
2 1 

correct output
1 2 

user output
2 1 

Test 2

Verdict: ACCEPTED

input
3
2 1 1 

correct output
1 2 2 

user output
2 1 1 

Test 3

Verdict: ACCEPTED

input
4
3 1 4 2 

correct output
1 2 2 1 

user output
1 2 2 1 

Test 4

Verdict: ACCEPTED

input
5
5 5 1 5 4 

correct output
1 1 2 1 2 

user output
2 2 1 2 1 

Test 5

Verdict: ACCEPTED

input
10
3 1 9 9 3 4 10 10 5 1 

correct output
1 2 2 2 3 1 1 1 1 2 

user output
2 1 3 2 2 1 2 2 1 1 

Test 6

Verdict: ACCEPTED

input
10
9 10 4 3 9 1 1 4 2 6 

correct output
1 1 1 2 1 3 2 1 2 2 

user output
3 2 1 2 2 2 1 1 1 1 

Test 7

Verdict: ACCEPTED

input
10
3 8 4 5 10 8 5 10 4 6 

correct output
1 1 2 1 2 2 1 3 2 1 

user output
2 1 1 3 2 3 1 2 1 1 

Test 8

Verdict: ACCEPTED

input
10
9 1 10 3 9 4 6 9 3 5 

correct output
1 2 1 2 1 1 2 1 2 2 

user output
2 1 2 1 2 2 1 2 1 1 

Test 9

Verdict: ACCEPTED

input
10
4 6 5 5 1 2 4 2 1 3 

correct output
1 1 1 2 3 2 1 2 2 2 

user output
3 2 2 2 1 1 1 1 1 1 

Test 10

Verdict:

input
100
19 7 2 67 47 20 73 93 43 11 49...

correct output
1 1 2 1 1 1 3 2 1 2 1 1 2 1 1 ...

user output
3 3 2 2 3 1 3 2 1 3 2 2 1 2 1 ...

Test 11

Verdict:

input
1000
155 447 741 874 264 87 534 724...

correct output
1 1 2 1 1 1 1 1 1 1 1 2 1 2 2 ...

user output
2 2 2 3 2 3 3 1 3 3 2 3 1 2 1 ...

Test 12

Verdict:

input
10000
7778 6074 2376 8595 8243 8930 ...

correct output
1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 ...

user output
3 3 1 2 2 2 2 1 2 2 3 2 2 1 2 ...

Test 13

Verdict:

input
100000
51396 92191 77318 65910 87045 ...

correct output
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

user output
2 3 1 3 1 2 1 3 2 3 2 2 2 2 1 ...