This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define rep(i, l, r) for(int i = l; i <= r; i++)
#define rep2(i, l, r) for(int i = l; i >= r; i--)
#define fi first
#define se second
#define bit(i, x) (x >> i & 1)
using namespace std;
const int N = 3e5 + 3;
const int mod = 1e9 + 7;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
long long rnd(long long l, long long r) {
return uniform_int_distribution<long long>(l, r)(rng);
}
int n, k, c[N];
vector<int> G[N];
pair<int, int> e[N];
namespace sub1{
int s[N], vst[N];
void dfs(int u, int mask) {
vst[u] = 1;
for(auto v : G[u]) if (!vst[v] && (mask & (1 << (c[v] - 1))) > 0) {
dfs(v, mask);
}
}
long long sol() {
int res = k;
rep(i, 1, n) s[c[i]] = i;
rep(mask, 1, (1 << k) - 1) {
int cnt = 0;
rep(i, 1, n) if (!vst[i] && (mask & (1 << (c[i] - 1))) > 0) {
cnt++;
dfs(i, mask);
}
if (cnt == 1) res = min(res, __builtin_popcount(mask) - 1);
rep(i, 1, n) vst[i] = 0;
}
return res;
}
}
int main(int argc, char* argv[]) {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen("testing.txt", "r", stdin);
// freopen("outputing.txt", "w", stdout);
cin >> n >> k;
rep(i, 1, n - 1) {
int u, v; cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
rep(i, 1, n) cin >> c[i];
cout << sub1::sol();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |