Advertisement
Guest User

Untitled

a guest
May 24th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int N = 1e5 + 5;
  6.  
  7. vector <int> g[N];
  8. char res[N];
  9. int cnt[N][26];
  10.  
  11. void dfs(int v, int pr = -1) {
  12. for (int to : g[v]) {
  13. if (to == pr) continue;
  14. dfs(to, v);
  15. }
  16. int x = 0;
  17. for (int i = 0; i < 26; i++) {
  18. if (cnt[v][i] > 1) break;
  19. if (!cnt[v][i]) x = i;
  20. }
  21. res[v] = 'a' + x;
  22. cnt[v][x] = 1;
  23. for (int i = 0; i <= x; i++) {
  24. if (pr != -1) cnt[pr][i] += cnt[v][i];
  25. }
  26. }
  27.  
  28. int main() {
  29. ios_base::sync_with_stdio(0);
  30. cin.tie(0);
  31. cout.tie(0);
  32. #ifdef LOCAL
  33. freopen("input.txt", "r", stdin);
  34. freopen("output.txt", "w", stdout);
  35. #endif
  36. int n;
  37. cin >> n;
  38. for (int i = 0; i < n - 1; i++) {
  39. int x, y;
  40. cin >> x >> y;
  41. g[x - 1].push_back(y - 1);
  42. g[y - 1].push_back(x - 1);
  43. }
  44. dfs(0);
  45. for (int i = 0; i < n; i++) cout << res[i];
  46. return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement