MUV LUV UNLIMITED(ccpc 秦皇岛2019)
题目描述
There are few entertainments in United Nations 11th Force, Pacific Theater, Yokohama base, the only pastime for squad 207 is gathering in PX to play games after supper. However, whatever they play, Shirogane Takeru is always the loser. So he decides to use the game theory knowledge from
another world to become the winner. According to the knowledge he has learnt, Takeru introduces his army friends a game:
Given a rooted tree of size n, whose root is vertex 1. Two players do operations on the tree alternately.
In each operation, a player should choose several (at least one) leaf vertices (which have no children vertices) and remove them from the tree. As can be seen, there might be some new leaf vertices after one operation. The player who cannot make a move in his/her turn loses the game.
But unfortunately, Takeru doesn’t master the knowledge skillfully, so he has no idea whether the first player will win if the two players are playing optimally. Please help him determine that.
Assume that the two players are playing optimally to make themselves win, print “Takeru” in a single line if the first player will win, or print “Meiya” otherwise.
another world to become the winner. According to the knowledge he has learnt, Takeru introduces his army friends a game:
Given a rooted tree of size n, whose root is vertex 1. Two players do operations on the tree alternately.
In each operation, a player should choose several (at least one) leaf vertices (which have no children vertices) and remove them from the tree. As can be seen, there might be some new leaf vertices after one operation. The player who cannot make a move in his/her turn loses the game.
But unfortunately, Takeru doesn’t master the knowledge skillfully, so he has no idea whether the first player will win if the two players are playing optimally. Please help him determine that.
Assume that the two players are playing optimally to make themselves win, print “Takeru” in a single line if the first player will win, or print “Meiya” otherwise.
输入
The first line contains one positive integer T, denoting the number of test cases.
For each test case:
The first line contains one positive integer n (2 ≤ n ≤ 10 6), denoting the size of the given tree.
The next line contains n − 1 positive integers pi (1 ≤ pi ≤ n), where i-th integer denotes the parent vectex of vectex i + 1.
It is guaranteed that the sum of n among all cases in one test file does not exceed 10 6.
For each test case:
The first line contains one positive integer n (2 ≤ n ≤ 10 6), denoting the size of the given tree.
The next line contains n − 1 positive integers pi (1 ≤ pi ≤ n), where i-th integer denotes the parent vectex of vectex i + 1.
It is guaranteed that the sum of n among all cases in one test file does not exceed 10 6.
输出
Output T lines each contains a string “Takeru” or “Meiya”, denoting the answer to corresponding test case.
样例输入
复制样例数据
2
3
1 1
4
1 2 3
样例输出
Takeru
Meiya
提示
For the first case, the first player can remove vertex 3 firstly. As a result, the second player has no choice but to remove vertex 2. Finally, the first player removes vertex 1 and becomes the winner.
For the second case, the two players can only remove exactly one vertex in each operation alternately.
As can be seen, the winner, who removes vertex 1, will be the second player.
ps;题意:给你一棵树,每次可以删去叶子节点(任意数量),不能删就输了。明显一条链 奇数必胜,偶数必败。
其次,如果存在长度为1的枝条,先手必胜。
为什么???
是否删除这个点取决于 删后 对手是什么状态,可以根据删后的状态决定是否删除,画几个图想一想。
如果所有的枝条都是偶数,则必败,无论怎么删,对手都可以删回全偶数的状态。
所以这个题先找到每个枝条,看是否存在奇数长度的枝条就可以了。
关于怎么求枝条的长度,从叶子节点,找父亲,父亲的度必小于等于1.
代码如下:
#pragma GCC optimize(3 , "Ofast" , "inline")
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
int n;
int deg[N] , dep[N];
void init () {
for ( register int i = 0 ; i <= n ; i ++ ) dep[ i ] = deg[ i ] = 0;
}
int main () {
int T;
scanf ("%d" , &T);
while (T --) {
scanf ("%d" , &n);
for ( register int i = 2 ; i <= n ; i ++ ) {
int p;
scanf ("%d" , &p);
deg[ p ] ++;
dep[ i ] = p;
}
bool f = 0;
for ( register int i = 1 ; i <= n ; i ++ ) {
int cnt = 0;
if ( deg[ i ] == 0 ) {
int u = i;
while (u != 0 && deg[ u ] <= 1) {
cnt ++;
u = dep[ u ];
}
if ( cnt & 1 ) {
f = 1;
break;
}
}
}
if ( f ) {
printf ("Takeru\n");
} else {
printf ("Meiya\n");
}
init ();
}
return 0;
}