#include <bits/stdc++.h>
//#include "includeall.h"
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define endl '\n'
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define input(x) scanf("%lld", &x);
#define input2(x, y) scanf("%lld%lld", &x, &y);
#define input3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z);
#define input4(x, y, z, a) scanf("%lld%lld%lld%lld", &x, &y, &z, &a);
#define print(x, y) printf("%lld%c", x, y);
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x,y) cerr << #x << " is " << x << " " << #y << " is " << y << endl;
#define show3(x,y,z) cerr << #x << " is " << x << " " << #y << " is " << y << " " << #z << " is " << z << endl;
#define all(x) x.begin(), x.end()
#define discretize(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
#define FOR(i, x, n) for (ll i =x; i<=n; ++i)
#define RFOR(i, x, n) for (ll i =x; i>=n; --i)
#pragma GCC optimize("O3","unroll-loops")
using namespace std;
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
//#define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
typedef long long ll;
typedef long double ld;
typedef pair<ld, ll> pd;
typedef pair<string, ll> psl;
typedef pair<ll, ll> pi;
typedef pair<pi, ll> pii;
typedef pair<pi, pi> piii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll const INF = 1e15;
ll n, a, b, b1 = 1, b2 = 2;
unordered_map<string, ll> hh;
string ans[4] = {"fuck", "Paula", "Marin", "Magenta"};
vector<ll> dist[2];
vector<pi> adj[100005];
void dfs(ll x, ll p, vector<ll> & dist, ll b)
{
for (pi u: adj[x])
{
if (u.f !=p && (u.s & b))
{
dist[u.f] = dist[x] + 1;
dfs(u.f, x, dist, b);
}
}
}
bool dfs2(ll x, ll p =-1)
{
for (pi u: adj[x])
{
if (u.f == p || ((u.s&b2) == 0) || dist[0][u.f] <= dist[1][u.f]) continue;
if (dist[0][x] == INF && dist[0][u.f] == INF) return 1;
if (dfs2(u.f, x)) return 1;
}
return 0;
}
int main()
{
hh["plava"] = 1;
hh["crvena"] = 2;
hh["magenta"] = 3;
cin >> n >> a >> b;
for (ll i=0; i<n-1; ++i)
{
ll u, v;
string co;
cin >> u >> v >> co;
adj[u].pb(mp(v, hh[co]));
adj[v].pb(mp(u, hh[co]));
}
// case where it cant move a single time
ll flag = 0;
for (pi u: adj[a]) if (u.f != b && (u.s&b1)) flag = 1;
if (flag==0) {cout << "Marin\n"; return 0;}
flag = 0;
for (pi u: adj[b]) if ( (u.s&b2)) flag = 1;
if (flag==0) {cout << "Paula\n"; return 0;}
// root at a, b and calculate distance
dist[0].assign(n + 1, INF);
dist[0][a] = 0;
dist[1].assign(n + 1, INF);
dist[1][b] = 0;
dfs(a, -1, dist[0], 1);
dfs(b, -1, dist[1],2);
//show(dist[0][b]);
if (dist[0][b]==INF || dist[0][b]%2) {swap(a, b); swap(dist[0], dist[1]); swap(b1, b2);}
//show2(a, b);
// a in chasing, b is escaping
if (dfs2(b, -1)) cout << ans[3] << endl;
else cout << ans[b1] << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |