提交时间:2023-08-25 11:20:14

运行 ID: 100333

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