Editorial for Trò Chơi Trên Cây Vui Vẻ
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
/*input 2 5 01111 1 2 1 3 2 4 3 5 5 11001 1 2 1 3 2 4 3 5 */ #include <bits/stdc++.h> using namespace std; vector<int> get_g(const vector<vector<int>>& adj) { int n = adj.size(); vector<int> g(n); vector<int> dad(n, -1); function<void(int)> dfs = [&](int u) -> void { int dupe = 0; for (int v: adj[u]) { if (v == dad[u]) { continue; } dad[v] = u; dfs(v); dupe = max(dupe, g[u] & g[v]); g[u] |= g[v]; } int bit = 1; while (bit <= dupe) { g[u] |= bit; bit *= 2; } g[u]++; }; dfs(0); for (int i = 0; i < n; ++i) { g[i] &= -g[i]; } return g; } int main(void) { ios_base::sync_with_stdio(false); cin.tie(NULL); int ntest; cin >> ntest; while (ntest--) { int n; string s; cin >> n >> s; vector<vector<int>> adj(n); for (int i = 1; i < n; ++i) { int u, v; cin >> u >> v; --u, --v; adj[u].push_back(v); adj[v].push_back(u); } vector<int> g = get_g(adj); int ans = 0; for (int i = 0; i < n; ++i) { if (s[i] == '1') { ans ^= g[i]; } } cout << (ans ? "MofK" : "ngfam") << '\n'; } }
Comments