-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaidu3.cpp
91 lines (82 loc) · 1.88 KB
/
baidu3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;
struct Node
{
int number;
enum class Color { null=0, black=1, white=2 } color;
vector<Node*> neighbors;
Node(int n): number(n), color(Color::null) {}
};
string Winner(unordered_map<int, Node*>& map)
{
auto IsBlack = [](const std::pair<int, Node*>& v)
{
return v.second->color == Node::Color::black;
};
auto IsWhite = [](const std::pair<int, Node*>& v)
{
return v.second->color == Node::Color::white;
};
for (size_t i = 1; i <= map.size(); ++i)
{
if (i & 1) // niuniu's step
{
bool flag = false;
auto it = std::find_if(map.begin(), map.end(), IsBlack);
for (auto e : it->second->neighbors)
{
if (e->color == Node::Color::null)
{
e->color = Node::Color::black;
flag = true;
break;
}
}
if (!flag) return "niumei";
}
else // niumei's step
{
bool flag = false;
auto it = std::find_if(map.begin(), map.end(), IsWhite);
for (auto e : it->second->neighbors)
{
if (e->color == Node::Color::null)
{
e->color = Node::Color::white;
flag = true;
break;
}
}
if (!flag) return "niuniu";
}
}
return "niumei";
}
int main()
{
int num_group;
cin >> num_group;
while (num_group--)
{
int num_node;
cin >> num_node;
unordered_map<int, Node*> map;
for (int i = 1; i <= num_node; ++i)
{
map[i] = new Node(i);
}
map[1]->color = Node::Color::black;
map[num_node]->color = Node::Color::white;
for (int from, to; --num_node;)
{
cin >> from >> to;
(map[from]->neighbors).push_back(map[to]);
(map[to]->neighbors).push_back(map[from]);
}
cout << Winner(map) << endl;
for (auto& e : map) delete e.second;
}
}