#include<bits/stdc++.h>
using namespace std;
#define X first
#define Y second
map <string, int> col = {
{"plava", 0},
{"crvena", 1},
{"magenta", 2}
};
const int N = 1e5 + 100;
int n;
int dist[2][N];
vector <pair <int, int> > g[N];
vector <string> names = {"Paula", "Marin", "Magenta"};
int move_to[2];
vector <int> s;
void print(int a)
{
cout << names[a];
}
int len;
void dfs(int v, int i, int p = -1)
{
for(int j = 0; j < g[v].size(); j++)
{
int to = g[v][j].X;
int type = g[v][j].Y;
if(to == p)
{
continue;
}
if(to == s[i ^ 1])
{
len = dist[i][v] + 1;
}
if(type != (i ^ 1))
{
dist[i][to] = dist[i][v] + 1;
dfs(to, i, v);
}
}
}
int val;
bool funct(int v, int p =-1)
{
bool fl = 0;
for(int i = 0; i < g[v].size(); i++)
{
int to = g[v][i].X;
int w = g[v][i].Y;
if(to == p || w == (val ^ 1) || dist[val ^ 1][to] != 1e9 && dist[val][to] >= dist[val ^ 1][to])
{
continue;
}
if(dist[val ^ 1][v]== 1e9 && dist[val ^ 1][to] == 1e9)
{
return 1;
}
fl |= funct(to, v);
}
return fl;
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
int a, b;
cin >> a >> b;
a--;
b--;
s = {a, b};
for(int i = 0; i < n - 1; i++)
{
int u, v;
cin >> u >> v;
u--;
v--;
string s;
cin >> s;
int type = col[s];
g[u].push_back({v, type});
g[v].push_back({u, type});
if(type != 1 && (u == a && v != b || v == a && u != b))
{
move_to[0] = 1;
}
if(type != 0 && (u == b || v == b))
{
move_to[1] = 1;
}
}
if(!move_to[0])
{
print(1);
return 0;
}
if(!move_to[1])
{
print(0);
return 0;
}
for(int i = 0; i < 2; i++)
{
int st = s[i];
fill(dist[i], dist[i] + N, 1e9);
dist[i][s[i]] = 0;
dfs(s[i], i);
}
val = (len + 1) % 2;
if(funct(s[val], -1))
{
print(2);
return 0;
}
print(1 - val);
return 0;
}
Compilation message
Main.cpp: In function 'void dfs(int, int, int)':
Main.cpp:24:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for(int j = 0; j < g[v].size(); j++)
| ~~^~~~~~~~~~~~~
Main.cpp: In function 'bool funct(int, int)':
Main.cpp:47:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for(int i = 0; i < g[v].size(); i++)
| ~~^~~~~~~~~~~~~
Main.cpp:51:66: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
51 | if(to == p || w == (val ^ 1) || dist[val ^ 1][to] != 1e9 && dist[val][to] >= dist[val ^ 1][to])
| ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:85:33: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
85 | if(type != 1 && (u == a && v != b || v == a && u != b))
| ~~~~~~~^~~~~~~~~
Main.cpp:106:13: warning: unused variable 'st' [-Wunused-variable]
106 | int st = s[i];
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2796 KB |
Output is correct |
2 |
Correct |
3 ms |
3436 KB |
Output is correct |
3 |
Correct |
2 ms |
3436 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
3 ms |
3436 KB |
Output is correct |
6 |
Correct |
3 ms |
3436 KB |
Output is correct |
7 |
Correct |
2 ms |
2668 KB |
Output is correct |
8 |
Correct |
2 ms |
2668 KB |
Output is correct |
9 |
Correct |
2 ms |
2668 KB |
Output is correct |
10 |
Correct |
3 ms |
3436 KB |
Output is correct |
11 |
Correct |
3 ms |
3436 KB |
Output is correct |
12 |
Correct |
3 ms |
3436 KB |
Output is correct |
13 |
Correct |
2 ms |
3436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
7148 KB |
Output is correct |
2 |
Correct |
72 ms |
7272 KB |
Output is correct |
3 |
Correct |
73 ms |
7148 KB |
Output is correct |
4 |
Correct |
69 ms |
7148 KB |
Output is correct |
5 |
Correct |
74 ms |
7148 KB |
Output is correct |
6 |
Correct |
2 ms |
2668 KB |
Output is correct |
7 |
Correct |
2 ms |
3436 KB |
Output is correct |
8 |
Correct |
2 ms |
3436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2796 KB |
Output is correct |
2 |
Correct |
3 ms |
3436 KB |
Output is correct |
3 |
Correct |
2 ms |
3436 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
3 ms |
3436 KB |
Output is correct |
6 |
Correct |
3 ms |
3436 KB |
Output is correct |
7 |
Correct |
2 ms |
2668 KB |
Output is correct |
8 |
Correct |
2 ms |
2668 KB |
Output is correct |
9 |
Correct |
2 ms |
2668 KB |
Output is correct |
10 |
Correct |
3 ms |
3436 KB |
Output is correct |
11 |
Correct |
3 ms |
3436 KB |
Output is correct |
12 |
Correct |
3 ms |
3436 KB |
Output is correct |
13 |
Correct |
2 ms |
3436 KB |
Output is correct |
14 |
Correct |
87 ms |
7148 KB |
Output is correct |
15 |
Correct |
72 ms |
7272 KB |
Output is correct |
16 |
Correct |
73 ms |
7148 KB |
Output is correct |
17 |
Correct |
69 ms |
7148 KB |
Output is correct |
18 |
Correct |
74 ms |
7148 KB |
Output is correct |
19 |
Correct |
2 ms |
2668 KB |
Output is correct |
20 |
Correct |
2 ms |
3436 KB |
Output is correct |
21 |
Correct |
2 ms |
3436 KB |
Output is correct |
22 |
Correct |
2 ms |
2668 KB |
Output is correct |
23 |
Correct |
3 ms |
3436 KB |
Output is correct |
24 |
Correct |
3 ms |
3436 KB |
Output is correct |
25 |
Correct |
2 ms |
2668 KB |
Output is correct |
26 |
Correct |
2 ms |
3436 KB |
Output is correct |
27 |
Correct |
2 ms |
3436 KB |
Output is correct |
28 |
Correct |
2 ms |
2688 KB |
Output is correct |
29 |
Correct |
2 ms |
2668 KB |
Output is correct |
30 |
Correct |
2 ms |
2668 KB |
Output is correct |
31 |
Correct |
50 ms |
6380 KB |
Output is correct |
32 |
Correct |
50 ms |
6380 KB |
Output is correct |
33 |
Correct |
58 ms |
7240 KB |
Output is correct |
34 |
Correct |
56 ms |
7148 KB |
Output is correct |