@[protractor](/user/964822) 过了
首先您的 lg 数组没初始化,然后您的 dfs 用的 b 数组,但是树上差分为什么要用 a 数组啊(
```cpp
#include<iostream>
#include<vector>
#define a_ de[a]
#define b_ de[b]
using namespace std;
vector<int> v[50050];
int n,k;
int x,y,ans=0;
int de[50050],fa[50050][20];
int lg[50050];
int a[50050];
int b[50050];
void init(int d,int f)
{
fa[d][0]=f,de[d]=de[f]+1;
int i=1;
while(fa[d][i-1]){fa[d][i]=fa[fa[d][i-1]][i-1];i++;}
for(auto xx : v[d]) if(xx-f) init(xx,d);
}
int dfs(int d,int f)
{
for(auto xx : v[d]) if(xx-f) {
a[d] += dfs(xx, d);
}
ans=max(ans,a[d]);
return a[d];
}
int Lca(int a,int b)
{
int l;
if(a_<b_){int t=a;a=b,b=t;}
l=lg[a_-b_];
// cout << l << ' ' << a_ - b_ << endl;
while(a_-b_){if(de[fa[a][l]]>=b_) a=fa[a][l];l--;}
if(a==b) return a;
l=lg[a_];
while(fa[a][0]-fa[b][0]){if(fa[b][l]-fa[a][l]) b=fa[b][l],a=fa[a][l];l--;}
return fa[a][0];
}
int main()
{
lg[1] = 1;
for (int i = 2; i <= 50000; ++i) lg[i] = lg[i / 2] + 1;
cin>>n>>k;
for(int i=1;i<n;i++)
{
cin>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
// cout << 1 << endl;
init(1,0);
// cout << 1 << endl;
for(int i=1;i<=k;i++)
{
cin>>x>>y;
// cout << x << ' ' << y << endl;
int l=Lca(x,y);
// cout << x << ' ' << y << ' ' << l << endl;
// cout << l << ' ' << fa[l][0] << ' ' << x << ' ' << y << ' ' << i << endl;
a[x]++,a[y]++,a[l]--,a[fa[l][0]]--;
}
// cout << 2 << endl;
dfs(1,0);
cout<<ans;
return 0;
}
```
by QWQ_123 @ 2024-02-18 06:32:20
wssb,我b数组忘记加上a数组了,后来改上忘记更帖子了,另一个没看出来,谢谢大佬
by protractor @ 2024-02-18 08:06:06
@[QWQ_123](/user/740328)
by protractor @ 2024-02-18 08:06:22
A了,又是不写初始化的一天……
很好奇我代码原先是怎么输出正确答案的
by protractor @ 2024-02-18 08:09:30