QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#544767#8716. 树shiqiaqiaya#WA 1ms3748kbC++202.1kb2024-09-02 20:10:402024-09-02 20:10:40

Judging History

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

  • [2024-09-02 20:10:40]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3748kb
  • [2024-09-02 20:10:40]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long
using namespace std;

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int n, m, q;
    cin >> n >> m >> q;

    vector<vector<int>> adj(n + 1);
    for (int i = 1; i <= n - 1; i++) {
    	int x, y;
    	cin >> x >> y;
    	adj[x].push_back(y);
    	adj[y].push_back(x);
    }

    vector<int> b(m + 1), c(m + 1, 1), dep(n + 1);
    vector<array<int, 32>> f(n + 1);
    auto dfs = [&](auto &&self, int x, int fa = 1) -> void {
    	f[x][0] = fa;
    	dep[x] = dep[fa] + 1;
    	for (auto y : adj[x]) {
    		if (y == fa) continue;
    		self(self, y, x);
    	}
    	for (int j = 1; j <= 20; j++) {
    		f[x][j] = f[f[x][j - 1]][j - 1];
    	}
    };

    dfs(dfs, 1);

    auto lca = [&](int x, int y) -> int {
    	if (dep[x] < dep[y]) swap(x, y);
    	for (int j = 20; j >= 0; j--) {
    		if (dep[f[x][j]] >= dep[y]) {
    			x = f[x][j];
    		}
    	}
    	if (x == y) return x;
    	for (int j = 20; j >= 0; j--) {
    		if (f[x][j] != f[y][j]) {
    			x = f[x][j];
    			y = f[y][j];
    		}
    	}
    	return f[x][0];
    };

    for (int i = 1; i <= m; i++) {
    	cin >> b[i];
    }

    int res = m;
    auto upd = [&](int x, int y) -> void {
		int l1 = lca(b[y], b[x]), l2 = lca(b[x], b[x + 1]), l3 = lca(b[y], b[x + 1]);
    	if (l2 == l1 && l3 == b[x + 1]) {
    		if (c[x + 1]) {
    			res -= c[x + 1];
	    		c[x + 1] ^= 1;
	    		res += c[x + 1];
    		}
    	} else if (l3 == l1 && l2 == b[x + 1]) {
    		if (c[x + 1]) {
    			res -= c[x + 1];
	    		c[x + 1] ^= 1;
	    		res += c[x + 1];
    		}
    	} else {
    		if (!c[x + 1]) {
				res -= c[x + 1];
	    		c[x + 1] ^= 1;
	    		res += c[x + 1];
    		}
    	}
    };

    for (int i = 3; i <= m; i++) {
    	upd(i - 2, i);
    }

    while (q--) {
    	int x, y;
    	cin >> x >> y;
    	b[x] = y;
    	if (x - 2 >= 1) {
    		upd(x - 2, x);
    	}
    	if (x + 2 <= m) {
    		upd(x, x + 2);
    	}
    	if (x - 1 >= 1 && x + 1 <= m) {
    		upd(x - 1, x + 1);
    	}
    	cout << res << "\n";
    }



    return 0;
}

详细

Test #1:

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

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: 3544kb

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:

183
184
184
184
184
184
184
184
184
183
183
183
183
183
182
182
181
181
181
181
181
181
181
181
181
181
181
182
182
181
180
180
180
180
180
180
180
179
179
179
180
180
180
180
180
181
181
181
182
181
181
180
180
180
180
180
180
180
180
180
181
182
181
181
181
181
181
182
181
182
182
182
182
182
182
...

result:

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