Skip to content

Instantly share code, notes, and snippets.

@psqq
Created December 18, 2015 15:25
Show Gist options
  • Save psqq/b63043296bdc544b53a8 to your computer and use it in GitHub Desktop.
Save psqq/b63043296bdc544b53a8 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <limits>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define forn(i, n) for (int i = 0; i < n; i++)
typedef vector<int> vi;
typedef pair<int, int> pi;
int n, m, a[111111], x, y, res = 0;
int main() {
freopen("aplusb.in", "r", stdin); freopen("aplusb.out", "w", stdout);
cin >> n >> m;
forn(i, n) cin >> a[i];
vector<vi> g(n, vi());
vi used(n, 0);
forn(i, n - 1) {
cin >> x >> y;
x--; y--;
g[x].push_back(y);
g[y].push_back(x);
}
queue<pi> q;
q.push(make_pair(0, a[0]));
while (!q.empty()) {
pi v = q.front();
used[v.first] = 1;
cout << v.first << endl;
q.pop();
forn(i, g[v.first].size()) {
cout << g[i].size() << " " << i << endl;
if (g[i].size() == 1 && i != 0) {
res++;
continue;
}
int next = g[v.first][i], d = v.second;
if (used[next]) continue;
if (a[next] == 0) {
d = 0;
}
else {
d += 1;
}
if (d > m) continue;
q.push(make_pair(next, d));
}
}
cout << res;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment