Lang:G++
Edit12345678910111213141516171819202122232425262728293031#include <bits/stdc++.h>using namespace std;typedef long long LL;const int N = 100005;const int Q = 1e9 + 7;int n , K , f[N] , s[N] , a[N];pair<int , int> E[N];int getf(int x) {return f[x] == x ? x : f[x] = getf(f[x]);}vector<int> e[N];void work() {int i , j , x , y;for (i = 1 ; i <= n ; ++ i) {scanf("%d" , &a[i]);f[i] = i , s[i] = 1;e[i].clear();}for (i = 1 ; i < n ; ++ i) {scanf("%d%d",&x,&y);E[i] = make_pair(x , y);if (a[x] >= K && a[y] >= K) {x = getf(x) , y = getf(y);if (x != y) {f[x] = y;s[y] += s[x];}} else {if (a[x] < K && a[y] < K)continue;if (a[x] < K)e[x].push_back(y);else