1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| signed main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n; std::cin >> n;
std::string s; std::cin >> s; s = '&' + s;
std::vector<std::vector<int>> adj(n + 1); for (int i = 1; i < n; i++) { int u, v; std::cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); }
bool ok = true; auto dfs = [&](auto self, int u, int fa) { if (s[u] == '?') s[u] = s[fa] == 'd' ? 'p' : 'd'; else if (s[u] == s[fa]) { ok = false; return ; } for (auto x : adj[u]) { if (x == fa) continue ; self(self, x, u); } }; dfs(dfs, 1, 0);
if (ok) { for (int i = 1; i <= n; i++) { std::cout << s[i]; } std::cout << "\n"; } else { std::cout << "-1\n"; }
return 0; }
|