-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10363.cpp
More file actions
59 lines (51 loc) · 1.13 KB
/
10363.cpp
File metadata and controls
59 lines (51 loc) · 1.13 KB
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
#include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define ll long long
using namespace std;
const ll INF = 1e9 + 7;
const ll DIV = 987654321;
int n;
int cnt[20202];
int grundy[20202];
vector<int> adj[20202];
int go(int cur){
int ret = 0;
vector<int> v;
for(int nxt : adj[cur]){
v.push_back(go(nxt));
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
while(ret < v.size() && v[ret] == ret) ret++;
return grundy[cur] = ret;
}
int main()
{
fastio;
int t;
cin >> t;
for(int j = 1;j<=t;j++){
cin >> n;
adj[0].clear();
for(int i = 1;i<=n;i++){
cin >> cnt[i];
adj[i].clear();
}
for(int i = 1;i<=n;i++){
int a;
cin >> a;
adj[a].push_back(i);
}
go(1);
int ans = 0;
for(int i= 1;i<=n;i++){
if(cnt[i]%2){
ans ^= grundy[i];
}
}
cout << "Case #"<< j <<": ";
if(ans) cout << "first\n";
else cout << "second\n";
}
return 0;
}