1042.cpp (619B)
1 class Solution { 2 public: 3 vector<int> gardenNoAdj(int n, vector<vector<int>> &paths) { 4 vector<vector<int>> adj(n); 5 for (auto &p : paths) { 6 adj[p[0] - 1].push_back(p[1] - 1); 7 adj[p[1] - 1].push_back(p[0] - 1); 8 } 9 10 vector<int> res(n); 11 for (int i = 0; i < n; i++) { 12 bitset<5> colors; 13 14 for (int c : adj[i]) 15 colors.set(res[c]); 16 17 for (int j = 1; j < 5; j++) { 18 if (colors[j]) continue; 19 res[i] = j; 20 break; 21 } 22 } 23 return res; 24 } 25 };