提交时间:2023-08-25 11:04:11

运行 ID: 100311

#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* inp() { string s; getline(cin, s); int *p = new int[105]; int cur=0, cnt=0; for (int i = 0; i < s.size(); i++) { if (s[i] == ' ') { p[++cnt] = cur; cur = 0; } else if (s[i] == '\n') cout << 1 << endl; else cur = cur * 10 + (s[i] - '0'); } p[++cnt] = cur; p[++cnt] = -1; return p; } int main() { int n; cin >> n; cin.get(); for (int i = 1; i <= n; i++) { int *p = inp(); int x = p[1]; for (int j = 2;;j++) { if (p[j] == -1) break; ar[x][p[j]] = true; } cout << endl; } dfs(1, n); for (int i = 1; i <= n; i++) { cout << color[i] << " "; } return 0; }