UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#214834#2642. colorlinzhiyi01098ms32076kbC++1.5kb2024-11-22 18:46:222024-11-22 23:10:26

answer

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, m, fa[N][25], dep[N], cnt[N][25][2];
char c[N];
struct edge
{
	int ed, next;
}b[N << 1];
int nbs[N], tot;
void add(int u, int v)
{
	b[++ tot] = {v, nbs[u]}, nbs[u] = tot;
}
void dfs(int x, int f)
{
	fa[x][0] = f, dep[x] = dep[f] + 1;
	if (c[x] == 'H') cnt[x][0][0] ++;
	else cnt[x][0][1] ++;
	for (int i = nbs[x]; i; i = b[i].next)
	{
		int j = b[i].ed;
		if (j == f) continue;
		dfs(j, x);
	}
	for (int i = 1; i <= 25; i ++ )
	{
		fa[x][i] = fa[fa[x][i - 1]][i - 1];
		cnt[x][i][0] = cnt[x][i - 1][0] + cnt[fa[x][i - 1]][i - 1][0];
		cnt[x][i][1] = cnt[x][i - 1][1] + cnt[fa[x][i - 1]][i - 1][1];
	}
}
int lca(int x, int y, int color)
{
	int res = 0;
	if (dep[x] < dep[y]) swap(x, y);
	for (int i = 25; i >= 0; i -- )
		if (dep[fa[x][i]] >= dep[y])
			res += cnt[x][i][color], x = fa[x][i];
	if (x == y) return res;
	for (int i = 25; i >= 0; i -- )
		if (fa[x][i] != fa[y][i])
			res += cnt[x][i][color] + cnt[y][i][color], x = fa[x][i], y = fa[y][i];
	return res + cnt[x][0][color] + cnt[y][0][color];
}
int main()
{
	string ans;
	cin >> n >> m;
	for (int i = 1; i <= n; i ++ )
		cin >> c[i];
	for (int i = 1; i < n; i ++ )
	{
		int u, v;
		cin >> u >> v;
		add(u, v), add(v, u);
	}
	dfs(1, 1);
	while (m -- )
	{
		int x, y;
		char color;
		cin >> x >> y >> color;
		int tot = lca(x, y, color == 'H' ? 0 : 1);
		if (tot > 0) ans += '1';
		else ans += '0';
	}
	cout << ans;
	return 0;
}

Details

小提示:点击横条可展开更详细的信息

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 1536kb

input:

920 900
HHHHHHHHHHHHHHHHHHHHHHHHHGHHHHHHHHHHHHGHHHHHHHHHHHHHHHHHHHHHHHGHHGHHHHHHHGGHGHHHGHHGHGHHHHHH...

output:

1110110111101111111110111111111111111110011111110110011101110111111111111111110111111111111101111111...

result:

wrong answer 1st lines differ - expected: '001010011110011110101000100001...101010011101001110111011...

Test #2:

score: 0
Wrong Answer
time: 0ms
memory: 1540kb

input:

927 949
HHHHGHGGGHHHHGHGHHHHHGHGGHGGGHHHGGHHHHHHGGGGGGHHHGGHHHHGHHHHGGGHHHGHGHHHHGGGGHHGHHGGHGHGGGGG...

output:

1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...

result:

wrong answer 1st lines differ - expected: '111100001111011110110110011011...001100101101011111101111...

Test #3:

score: 0
Wrong Answer
time: 1ms
memory: 1536kb

input:

934 998
HHHHGHHHGHHGGGHGGHGHGHHHGHHGHGGGHHHHHGHHGGHHHHHHGHHHGGHHHHHGGHHHGHHHHHHHGHHGHGHGGHHGHGHHHGGH...

output:

1111111111111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111...

result:

wrong answer 1st lines differ - expected: '110101011111101011010101111111...101110111001110010111110...

Test #4:

score: 0
Wrong Answer
time: 1ms
memory: 1536kb

input:

941 947
HHHHHHHHHHHHHHHGHHHGHHHGHHHHGHHHGHHHHHHHHGHHHHHHHHHGHHHHHGHHHHHGHHGHHHHGHGGHHGHHHHHHHHHHGGHH...

output:

1111111111111111101111011111111111111111011101111111111111111011111111111111111111111111111111111111...

result:

wrong answer 1st lines differ - expected: '111111101101011000110101010111...010110001111011001100000...

Test #5:

score: 0
Wrong Answer
time: 183ms
memory: 30628kb

input:

92189 98896
HHHHHHHHHGHHHHGGGHGHHHHHHGHHHHHGHHHHHHHHHGHGHHHGHHHHHHGGHHHGHGGHHHHHHHHHGHHHHHHGHGHGHHHG...

output:

1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...

result:

wrong answer 1st lines differ - expected: '111011111111111111111111111111...111111111111111111111111...

Test #6:

score: 0
Wrong Answer
time: 193ms
memory: 31840kb

input:

95803 95747
HHHHHHHHHHHHHGHHHHHHHHHHHHHHHHHHGHHGHGHHHHGHGHHHHHHGGHHHHHHHHHHHHGHHHGHHHHHHHHHGHGGHHHHH...

output:

1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...

result:

wrong answer 1st lines differ - expected: '101101110111111111111111110111...111110110111111011111111...

Test #7:

score: 0
Wrong Answer
time: 194ms
memory: 30776kb

input:

92610 90996
HHGGGHHGHGGGHGHGHGGGGGGHGGGHGGHGHGHGGHHHHHGHGGHGHGGGGHGGGHGGHHGHHHGGHGHGGHGGGHGGGGGGGGGG...

output:

1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...

result:

wrong answer 1st lines differ - expected: '111111111111111111101111111111...111111001111111111111111...

Test #8:

score: 0
Wrong Answer
time: 167ms
memory: 31912kb

input:

96224 91494
HHHHHHHHGGHHGHHHHHHHHHHHHHGHHHGHHHHHHHHHHHHHHHHHGGHHHHHHGHHHHHHHHHHHHHHHHHGHGGGHGHGHHHHH...

output:

1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111011111111...

result:

wrong answer 1st lines differ - expected: '111101011011110011111111111101...110101101111111111111110...

Test #9:

score: 0
Wrong Answer
time: 168ms
memory: 30904kb

input:

93031 96743
HHHHHHHHHGHHHHHHHHHHHHHHHHHGGHHHHGHHHGHHHHHHHHHGHHHHHHHHHHHHHHGGHHHHHGGHGHHHGHHHGHHHHHHG...

output:

1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...

result:

wrong answer 1st lines differ - expected: '010001111110111101111111011100...111111110111001111110101...

Test #10:

score: 0
Wrong Answer
time: 191ms
memory: 32076kb

input:

96645 93594
HHHHHHHHHGHGHHHHHHHGHHHHGGGHHHHHHHHHHHHHHHHHHGHHHHHGHGHHHHHGHGHHHHHGHHHHGHHHHGGHHGHHHHGH...

output:

1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...

result:

wrong answer 1st lines differ - expected: '010100011011111001111111111111...111001111111111111011001...