QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#602751#8716. 树UESTC_DebugSimulator#WA 1ms3708kbC++172.5kb2024-10-01 12:36:402024-10-01 12:36:41

Judging History

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

  • [2024-10-01 12:36:41]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3708kb
  • [2024-10-01 12:36:40]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define lowbit(i) (i&-i)
#define rand() (myrand())
using namespace std;
mt19937 myrand(time(0));
const int maxn = 2e5 + 5;
int n, _, m, q, fa[maxn][20], head[maxn], cnt, b[maxn], dep[maxn], c[maxn];
struct EDGE{
	int next, to;
}edge[maxn<<1];
void addedge(int from, int to) {
	edge[++cnt].next = head[from];
	edge[cnt].to = to;
	head[from] = cnt;
}
void dfs(int u, int p) {
	fa[u][0] = p;
	dep[u] = dep[p] + 1;
	for (int i = head[u] ; i ; i = edge[i].next) {
		int v = edge[i].to;
		if (v == p) continue;
		dfs(v, u);
	} 
}
int check(int u, int v) {
	if (dep[v] > dep[u]) {
		int k = dep[v] - dep[u];
		for (int i = 19 ; i >= 0 ; --i) {
			if (k&(1<<i)) v = fa[v][i];
		}
		if (v != u) return 2;
		return 0;
	}
	else {
		int k = dep[u] - dep[v];
		for (int i = 19 ; i >= 0 ; --i) {
			if (k&(1<<i)) u = fa[u][i];
		}
		if (v != u) return 2;
		return 1;
	}
}
int get(int x, int y) {
	if (x == y) return x == 2;
	if ((x == 1 && y == 0) || (x == 0 && y == 1)) return 1;
	if (x == 2) {
		if (y == 0) return 0;
		return 1;
	}
	if (y == 2) {
		if (x == 0) return 1;
		return 0;
	}
}
void solve() {
	cin >> n >> m >> q;
	for (int i = 1 ; i < n ; ++i) {
		int u, v;
		cin >> u >> v;
		addedge(u, v);
		addedge(v, u);
	}
	dfs(1, 0);
	for (int j = 1 ; j < 20 ; ++j) {
		for (int i = 1 ; i <= n ; ++i) fa[i][j] = fa[fa[i][j - 1]][j - 1];
	}
	for (int i = 1 ; i <= m ; ++i) cin >> b[i];
	if (m == 1) {
		for (int i = 1 ; i <= q ; ++i) {
			int p, w;
			cin >> p >> w;
			cout << 1 << '\n';
		}
		return;
	}
	int ans = 2;
	c[1] = check(b[1], b[2]);
	for (int i = 3 ; i <= m ; ++i) {
		c[i - 1] = check(b[i - 1], b[i]);
		ans += get(c[i - 2], c[i - 1]);
	}
	for (int i = 1 ; i <= q ; ++i) {
		int p, w;
		cin >> p >> w;
		b[p] = w;
		if (p == 1) {
			ans -= get(c[p], c[p + 1]);
			c[p] = check(b[p], b[p + 1]);
			ans += get(c[p], c[p + 1]);
		}
		else if (p == n) {
			ans -= get(c[p - 2], c[p - 1]);
			c[p - 1] = check(b[p - 1], b[p]);
			ans += get(c[p - 2], c[p - 1]);
		}
		else {
			if (p >= 3) ans -= get(c[p - 2], c[p - 1]);
			c[p - 1] = check(b[p - 1], b[p]);
			if (p >= 3) ans += get(c[p - 2], c[p - 1]);
			
			if (p <= n - 2) ans -= get(c[p], c[p + 1]);
			c[p] = check(b[p], b[p + 1]);
			if (p <= n - 2) ans += get(c[p], c[p + 1]);
		}
		cout << ans << '\n';
	}
}
signed main(void) {
	ios::sync_with_stdio(false);
	cin.tie(0);
	solve();
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3644kb

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: 1ms
memory: 3708kb

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:

180
181
181
181
181
182
182
182
183
183
183
183
183
183
183
183
183
183
183
183
183
183
183
183
183
183
183
183
183
183
183
183
183
184
184
184
184
184
184
184
184
184
183
183
183
184
184
184
184
184
185
186
186
186
186
186
186
186
186
186
186
186
186
187
187
187
187
187
187
187
187
187
187
187
187
...

result:

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