Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 1e5 + 5;
- vector <int> g[N];
- char res[N];
- int cnt[N][26];
- void dfs(int v, int pr = -1) {
- for (int to : g[v]) {
- if (to == pr) continue;
- dfs(to, v);
- }
- int x = 0;
- for (int i = 0; i < 26; i++) {
- if (cnt[v][i] > 1) break;
- if (!cnt[v][i]) x = i;
- }
- res[v] = 'a' + x;
- cnt[v][x] = 1;
- for (int i = 0; i <= x; i++) {
- if (pr != -1) cnt[pr][i] += cnt[v][i];
- }
- }
- int main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- #ifdef LOCAL
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- int n;
- cin >> n;
- for (int i = 0; i < n - 1; i++) {
- int x, y;
- cin >> x >> y;
- g[x - 1].push_back(y - 1);
- g[y - 1].push_back(x - 1);
- }
- dfs(0);
- for (int i = 0; i < n; i++) cout << res[i];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement