树上查分0peters!
查看原帖
树上查分0peters!
743373
Special_Tony楼主2025/2/7 10:47
# include <bits/stdc++.h>

# define I return

# define AK 0

# define IOI ;

using namespace std;

typedef long long ll;

typedef pair <int, int> pii;

int n, m, x, y, s[100005], a[100005];

vector <int> v[100005];

bitset <100005> vis;

map <int, int> mp;

ll dfs (int x) {

	++ mp[s[x] += a[x]];

	ll sum = mp[s[x] - m];

	for (const int& i : v[x])
		s[i] = s[x], sum += dfs (i);

	return sum;

}

int main () {

	ios::sync_with_stdio (0);

	cin.tie (0);

	cout.tie (0);

	cin >> n >> m;

	for (int i = 1; i <= n; ++ i)
		cin >> a[i];

	for (int i = 1; i < n; ++ i)
		cin >> x >> y, v[x].emplace_back (y), vis[y] = 1;

	mp[0] = 1;

	for (int i = 1; i <= n; ++ i)
		if (! vis[i]) {

			cout << dfs (i);

			I AK IOI

		}

	cout << -1;

	I AK IOI

}
2025/2/7 10:47
加载中...