#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 1;
const int INF = 1e9 + 7;
struct Edge{
int u, v, c;
Edge(int _u, int _v, int _c){
u = _u;
v = _v;
c = _c;
}
};
int n, a, b;
vector<Edge> adj[N];
int dist[N], distFromA[N], distFromB[N];
bool vst[N];
int dfs(int u, int p, int dist[], int type){
if (p == -1)
dist[u] = 0;
int res = 1;
for(Edge e : adj[u])
if (e.v != p && (e.c & type)){
dist[e.v] = dist[u] + 1;
res += dfs(e.v, u, dist, type);
}
return res;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> a >> b;
for(int i = 1; i < n; i++){
int u, v, c;
string s;
cin >> u >> v >> s;
if (s == "plava")
c = 1;
else if (s == "crvena")
c = 2;
else
c = 3;
adj[u].push_back(Edge(u, v, c));
adj[v].push_back(Edge(v, u, c));
}
for(int i = 1; i <= n; i++)
dist[i] = distFromA[i] = distFromB[i] = INF;
dfs(a, -1, dist, 3);
dfs(a, -1, distFromA, 1);
dfs(b, -1, distFromB, 2);
bool ACanMove = 0, BCanMove = 0;
for(Edge e : adj[a])
if (e.v != b && (e.c & 1))
ACanMove = 1;
for(Edge e : adj[b])
if ((e.c & 2))
BCanMove = 1;
if (!ACanMove){
cout << "Marin";
return 0;
}
if (!BCanMove){
cout << "Paula";
return 0;
}
if (dist[b] % 2 == 0){
queue<int> qu;
vst[b] = 1;
qu.push(b);
while(!qu.empty()){
int u = qu.front();
qu.pop();
for(Edge e : adj[u]){
int v = e.v;
int c = e.c;
if (distFromB[v] < distFromA[v] && !vst[v] && (c & 2)){
vst[v] = 1;
qu.push(v);
}
}
}
for(int i = 1; i <= n; i++)
vst[i] &= (distFromA[i] == INF);
for(int u = 1; u <= n; u++)
for(Edge e : adj[u])
if (vst[u] && vst[e.v]){
cout << "Magenta";
return 0;
}
cout << "Paula";
} else {
queue<int> qu;
vst[a] = 1;
qu.push(a);
while(!qu.empty()){
int u = qu.front();
qu.pop();
for(Edge e : adj[u]){
int v = e.v;
int c = e.c;
if (distFromA[v] <= distFromB[v] && !vst[v] && (c & 1)){
vst[v] = 1;
qu.push(v);
}
}
}
for(int i = 1; i <= n; i++)
vst[i] &= (distFromB[i] == INF);
for(int u = 1; u <= n; u++)
for(Edge e : adj[u])
if (vst[u] && vst[e.v]){
cout << "Magenta";
return 0;
}
cout << "Marin";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Correct |
3 ms |
2668 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
2668 KB |
Output is correct |
6 |
Correct |
2 ms |
2668 KB |
Output is correct |
7 |
Correct |
3 ms |
2668 KB |
Output is correct |
8 |
Correct |
3 ms |
2668 KB |
Output is correct |
9 |
Correct |
3 ms |
2668 KB |
Output is correct |
10 |
Correct |
3 ms |
2668 KB |
Output is correct |
11 |
Correct |
3 ms |
2668 KB |
Output is correct |
12 |
Correct |
3 ms |
2668 KB |
Output is correct |
13 |
Correct |
4 ms |
2668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
106 ms |
8340 KB |
Output is correct |
2 |
Correct |
84 ms |
8172 KB |
Output is correct |
3 |
Correct |
92 ms |
8300 KB |
Output is correct |
4 |
Correct |
85 ms |
8172 KB |
Output is correct |
5 |
Correct |
80 ms |
8172 KB |
Output is correct |
6 |
Correct |
2 ms |
2668 KB |
Output is correct |
7 |
Correct |
2 ms |
2668 KB |
Output is correct |
8 |
Correct |
2 ms |
2668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Correct |
3 ms |
2668 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
2668 KB |
Output is correct |
6 |
Correct |
2 ms |
2668 KB |
Output is correct |
7 |
Correct |
3 ms |
2668 KB |
Output is correct |
8 |
Correct |
3 ms |
2668 KB |
Output is correct |
9 |
Correct |
3 ms |
2668 KB |
Output is correct |
10 |
Correct |
3 ms |
2668 KB |
Output is correct |
11 |
Correct |
3 ms |
2668 KB |
Output is correct |
12 |
Correct |
3 ms |
2668 KB |
Output is correct |
13 |
Correct |
4 ms |
2668 KB |
Output is correct |
14 |
Correct |
106 ms |
8340 KB |
Output is correct |
15 |
Correct |
84 ms |
8172 KB |
Output is correct |
16 |
Correct |
92 ms |
8300 KB |
Output is correct |
17 |
Correct |
85 ms |
8172 KB |
Output is correct |
18 |
Correct |
80 ms |
8172 KB |
Output is correct |
19 |
Correct |
2 ms |
2668 KB |
Output is correct |
20 |
Correct |
2 ms |
2668 KB |
Output is correct |
21 |
Correct |
2 ms |
2668 KB |
Output is correct |
22 |
Correct |
2 ms |
2668 KB |
Output is correct |
23 |
Correct |
2 ms |
2668 KB |
Output is correct |
24 |
Correct |
2 ms |
2668 KB |
Output is correct |
25 |
Correct |
3 ms |
2668 KB |
Output is correct |
26 |
Correct |
2 ms |
2668 KB |
Output is correct |
27 |
Correct |
2 ms |
2668 KB |
Output is correct |
28 |
Correct |
2 ms |
2668 KB |
Output is correct |
29 |
Correct |
2 ms |
2668 KB |
Output is correct |
30 |
Correct |
2 ms |
2668 KB |
Output is correct |
31 |
Correct |
75 ms |
10020 KB |
Output is correct |
32 |
Correct |
67 ms |
9964 KB |
Output is correct |
33 |
Correct |
75 ms |
9964 KB |
Output is correct |
34 |
Correct |
86 ms |
9964 KB |
Output is correct |