50分的蒟蒻求助
查看原帖
50分的蒟蒻求助
234224
青鸟_Blue_Bird楼主2020/5/22 17:19

RT,不知道为什么一部分点对了,一部分错了,蒟蒻表示打得不是部分分QAQ

#include <bits/stdc++.h>
using namespace std;
#define N 300010

inline int read(){
	int x = 0, s = 1;
	char c = getchar();
	while(!isdigit(c)){
		if(c == '-')s = -1;
		c = getchar();
	}
	while(isdigit(c)){
		x = (x << 1) + (x << 3) + (c ^ '0');
		c = getchar();
	}
	return x * s;
}

struct node{
	int v, next;
} t3[N << 1];
int f[N];

struct node1{
	int v, next;
}t1[N << 1];
int f1[N];  

struct node2{
	int v, next;
}t2[N];
int f2[N << 1];

int bian = 0;
inline void add(int u, int v){
	t3[++bian] = (node){v, f[u]}, f[u] = bian;
	return ;
}

int bian1 = 0;
inline void add1(int u, int v){
	t1[++bian1] = (node1){v, f1[u]}, f1[u] = bian1;
	return ;
}

int bian2 = 0;
inline void add2(int u, int v){
	t2[++bian2] = (node2){v, f2[u]}, f2[u] = bian;
	return ;
}


int fa[N], siz[N], top[N], son[N]; 
int deth[N];
int s[N], t[N], w[N];
int tong1[N], tong2[N << 1];
int dist[N], path[N];
int ans[N];
/*以上为基本需求区*/

void dfs1_LCA(int now, int father){
	deth[now] = deth[father] + 1;
	fa[now] = father;
	siz[now] = 1;
	for(int i = f[now]; i; i = t3[i].next){
		int v = t3[i].v;
		if(v != fa[now]){
			dfs1_LCA(v, now);
			siz[now] += siz[v];
			if(siz[v] > siz[son[now]]){
				son[now] = v;
			}
		}
	}
	return ;
}

void dfs2_LCA(int now, int tp){
	deth[now] = deth[fa[now]] + 1;
	top[now] = tp;
	if(!son[now]) return;
	dfs2_LCA(son[now], tp);
	for(int i = f[now]; i; i = t3[i].next){
		int v = t3[i].v;
		if(v != son[now] && v != fa[now]){
			dfs2_LCA(v, v);
		}
	}
	return ;
}

int get_LCA(int x, int y){
	while(top[x] != top[y]){
		if(deth[top[x]] < deth[top[y]]) swap(x, y);
		x = fa[top[x]];
	}
	return deth[x] < deth[y] ? x : y;
}

void dfs(int now){
	int sum1 = tong1[w[now] + deth[now]], sum2 = tong2[w[now] - deth[now] + N];
	for(int i = f[now]; i; i = t3[i].next){
		int v = t3[i].v;
		if(v != fa[now]) dfs(v);
	}
	tong1[deth[now]] += path[now];
	for(int i = f1[now]; i; i = t1[i].next){
		int v = t1[i].v;
		tong2[dist[v] - deth[t[v]] + N]++;
	}
	ans[now] += tong1[w[now] + deth[now]] - sum1 + tong2[w[now] - deth[now] + N] - sum2;
	for(int i = f2[now]; i; i = t2[i].next){
		int v = t2[i].v;
		tong1[deth[s[v]]] --;
		tong2[dist[v] - deth[t[v]] + N] --;
	}
}

int main(){
//	freopen("P1600_1.in", "r", stdin);
//	siz[0] = -666;
	int n = read(), m = read();
	for(int i = 1;i < n; i++){
		int x = read(), y = read();
		add(x, y);
		add(y, x); 
	}
	dfs1_LCA(1, 1);
	dfs2_LCA(1, 1);
	for(int i = 1;i <= n; i++) w[i] = read();
	for(int i = 1; i <= m; i++){
		s[i] = read(), t[i] = read();
		int lca = get_LCA(s[i], t[i]);
//			printf("lca:  %d\n", lca);
		dist[i] = deth[s[i]] + deth[t[i]] - (deth[lca] << 1);
		path[s[i]]++;
		add1(t[i], i);
		add2(lca, i);
		if(deth[lca] + w[lca] == deth[s[i]]) ans[lca]--;
	}
	dfs(1);
	for(int i = 1;i <= n; i++) printf("%d ", ans[i]);
	return 0;
}
2020/5/22 17:19
加载中...