QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#449913#8716. 树JEdwardWA 2ms14440kbC++142.9kb2024-06-21 19:12:512024-06-21 22:28:15

Judging History

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

  • [2024-06-21 22:28:15]
  • 管理员手动重测该提交记录
  • 测评结果:WA
  • 用时:2ms
  • 内存:14440kb
  • [2024-06-21 19:12:51]
  • 提交

answer

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0), cin.tie(0)
#define int long long
#define endl '\n'
#define lowbit(x) (x)&(-x)
#define pii pair<int,int>
#define all(s) s.begin(), s.end()
#define ls(x) (x<<1)
#define rs(x) (x<<1|1) 
#define here system("pause")
using namespace std;

const int N = 2e5+5;
const int mod = 1e9+7;
const int INF = 1e15+7;
const double eps = 1e-6;
inline int pow(int a, int b, int p){ int res = 1%p; while(b){ if(b&1) res=res*a%p; a=a*a%p, b>>=1;} return res;}
inline int inv(int a, int p){ return pow(a,p-2,p)%p;}

int n, m, q;
vector<int> g[N];
int b[N], ok[N];
int f[N][20], dep[N];
inline void dfs(int u, int fa){
	f[u][0] = fa;
	dep[u] = dep[fa] + 1;
	for(int i=1;i<20 && f[u][i-1];i++){
		f[u][i] = f[f[u][i-1]][i];
	}
	for(auto v:g[u]){
		if(v==fa) continue;
		dfs(v, u);
	}
}

inline int getlca(int u, int v){
	if(dep[u]<dep[v]) swap(u, v);
	for(int i=19;i>=0;i--){
		if(dep[f[u][i]]>=dep[v]) u = f[u][i];
	}
	
	if(u==v) return u;
	for(int i=19;i>=0;i--){
		if(f[u][i]!=f[v][i]) u = f[u][i], v = f[v][i];
	}
	return f[v][0];
}

inline int check(int i){ //共链返回0 
	int x = b[i-1], y = b[i], z = b[i+1];
	if(x==z) return 1;
	if(getlca(x, y)==y || getlca(y, z)==y) return 0;
	else return 1; 
}

inline void sol(){
	cin >> n >> m >> q;
	for(int i=1;i<n;i++){
		int u, v;
		cin >> u >> v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	dfs(1, 0);
	
	for(int i=1;i<=m;i++) cin >> b[i];
	
	for(int i=2;i<m;i++){
		ok[i] = check(i);
	}
	
	int ans = accumulate(ok+1,ok+1+m,0LL);
	
	while(q--){
		int idx, now;
		cin >> idx >> now;
		b[idx] = now;
		
		if(idx==1){
			int delta = ok[2];
			ok[2] = check(2);
			delta -= ok[2];
			ans -= delta;
		}else if(idx==m){
			int delta = ok[m-1];
			ok[m-1] = check(m-1);
			delta -= ok[m-1];
			ans -= delta;
		}else if(idx==2){
			int delta = ok[2];
			ok[2] = check(2);
			delta -= ok[2];
			ans -= delta;
			
			delta = ok[3];
			ok[3] = check(3);
			delta -= ok[3];
			ans -= delta;
		}else if(idx==m-1){
			int delta = ok[m-1];
			ok[m-1] = check(m-1);
			delta -= ok[m-1];
			ans -= delta;
			
			delta = ok[n-2];
			ok[n-2] = check(n-2);
			delta -= ok[n-2];
			ans -= delta;
		}else{
			int delta = ok[idx-1];
			ok[idx-1] = check(idx-1);
			delta -= ok[idx-1];
			ans -= delta;
			
			delta = ok[idx];
			ok[idx] = check(idx);
			delta -= ok[idx];
			ans -= delta;
			
			delta = ok[idx+1];
			ok[idx+1] = check(idx+1);
			delta -= ok[idx+1];
			ans -= delta;
		}
//		for(int i=1;i<=m;i++){
//			cout << " " << b[i];
//		}cout << endl;
//		for(int i=1;i<=m;i++){
//			cout << " " << ok[i];
//		}cout << endl;
		
		cout << ans+2 << endl;
	}
}

signed main(){
	IOS;
	int tc = 1;
	while(tc--){
		sol();
	}
	return 0;
}
/*
10 8 2
1 2
2 3
2 6
6 7
3 5
3 4
1 8
8 9
9 10
4 5 6 2 3 8 4 1
1 4
2 3
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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:

182
183
183
183
183
183
183
183
183
183
184
184
184
184
184
184
184
184
184
184
184
184
184
184
184
184
184
185
185
185
185
185
184
184
184
184
184
184
184
184
184
184
184
184
185
185
184
184
185
184
184
184
184
184
184
184
184
184
184
184
184
185
185
184
184
184
184
185
184
185
185
185
185
185
184
...

result:

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