0%

HDU 5963 朋友 题解

HDU 5963 朋友 题解

题解

手画了一下样例,会发现奇数次操作则女生赢,偶数次操作则男生赢,我们考虑以 \(u\) 为根节点,引出了一些连向子树的边 \(s_1,\cdots, s_p\),我们从整体考虑,如果这个边是 \(1\),则我们一定要在该子树里进行奇数次操作, 边为 \(0\) 时同理,这样我们就可以得到总共需要的操作的奇偶性,也就是得到了答案。

代码

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
// https://acm.hdu.edu.cn/showproblem.php?pid=5963
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cassert>
#include <cmath>
using namespace std;
typedef long long ll;

template <typename _Tp>
void read(_Tp &a, char c = 0, int f = 1) {
for(c = getchar(); !isdigit(c); c = getchar()) if(c == '-') f = -1;
for(a = 0; isdigit(c); a = a * 10 + c - '0', c = getchar()); a *= f;
}

template <typename _Tp>
void write(_Tp a) {
if(a < 0) putchar('-'), a = -a;
if(a > 9) write(a / 10); putchar(a % 10 + '0');
}

const int N = 4e4 + 5;

int n, m, tot, hd[N], nxt[2 * N], to[2 * N], w[2 * N];

void add(int u, int v, int c) {
nxt[++tot] = hd[u], to[hd[u] = tot] = v, w[tot] = c;
}

void alter(int u, int v, int c) {
for(int e = hd[u]; e != -1; e = nxt[e]) {
if(to[e] == v) {
w[e] = c;
w[e ^ 1] = c;
}
}
}

void init() {
tot = -1;
memset(hd, -1, sizeof hd);
memset(to, 0, sizeof to);
memset(w, 0, sizeof w);
memset(nxt, -1, sizeof nxt);
}

int main() {
// freopen("0728_2.in", "r", stdin);
int T;
read(T);
while(T--) {
init();
read(n), read(m);
for(int i = 1; i < n; i++) {
int u, v, c;
read(u), read(v), read(c);
add(u, v, c);
add(v, u, c);
}
while(m--) {
int op;
read(op);
switch(op) {
case 0: {
int root;
read(root);
int x = 0;
for(int e = hd[root]; e != -1; e = nxt[e]) {
x += w[e];
}
if(x & 1) puts("Girls win!");
else puts("Boys win!");
break;
}
case 1: {
int u, v, c;
read(u), read(v), read(c);
alter(u, v, c);
break;
}
}
}
}
return 0;
}