#include <bits/stdc++.h>
using std::vector;
using std::array;
using std::pair;
using std::tuple;
template <class F> struct RecLambda : private F {
explicit RecLambda(F&& f) : F(std::forward<F>(f)) {}
template <class... Args> decltype(auto) operator()(Args&&... args) const {
return F::operator()(*this, std::forward<Args>(args)...);
}
};
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int N;
std::cin >> N;
array<int, 2> root;
for (auto& x : root) {
std::cin >> x;
x -= 1;
}
vector<vector<pair<int, int>>> graph(N);
for (int i = 1; i < N; ++i) {
int u, v;
std::string s;
std::cin >> u >> v >> s;
u -= 1, v -= 1;
const int k = (s == "plava" ? 0 : (s == "crvena" ? 1 : 2));
graph[u].emplace_back(v, k);
graph[v].emplace_back(u, k);
}
array<vector<int>, 2> depth = {}, parent = {};
array<vector<char>, 2> reach = {};
for (int k = 0; k < 2; ++k) {
depth[k] = vector<int>(N);
parent[k] = vector<int>(N);
reach[k] = vector<char>(N);
depth[k][root[k]] = 0;
parent[k][root[k]] = -1;
reach[k][root[k]] = true;
RecLambda([&](auto&& dfs, const int u) -> void {
for (const auto& [v, c] : graph[u]) {
if (v != parent[k][u]) {
depth[k][v] = depth[k][u] + 1;
parent[k][v] = u;
reach[k][v] = reach[k][u] and (c != (k ^ 1));
dfs(v);
}
}
})(root[k]);
}
if (std::count(reach[0].begin(), reach[0].end(), true) - reach[0][root[1]] == 1) {
std::cout << "Marin\n";
return 0;
}
if (std::count(reach[1].begin(), reach[1].end(), true) == 1) {
std::cout << "Paula\n";
return 0;
}
const int length = depth[0][root[1]];
const int type = length % 2;
vector<char> good(N);
for (int u = 0; u < N; ++u) {
if (reach[type][u] or !reach[type ^ 1][u]) {
continue;
}
for (const auto& [v, c] : graph[u]) {
if (!reach[type][v] and reach[type ^ 1][v]) {
good[u] = true;
break;
}
}
}
int src = root[type];
for (int i = 0; i <= length / 2; ++i) {
src = parent[type ^ 1][src];
if (!reach[type][src]) {
break;
}
}
if (RecLambda([&](auto&& dfs, const int u) -> bool {
if (good[u]) {
return true;
}
for (const auto& [v, k] : graph[u]) {
if (v != parent[type][u] and dfs(v)) {
return true;
}
}
return false;
})(src)) {
std::cout << "Magenta\n";
} else {
std::cout << (type == 0 ? "Paula" : "Marin") << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
308 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
312 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
8356 KB |
Output is correct |
2 |
Correct |
52 ms |
8388 KB |
Output is correct |
3 |
Correct |
54 ms |
8260 KB |
Output is correct |
4 |
Correct |
51 ms |
8296 KB |
Output is correct |
5 |
Correct |
51 ms |
8312 KB |
Output is correct |
6 |
Correct |
0 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
308 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
312 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
67 ms |
8356 KB |
Output is correct |
15 |
Correct |
52 ms |
8388 KB |
Output is correct |
16 |
Correct |
54 ms |
8260 KB |
Output is correct |
17 |
Correct |
51 ms |
8296 KB |
Output is correct |
18 |
Correct |
51 ms |
8312 KB |
Output is correct |
19 |
Correct |
0 ms |
312 KB |
Output is correct |
20 |
Correct |
1 ms |
316 KB |
Output is correct |
21 |
Correct |
1 ms |
312 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
0 ms |
320 KB |
Output is correct |
24 |
Correct |
0 ms |
204 KB |
Output is correct |
25 |
Correct |
0 ms |
316 KB |
Output is correct |
26 |
Correct |
0 ms |
204 KB |
Output is correct |
27 |
Correct |
0 ms |
204 KB |
Output is correct |
28 |
Correct |
0 ms |
204 KB |
Output is correct |
29 |
Correct |
0 ms |
312 KB |
Output is correct |
30 |
Correct |
0 ms |
204 KB |
Output is correct |
31 |
Correct |
53 ms |
9968 KB |
Output is correct |
32 |
Correct |
52 ms |
9916 KB |
Output is correct |
33 |
Correct |
68 ms |
9988 KB |
Output is correct |
34 |
Correct |
55 ms |
10012 KB |
Output is correct |