ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#214853 | #2642. color | nodgd | 100 | 150ms | 6552kb | C++11 | 2.5kb | 2024-11-22 19:37:43 | 2024-11-22 23:12:37 |
answer
#include <bits/stdc++.h>
using namespace std;
const int BUFFER_SIZE = 1 << 20;
char rb[BUFFER_SIZE], *rp = rb, *rt = rb;
inline char read_char() {
return rp == rt ? (rt = rb + fread(rb, 1, BUFFER_SIZE, stdin), rp = rb, *rp ++) : *rp ++;
}
inline int read_int() {
int x = 0;
char ch = read_char(), flag = 0;
while (ch != '-' && (ch < '0' || ch > '9')) {
ch = read_char();
}
if (ch == '-') {
flag = 1;
ch = read_char();
}
for (x = 0; ch >= '0' && ch <= '9'; ch = read_char()) {
x = x * 10 + (ch - '0');
}
return flag ? -x : x;
}
inline int read_string(char* s) {
char* s0 = s;
for (char ch = read_char(); ch > ' '; ch = read_char()) {
*s ++ = ch;
}
return s - s0;
}
const int MAX_N = 100000 + 5;
int N, M;
char a[MAX_N], b[5], ans[MAX_N];
int elast[MAX_N], ey[MAX_N << 1], enext[MAX_N << 1];
int fath[MAX_N], siz[MAX_N], son[MAX_N], anc[MAX_N], d[MAX_N], h[MAX_N];
void dfs_1(int u, int fa) {
fath[u] = fa;
siz[u] = 1;
d[u] = d[fa] + 1;
h[u] = h[fa] + (a[u] == 'H');
for (int j = elast[u], v; j; j = enext[j]) {
if ((v = ey[j]) != fa) {
dfs_1(v, u);
siz[u] += siz[v];
son[u] = siz[son[u]] < siz[v] ? v : son[u];
}
}
}
void dfs_2(int u, int anc_) {
anc[u] = anc_;
if (son[u]) {
dfs_2(son[u], anc_);
}
for (int j = elast[u], v; j; j = enext[j]) {
if ((v = ey[j]) != fath[u] && v != son[u]) {
dfs_2(v, v);
}
}
}
int get_lca(int u, int v) {
while (anc[u] != anc[v]) {
if (d[anc[u]] > d[anc[v]]) {
u = fath[anc[u]];
} else {
v = fath[anc[v]];
}
}
return d[u] < d[v] ? u : v;
}
int main() {
N = read_int(), M = read_int();
read_string(a + 1);
for (int i = 1, j = 1; i < N; i ++) {
int x = read_int(), y = read_int();
ey[j] = y, enext[j] = elast[x], elast[x] = j ++;
ey[j] = x, enext[j] = elast[y], elast[y] = j ++;
}
dfs_1(1, 0);
dfs_2(1, 1);
for (int i = 1; i <= M; i ++) {
int x = read_int(), y = read_int(), z = get_lca(x, y);
read_string(b);
int t = h[x] + h[y] - h[z] - h[fath[z]];
if (b[0] == 'H') {
ans[i] = t ? '1' : '0';
} else {
int s = d[x] + d[y] - d[z] - d[fath[z]];
ans[i] = t < s ? '1' : '0';
}
}
printf("%s\n", ans + 1);
return 0;
}
详细
小提示:点击横条可展开更详细的信息
Test #1:
score: 10
Accepted
time: 0ms
memory: 1244kb
input:
920 900 HHHHHHHHHHHHHHHHHHHHHHHHHGHHHHHHHHHHHHGHHHHHHHHHHHHHHHHHHHHHHHGHHGHHHHHHHGGHGHHHGHHGHGHHHHHH...
output:
0010100111100111101010001000010101100010001110110110001101100101111000101111100001111001110100001111...
result:
ok single line: '001010011110011110101000100001...1010100111010011101110111010101'
Test #2:
score: 10
Accepted
time: 0ms
memory: 1244kb
input:
927 949 HHHHGHGGGHHHHGHGHHHHHGHGGHGGGHHHGGHHHHHHGGGGGGHHHGGHHHHGHHHHGGGHHHGHGHHHHGGGGHHGHHGGHGHGGGGG...
output:
1111000011110111101101100110110011110101011000110111111111101110110011111011111110110011101010111111...
result:
ok single line: '111100001111011110110110011011...0011001011010111111011111001110'
Test #3:
score: 10
Accepted
time: 0ms
memory: 1244kb
input:
934 998 HHHHGHHHGHHGGGHGGHGHGHHHGHHGHGGGHHHHHGHHGGHHHHHHGHHHGGHHHHHGGHHHGHHHHHHHGHHGHGHGGHHGHGHHHGGH...
output:
1101010111111010110101011111110111111101101100011111001101110111111111011111101001111011001010101111...
result:
ok single line: '110101011111101011010101111111...1011101110011100101111100101110'
Test #4:
score: 10
Accepted
time: 0ms
memory: 1240kb
input:
941 947 HHHHHHHHHHHHHHHGHHHGHHHGHHHHGHHHGHHHHHHHHGHHHHHHHHHGHHHHHGHHHHHGHHGHHHHGHGGHHGHHHHHHHHHHGGHH...
output:
1111111011010110001101010101110100110001000101100101111101110001111111111110111011010101001111101111...
result:
ok single line: '111111101101011000110101010111...0101100011110110011000000000011'
Test #5:
score: 10
Accepted
time: 22ms
memory: 6356kb
input:
92189 98896 HHHHHHHHHGHHHHGGGHGHHHHHHGHHHHHGHHHHHHHHHGHGHHHGHHHHHHGGHHHGHGGHHHHHHHHHGHHHHHHGHGHGHHHG...
output:
1110111111111111111111111111111111111011110111111111111111110111111101111111111111111111111111101111...
result:
ok single line: '111011111111111111111111111111...1111111111111111111111111111111'
Test #6:
score: 10
Accepted
time: 20ms
memory: 6520kb
input:
95803 95747 HHHHHHHHHHHHHGHHHHHHHHHHHHHHHHHHGHHGHGHHHHGHGHHHHHHGGHHHHHHHHHHHHGHHHGHHHHHHHHHGHGGHHHHH...
output:
1011011101111111111111111101111111111011101101111110111111101111111011110111111101111111001100101111...
result:
ok single line: '101101110111111111111111110111...1111101101111110111111111001111'
Test #7:
score: 10
Accepted
time: 26ms
memory: 6372kb
input:
92610 90996 HHGGGHHGHGGGHGHGHGGGGGGHGGGHGGHGHGHGGHHHHHGHGGHGHGGGGHGGGHGGHHGHHHGGHGHGGHGGGHGGGGGGGGGG...
output:
1111111111111111111011111111111111111110111111111111100111111111011111111111101101111111111111011111...
result:
ok single line: '111111111111111111101111111111...1111110011111111111111111111111'
Test #8:
score: 10
Accepted
time: 26ms
memory: 6528kb
input:
96224 91494 HHHHHHHHGGHHGHHHHHHHHHHHHHGHHHGHHHHHHHHHHHHHHHHHGGHHHHHHGHHHHHHHHHHHHHHHHHGHGGGHGHGHHHHH...
output:
1111010110111100111111111111011011010101011111011101111011111011111011111111111111111111111011111101...
result:
ok single line: '111101011011110011111111111101...1101011011111111111111101101111'
Test #9:
score: 10
Accepted
time: 29ms
memory: 6396kb
input:
93031 96743 HHHHHHHHHGHHHHHHHHHHHHHHHHHGGHHHHGHHHGHHHHHHHHHGHHHHHHHHHHHHHHGGHHHHHGGHGHHHGHHHGHHHHHHG...
output:
0100011111101111011111110111001111101111110101110110111111111111111101111110110001011110011111011111...
result:
ok single line: '010001111110111101111111011100...1111111101110011111101011111110'
Test #10:
score: 10
Accepted
time: 27ms
memory: 6552kb
input:
96645 93594 HHHHHHHHHGHGHHHHHHHGHHHHGGGHHHHHHHHHHHHHHHHHHGHHHHHGHGHHHHHGHGHHHHHGHHHHGHHHHGGHHGHHHHGH...
output:
0101000110111110011111111111110111101011011101111110111111011011111111111110111110101111111111111111...
result:
ok single line: '010100011011111001111111111111...1110011111111111110110011111111'