# POJ – 3417 Network 【樹上差分 LCA 思維】 好題!

AC Ciode

``````const int maxn = 1e5   5;
int up[maxn][23];
int deep[maxn], dis[maxn];
int n, m, q;
struct node {
int to, next, w;
}e[maxn<<1];
void init() {
Fill(up,0); Fill(deep,0);
cnt = 0;
}
void add(int u, int v, int w) {
}
void dfs(int u,int fa,int d) {
deep[u] = d   1;
for(int i = 1 ; i < 20 ; i   ) {
up[u][i] = up[up[u][i-1]][i-1];
}
for(int i = head[u] ; ~i ; i = e[i].next) {
int to = e[i].to;
if(to == fa) continue;
up[to][0] = u;
dfs(to, u, d 1);
}
}
int LCA_BZ(int u,int v) {
int mx = 0;
if(deep[u] < deep[v]) swap(u,v);
int k = deep[u] - deep[v];
for(int i = 19 ; i >= 0 ; i --) {
if((1<<i) & k) {
u = up[u][i];
}
}
if(u == v) return u;
for(int i = 19 ; i >= 0 ; i --) {
if(up[u][i] != up[v][i]){
u = up[u][i];
v = up[v][i];
}
}
return up[u][0];
}
ll ans;
ll dfs2(int u, int fa) {
int num = dis[u];
for (int i = head[u] ; ~i ; i = e[i].next) {
int to = e[i].to;
if (to == fa) continue;
num  = dfs2(to, u);
}
if (num < 2 && u != 1) {
if (num)    ans;
else ans  = m;
}
return num;
}
void solve() {
scanf("%d%d", &n, &m); init();
for (int i = 1 ; i < n ; i   ) {
int u, v;
scanf("%d%d", &u, &v);