QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#415985#8716. 树chengpengWA 2ms3580kbC++112.2kb2024-05-21 13:47:542024-05-21 13:47:55

Judging History

你现在查看的是最新测评结果

  • [2024-05-21 13:47:55]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:3580kb
  • [2024-05-21 13:47:54]
  • 提交

answer

#include<bits/stdc++.h>
#define maxn 20
using namespace std;

const int N = 200010, M = N << 1;
int b[N], dph[N];
int h[M], e[M], ne[M], idx = 1;
int n, m, q;
int fa[N][maxn];

void add(int a, int b)
{
	e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}

void dfs(int u, int father)
{
	fa[u][0] = father;
	for (int i = h[u]; i; i = ne[i])
	{
		int v = e[i];
		if (v == father) continue;
		dph[v] = dph[u] + 1;
		dfs(v, u);
	}
}

int lca(int a, int b)
{
	if (dph[a] < dph[b]) swap(a, b);
	int t = dph[a] - dph[b];
	for (int i = maxn - 1; i >= 0; --i)
		if (t >> i & 1) a = fa[a][i];
	if (a == b) return a;
	else {
		for (int i = maxn - 1; i >= 0; --i)
			if (fa[a][i] != fa[b][i])
				a = fa[a][i], b = fa[b][i];
		return fa[a][0];
	}
}

int dist(int a, int b)
{
	int p = lca(a, b);
	return dph[a] + dph[b] - dph[p] * 2;
}

int checklink(int l, int p, int r)
{
	int dis1 = dist(l, p);
	int dis2 = dist(p, r);
	int dis3 = dist(l, r);
	return ((dis1 + dis2) == dis3);
}

int init() // 修改前的答案
{
	if (m <= 2) return m;
	
	int ans = 2;
	for (int i = 2; i < m; ++i)
		if (!checklink(b[i - 1], b[i], b[i + 1])) ans++;
	return ans;
}

int main()
{
	cin >> n >> m >> q;
	for (int i = 1; i < n; ++i)
	{
		int a, b;
		cin >> a >> b;
		add(a, b), add(b, a);
	}
	for (int i = 1; i <= m; ++i) cin >> b[i];
	
	dfs(1, 0);
	
	for (int i = 2; i <= n; ++i)
		for (int j = 1; j < maxn; ++j)
			fa[i][j] = fa[fa[i][j - 1]][j - 1];
	
	int res = init();
//	printf("res0=%d\n", res);
	int pos = 0, x = 0;
	for (int _ = 0; _ < q; ++_)
	{
		cin >> pos >> x;
//		if (pos - 2 >= 1)
//		{
//			int st1 = checklink(b[pos - 2], b[pos - 1], b[pos]);
//			int st2 = checklink(b[pos - 2], b[pos - 1], x);
//			res += st1 - st2;
//		}
//		if (pos - 1 >= 1 && pos + 1 <= m)
//		{
//			int st1 = checklink(b[pos - 1], b[pos], b[pos + 1]);
//			int st2 = checklink(b[pos - 1], x, b[pos + 1]);
//			res += st1 - st2;
//		}
//		if (pos + 2 <= m)
//		{
//			int st1 = checklink(b[pos], b[pos + 1], b[pos + 2]);
//			int st2 = checklink(x, b[pos + 1], b[pos + 2]);
//			res += st1 - st2;
//		}
		b[pos] = x;
		res = init();
		cout << res << endl;
	}
	
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3580kb

input:

5 5 3
2 1
3 2
1 4
5 1
1 5 4 2 3
1 3
5 3
3 3

output:

4
4
5

result:

ok 3 number(s): "4 4 5"

Test #2:

score: -100
Wrong Answer
time: 2ms
memory: 3520kb

input:

30 200 200
10 24
10 13
10 26
13 29
27 26
17 24
27 21
17 15
13 5
13 30
27 3
18 21
9 21
2 24
10 4
11 5
2 8
10 23
1 18
21 25
4 20
12 23
22 27
28 27
18 7
13 6
14 30
10 19
16 21
14 29 25 30 1 17 22 21 11 19 21 30 13 1 22 10 14 7 29 7 15 21 25 29 25 7 29 7 1 23 3 17 2 7 4 27 18 26 3 6 5 3 16 26 20 19 16 2...

output:

169
170
170
170
170
172
173
173
173
173
174
174
174
174
174
174
173
173
173
173
172
171
170
170
170
170
170
171
171
171
171
171
171
171
171
171
171
171
170
170
170
171
170
170
170
170
170
170
171
170
171
170
170
170
170
170
170
170
170
170
171
172
170
170
170
170
170
171
171
172
172
172
172
172
172
...

result:

wrong answer 1st numbers differ - expected: '174', found: '169'