提交时间:2023-08-25 10:02:24

运行 ID: 100241

#include <bits/stdc++.h> using namespace std; bool ar[30][30]; int color[30]; int dfs(int n, int t) { if (color[n] != 0) return color[n]; color[n] = -1; bool *a = new bool[5]; for (int i = 1; i <= t; i++) { if (ar[n][i] && color[i] != -1) { a[color[i]] = true; } } for (int i = 1; i <= 4; i++) { if (!a[i]) { color[n] = i; break; } } delete a; for (int i = 1; i <= t; i++) { if (ar[n][i]) dfs(i, t); } return color[n]; } int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { int x, a, b; cin >> x >> a >> b; ar[x][a] = true; ar[x][b] = true; } dfs(1, n); for (int i = 1; i <= n; i++) { cout << color[i] << " "; } return 0; }