Task: | 3-Coloring |
Sender: | bubu2006 |
Submission time: | 2024-11-27 17:40:53 +0200 |
Language: | C++ (C++20) |
Status: | READY |
Result: | ACCEPTED |
test | verdict | time | |
---|---|---|---|
#1 | ACCEPTED | 0.01 s | details |
#2 | ACCEPTED | 0.01 s | details |
#3 | ACCEPTED | 0.01 s | details |
#4 | ACCEPTED | 0.01 s | details |
#5 | ACCEPTED | 0.01 s | details |
#6 | ACCEPTED | 0.01 s | details |
#7 | ACCEPTED | 0.01 s | details |
#8 | ACCEPTED | 0.01 s | details |
#9 | ACCEPTED | 0.01 s | details |
#10 | ACCEPTED | 0.01 s | details |
#11 | ACCEPTED | 0.01 s | details |
#12 | ACCEPTED | 0.01 s | details |
#13 | ACCEPTED | 0.06 s | details |
Code
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for(int i = a; i < (b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; string to_string(string s) { return '"' + s + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif const int N = 1e5 + 5; int n, e[N]; vector<int> adj[N]; int col[N]; void dfs(int u, int c) { col[u] = c; for (int v : adj[u]) { if (col[v]) continue; dfs(v, 3 - c); } } signed main() { cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); // RTE if input wrong datatype cin >> n; for (int i = 1; i <= n; i++) { cin >> e[i]; adj[e[i]].push_back(i); adj[i].push_back(e[i]); } for (int i = 1; i <= n; i++) { if (!col[i]) { dfs(i, 1); } } for (int i = 1; i <= n; i++) { bool found = false; for (int v : adj[i]) { if (col[v] == col[i]) { found = true; break; } } if (found) { col[i] = 3; } } // for (int i = 1; i <= n; i++) { // for (int v : adj[i]) { // assert(col[i] != col[v]); // } // } for (int i = 1; i <= n; i++) { cout << col[i] << ' '; } }
Test details
Test 1
Verdict: ACCEPTED
input |
---|
2 2 1 |
correct output |
---|
1 2 |
user output |
---|
1 2 |
Test 2
Verdict: ACCEPTED
input |
---|
3 2 1 1 |
correct output |
---|
1 2 2 |
user output |
---|
1 2 2 |
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 |
---|
1 1 2 1 2 |
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 |
---|
1 2 3 2 2 1 1 1 1 2 |
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 1 1 2 1 1 2 1 2 2 |
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 |
---|
1 1 2 1 2 3 1 2 2 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 |
---|
1 2 1 2 1 1 2 1 2 2 |
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 1 2 2 1 2 1 2 2 1 |
Test 10
Verdict: ACCEPTED
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 |
---|
1 2 1 1 1 2 1 1 2 2 1 1 1 1 1 ... |
Test 11
Verdict: ACCEPTED
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 |
---|
1 1 2 1 1 1 1 1 1 1 1 2 1 2 2 ... |
Test 12
Verdict: ACCEPTED
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 |
---|
1 1 2 1 2 1 2 1 1 2 2 1 2 1 1 ... |
Test 13
Verdict: ACCEPTED
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 |
---|
1 1 2 1 1 2 1 1 1 1 2 2 1 2 2 ... |