#include <bits/stdc++.h>
#define FOR(ii,aa,bb) for(int ii=aa;ii<bb;ii++)
#define for0(ii,bb) FOR(ii,0,bb)
#define for1(ii,bb) FOR(ii,1,bb+1)
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define st first
#define nd second
#define pii pair<int,int>
#define piii pair<int,pii>
#define pdi pair<double,int>
#define vi vector<int>
#define sp " "
#define nl "\n"
#define all(x) x.begin(),x.end()
#define fastio() ios_base::sync_with_stdio(0);cin.tie(0);
#define ll long long
#define int ll
using namespace std;
const int N = 1e5+5;
const int INF = 1e9+5;
const int mod = 998244353;
int n,s[2],can_move[2],dist[N][2],len,kacan;
vector<pii> g[N];
string str[3]={"Paula","Marin","Magenta"};
map<string,int> col;
void dfs(int v,int p,int x){
for(auto e:g[v]){
if(e.st==p)continue;
if(e.st==s[x^1])
len=dist[v][x]+1;
if(e.nd!=(x^1)){
dist[e.st][x]=dist[v][x]+1;
dfs(e.st,v,x);
}
}
}
bool check(int v,int p=-1){
bool ok=false;
for(auto e:g[v]){
if(e.st==p||e.nd==(kacan^1)||(dist[e.st][kacan^1]!=-1&&dist[e.st][kacan]>=dist[e.st][kacan^1]))
continue;
if(dist[e.st][kacan^1]==-1&&dist[v][kacan^1]==-1)
return true;
ok|=check(e.st,v);
}
return ok;
}
void print(int x){
cout << str[x] << nl;
exit(0);
}
signed main(){
fastio()
col["plava"]=0;
col["crvena"]=1;
col["magenta"]=2;
cin >> n >> s[0] >> s[1];
for(int i=1;i<n;i++){
int u,v;
string t;
cin >> u >> v >> t;
int c=col[t];
g[u].pb({v,c});
g[v].pb({u,c});
if(c!=1&&((s[0]==u&&s[1]!=v)||(s[0]==v&&s[1]!=u)))
can_move[0]=1;
if(c!=0&&(s[1]==u||s[1]==v))
can_move[1]=1;
}
if(!can_move[0])
print(1);
if(!can_move[1])
print(0);
memset(dist,-1,sizeof(dist));
dist[s[0]][0]=0;
dfs(s[0],-1,0);
dist[s[1]][1]=0;
dfs(s[1],-1,1);
kacan=(len+1)%2;
if(check(s[kacan]))
print(2);
print(kacan^1);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
3 ms |
4172 KB |
Output is correct |
3 |
Correct |
3 ms |
4172 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
3 ms |
4172 KB |
Output is correct |
6 |
Correct |
3 ms |
4172 KB |
Output is correct |
7 |
Correct |
2 ms |
2636 KB |
Output is correct |
8 |
Correct |
2 ms |
2640 KB |
Output is correct |
9 |
Correct |
2 ms |
2636 KB |
Output is correct |
10 |
Correct |
5 ms |
4172 KB |
Output is correct |
11 |
Correct |
3 ms |
4172 KB |
Output is correct |
12 |
Correct |
3 ms |
4172 KB |
Output is correct |
13 |
Correct |
3 ms |
4180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
84 ms |
9456 KB |
Output is correct |
2 |
Correct |
68 ms |
9284 KB |
Output is correct |
3 |
Correct |
78 ms |
9344 KB |
Output is correct |
4 |
Correct |
67 ms |
9284 KB |
Output is correct |
5 |
Correct |
74 ms |
9428 KB |
Output is correct |
6 |
Correct |
2 ms |
2760 KB |
Output is correct |
7 |
Correct |
3 ms |
4172 KB |
Output is correct |
8 |
Correct |
3 ms |
4172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
3 ms |
4172 KB |
Output is correct |
3 |
Correct |
3 ms |
4172 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
3 ms |
4172 KB |
Output is correct |
6 |
Correct |
3 ms |
4172 KB |
Output is correct |
7 |
Correct |
2 ms |
2636 KB |
Output is correct |
8 |
Correct |
2 ms |
2640 KB |
Output is correct |
9 |
Correct |
2 ms |
2636 KB |
Output is correct |
10 |
Correct |
5 ms |
4172 KB |
Output is correct |
11 |
Correct |
3 ms |
4172 KB |
Output is correct |
12 |
Correct |
3 ms |
4172 KB |
Output is correct |
13 |
Correct |
3 ms |
4180 KB |
Output is correct |
14 |
Correct |
84 ms |
9456 KB |
Output is correct |
15 |
Correct |
68 ms |
9284 KB |
Output is correct |
16 |
Correct |
78 ms |
9344 KB |
Output is correct |
17 |
Correct |
67 ms |
9284 KB |
Output is correct |
18 |
Correct |
74 ms |
9428 KB |
Output is correct |
19 |
Correct |
2 ms |
2760 KB |
Output is correct |
20 |
Correct |
3 ms |
4172 KB |
Output is correct |
21 |
Correct |
3 ms |
4172 KB |
Output is correct |
22 |
Correct |
2 ms |
2636 KB |
Output is correct |
23 |
Correct |
3 ms |
4172 KB |
Output is correct |
24 |
Correct |
3 ms |
4172 KB |
Output is correct |
25 |
Correct |
2 ms |
2636 KB |
Output is correct |
26 |
Correct |
3 ms |
4172 KB |
Output is correct |
27 |
Correct |
3 ms |
4172 KB |
Output is correct |
28 |
Correct |
2 ms |
2636 KB |
Output is correct |
29 |
Correct |
3 ms |
2636 KB |
Output is correct |
30 |
Correct |
2 ms |
2636 KB |
Output is correct |
31 |
Correct |
56 ms |
9496 KB |
Output is correct |
32 |
Correct |
53 ms |
9536 KB |
Output is correct |
33 |
Correct |
52 ms |
11076 KB |
Output is correct |
34 |
Correct |
52 ms |
11076 KB |
Output is correct |