比赛 |
20191022轻松模拟测试 |
评测结果 |
WWWWWWWWWW |
题目名称 |
原谅 |
最终得分 |
0 |
用户昵称 |
liujiaqi |
运行时间 |
0.663 s |
代码语言 |
C++ |
内存使用 |
84.23 MiB |
提交时间 |
2019-10-22 17:25:08 |
显示代码纯文本
#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
int v, c;
template <class T> T read(T& x) {
x = 0; v = 1; c = getchar();
for (; c < '0' || c > '9'; c = getchar()) if (c == '-') v = -1;
for (; c >= '0' && c <= '9'; c = getchar()) x = (x << 3) + (x << 1) + (c ^ 48);
return x *= v;
}
const int N = 2000010;
int n, K, x, y, ans, a[N], id[N], b[N], br[N], t[N];
int cnt, head[N], fa[N];
bool vis[N];
struct Edge {
int y, nxt;
} e[N];
inline void add(int x, int y) {
e[++cnt].y = y; e[cnt].nxt = head[x]; head[x] = cnt;
}
inline bool cmp(int x, int y) {return a[x] > a[y];}
void dfs(int x, int faz) {
fa[x] = faz;
for (int i = head[x]; i; i = e[i].nxt) {
int y = e[i].y;
if (y != faz) dfs(y, x);
}
}
bool chek(int x) {
if (!fa[x]) return 1;
for (int i = 1; id[i] != x; ++i) {
if (!vis[id[i]]) continue;
if (fa[x] == id[i]) return 1;
}
return 0;
}
struct Node {
int x, val;
Node() {}
Node(int _, int __) {this -> x = _; this -> val = __;}
bool operator< (const Node& b) const {return val < b.val;}
};
std::priority_queue <Node> q;
void dijk(int s) {
vis[s] = 1;
q.push(Node(s, a[id[1]]));
for (; !q.empty(); ) {
Node f = q.top(); q.pop();
int x = f.x;
if (br[a[x] + 1] != t[a[x] + 1]) continue;
vis[x] = 1;
++t[a[x]]; ++ans;
if (ans == K) break;
for (int i = head[x]; i; i = e[i].nxt) {
int y = e[i].y;
if (y != fa[x]) q.push(Node(y, a[id[y]]));
}
}
}
int main() {
freopen("green.in", "r", stdin);
freopen("green.out", "w", stdout);
read(n); read(K);
for (int i = 1; i <= n; ++i) {
b[i] = read(a[i]);
id[i] = i;
}
for (int i = 1; i < n; ++i) {
read(x); read(y);
++x; ++y;
add(x, y); add(y, x);
}
std::sort(id + 1, id + n + 1, cmp);
if (n <= 2000) {
dfs(id[1], 0);
for (int i = 1; i <= K; ++i) {
if (chek(id[i])) {
vis[id[i]] = 1;
++ans;
}
}
printf("%d\n", ans);
return 0;
}
for (int i = 1; i <= n; ++i) {
b[i] = a[i];
}
std::sort(b + 1, b + n + 1);
cnt = std::unique(b + 1, b + n + 1) - b - 1;
for (int i = 1; i <= n; ++i) {
a[i] = std::lower_bound(b + 1, b + cnt + 1, a[i]) - b;
++br[a[i]];
}
dijk(id[1]);
printf("%d\n", ans);
return 0;
}